vb6 to .net conversion problem, the 'any' object


VB.NET Forum Moderator
Staff member
Jun 3, 2004
Lansing, MI; USA
Programming Experience
ok i've got this piece of code that gets the names of all the computers in the work group for a local lan and displays them in a listbox (which is what i want). i ran the project through the .net converter and it did it's thing, here's the section(s) of the .net code:

Private Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Integer, ByVal level As Integer, ByRef buf As Object, ByVal prefmaxlen As Integer, ByRef entriesread As Integer, ByRef totalentries As Integer, ByVal servertype As Integer, ByVal domain As Integer, ByRef resume_handle As Integer) As Integer
Private Const MAX_PREFERRED_LENGTH As Integer = -1
Private Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Private Const NERR_SUCCESS As Integer = 0
Private Const ERROR_MORE_DATA As Integer = 234
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef pTo As Object, ByRef uFrom As Object, ByVal lSize As Integer)
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Integer) As Integer
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Integer) As Integer

Private Structure SERVER_INFO_100
		Dim sv100_platform_id As Integer
		Dim sv100_name As Integer
	End Structure

Private Sub frmShutdown_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
	End Sub

Private Function GetServers(ByRef sDomain As String) As Integer
		'lists all servers of the specified type
		'that are visible in a domain.
		Dim bufptr As Integer
		Dim dwEntriesread As Integer
		Dim dwTotalentries As Integer
		Dim dwResumehandle As Integer
		Dim se100 As SERVER_INFO_100
		Dim success As Integer
		Dim nStructSize As Integer
		Dim cnt As Integer
		'UPGRADE_ISSUE: LenB function is not supported. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1040"'
        nStructSize = Len(se100)
		'Call passing MAX_PREFERRED_LENGTH to have the
		'API allocate required memory for the return values.
		'The call is enumerating all machines on the
		'network (SV_TYPE_ALL); however, by Or'ing
		'specific bit masks for defined types you can
		'customize the returned data. For example, a
		'value of 0x00000003 combines the bit masks for
		'SV_TYPE_WORKSTATION (0x00000001) and
		'SV_TYPE_SERVER (0x00000002).
		'dwServerName must be Null. The level parameter
		'(100 here) specifies the data structure being
		'used (in this case a SERVER_INFO_100 structure).
		'The domain member is passed as Null, indicating
		'machines on the primary domain are to be retrieved.
		'If you decide to use this member, pass
		'StrPtr("domain name"), not the string itself.
        success = NetServerEnum(0, 100, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_ALL, 0, dwResumehandle)
		'if all goes well
		If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then
			'loop through the returned data, adding each
			'machine to the list
			For cnt = 0 To dwEntriesread - 1
				'get one chunk of data and cast
				'into an SERVER_INFO_100 struct
				'in order to add the name to a list
				'UPGRADE_WARNING: Couldn't resolve default property of object se100. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1037"'
				CopyMemory(se100, bufptr + (nStructSize * cnt), nStructSize)
		End If
		'clean up regardless of success
		Call NetApiBufferFree(bufptr)
		'return entries as sign of success
		GetServers = dwEntriesread
	End Function
	Public Function GetPointerToByteStringW(ByVal dwData As Integer) As String
		Dim tmp() As Byte
		Dim tmplen As Integer
		If dwData <> 0 Then
			tmplen = lstrlenW(dwData) * 2
			If tmplen <> 0 Then
				ReDim tmp((tmplen - 1))
				CopyMemory(tmp(0), dwData, tmplen)
				'UPGRADE_TODO: Code was upgraded to use System.Text.UnicodeEncoding.Unicode.GetString() which may not have the same behavior. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1059"'
				GetPointerToByteStringW = System.Text.UnicodeEncoding.Unicode.GetString(tmp)
			End If
		End If
	End Function

now when i change the two keywords 'Any' to 'Object' vb.net takes it just fine, but when i run it i get the exception: 'Additional information: Specified OLE variant is invalid.' on the line: 'success = NetServerEnum(0, 100, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_ALL, 0, dwResumehandle)'

so yea, any help would be appriciated


Aug 19, 2004
Programming Experience
What I have found is to figure out exactly what type of variable the "as any" variable is. For example, if you can figure out that the variable is used as a string, declare it as a string.
Top Bottom