Connecting Tech Pros Worldwide Help | Site Map

How can I automate Winforms of .NET in VB6?

Newbie
 
Join Date: Mar 2009
Posts: 2
#1: Mar 1 '09
I want to get the ControlName of .NET properties in VB6 code, just like described in this article. But unfortunately, it doesn't work for me. I always got 0 for bufferMem.
Expand|Select|Wrap|Line Numbers
  1.         bufferMem = VirtualAllocEx(processHandle, 0, size, 
  2.         MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
  3.         If bufferMem = 0 Then
  4.             Error Err, "VirtualAllocEx API Failed"
  5.         End If
What am I doing wrong?
Newbie
 
Join Date: Mar 2009
Posts: 2
#2: Mar 9 '09

re: How can I automate Winforms of .NET in VB6?


Hi All,

I have managed VirtualAllocEx. Before I declared this function as Private. Then when I moved it to Module file and changed it to Public, I don't get 0 anymore for bufferMem. Also I need to set lpAddress as ByVal. Otherwise, the length of controlName that given is twice than correct length (I don't know exactly how this happens).

Expand|Select|Wrap|Line Numbers
  1. Declare Function VirtualAllocEx Lib "kernel32" _
  2.                              (ByVal hProcess As Long, _
  3.                              ByVal lpAddress As Long, _
  4.                              ByVal dwSize As Long, _
  5.                              ByVal flAllocationType As Long, _
  6.                              ByVal flProtect As Long) As Long
Now I got the correct retLength, but unfortunately retVal is 0, return the error description: Invalid procedure call or argument.

Expand|Select|Wrap|Line Numbers
  1. If retLength <> 0 Then
  2.           'Now read the component's name from the shared memory location.
  3.               retVal = ReadProcessMemory(processHandle, bufferMem, bytearray, size, VarPtr(written))
  4.               If retVal = 0 Then
  5.                    Error Err 'ReadProcessMemory API Failed
  6.               End If
  7.           End If
My declaration of ReadProcessMemory is:

Expand|Select|Wrap|Line Numbers
  1. Declare Function ReadProcessMemory Lib "kernel32" _
  2.                              (ByVal hProcess As Long, _
  3.                              ByVal lpBaseAddress As Long, _
  4.                              lpBuffer As Any, _
  5.                              ByVal nSize As Long, _
  6.                              lpNumberOfBytesRead As Long) As Long
And when I run the whole thing (ignore the retVal value), I got error in WideCharToMultiByte:

Expand|Select|Wrap|Line Numbers
  1. Function ByteArrayToString(bytes As String, length As Long) As String
  2.    Dim retValStr As String
  3.    Dim l As Long
  4.  
  5.    If IsWin9x() Then
  6.        retValStr = Left(bytes, InStr(1, bytes, Chr(0)) - 1)
  7.    Else
  8.        retValStr = String$(length + 1, Chr(0))
  9.        l = WideCharToMultiByte(CP_ACP, 0, bytes, -1, retValStr, length + 1, Null, Null)
  10.    End If
  11.  
  12.    ByteArrayToString = retValStr
  13. End Function
The error is saying:

Expand|Select|Wrap|Line Numbers
  1. Run-time error '94':
  2.  
  3. Invalid use of Null
Any idea?

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. Function GetWindowsFormsID(ByVal wnd As Long) As String
  2.     Dim PID As Long 'pid of the process that contains the control
  3.     Dim msg As Long
  4.  
  5.     ' Define the buffer that will eventually contain the desired
  6.     ' component's name.
  7.     Dim bytearray As String * 65526
  8.  
  9.     ' Allocate space in the target process for the buffer as shared
  10.     ' memory.
  11.     Dim bufferMem As Long
  12.     ' Base address of the allocated region for the buffer.
  13.     Dim size As Long
  14.     ' The amount of memory to be allocated.
  15.     Dim written As Long
  16.     ' Number of bytes written to memory.
  17.     Dim retLength As Long
  18.     Dim retVal As Long
  19.     Dim errNum As Integer
  20.     Dim errDescription As String
  21.  
  22.     size = 65527 'Len(bytearray)
  23.  
  24.     ' Creating and reading from a shared memory region is done
  25.     ' differently in Win9x than in newer Oss.
  26.     Dim processHandle As Long
  27.     Dim fileHandle As Long
  28.  
  29.     msg = RegisterWindowMessage("WM_GETCONTROLNAME")
  30.  
  31.     If Not IsWin9x() Then
  32.         On Local Error GoTo Error_Handler_NT
  33.             Dim dwResult As Long
  34.             Call GetWindowThreadProcessId(wnd, PID)
  35.  
  36.             processHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, PID)
  37.             If processHandle = 0 Then
  38.                 Error Err 'OpenProcess API Failed
  39.             End If
  40.  
  41.             bufferMem = VirtualAllocEx(processHandle, 0, size, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
  42.             If bufferMem = 0 Then
  43.                 Error Err 'VirtualAllocEx API Failed
  44.             End If
  45.  
  46.             ' Send message to the control's HWND for getting the
  47.             ' Specified control name.
  48.             retLength = SendMessage(wnd, msg, size, ByVal bufferMem)
  49.  
  50.             If retLength <> 0 Then
  51.             ' Now read the component's name from the shared memory location.
  52.                 retVal = ReadProcessMemory(processHandle, bufferMem, bytearray, size, VarPtr(written))
  53.                 If retVal = 0 Then
  54.                      Error Err 'ReadProcessMemory API Failed
  55.                 End If
  56.             End If
  57.  
  58. Error_Handler_NT:
  59.             errNum = Err
  60.             errDescription = Error$
  61.             ' Free the memory that was allocated.
  62.             retVal = VirtualFreeEx(processHandle, bufferMem, 0, MEM_RELEASE)
  63.             If retVal = 0 Then
  64.                 Error Err 'VirtualFreeEx API Failed
  65.             End If
  66.             CloseHandle (processHandle)
  67.             If errNum <> 0 Then
  68.                 On Local Error GoTo 0
  69.                 Error errNum 'errDescription
  70.             End If
  71.         On Local Error GoTo 0
  72.  
  73.  
  74.     Else
  75.        On Local Error GoTo Error_Handler_9x
  76.  
  77.             Dim SA As SECURITY_ATTRIBUTES
  78.  
  79.             fileHandle = CreateFileMapping(INVALID_HANDLE_VALUE, SA, PAGE_READWRITE, 0, size, Null)
  80.             If fileHandle = 0 Then
  81.                 Error Err 'CreateFileMapping API Failed
  82.             End If
  83.             bufferMem = MapViewOfFile(fileHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
  84.             If bufferMem = 0 Then
  85.                 Error Err 'MapViewOfFile API Failed
  86.             End If
  87.  
  88.             Call CopyMemory(bufferMem, bytearray, size)
  89.  
  90.             ' Send message to the treeview control's HWND for
  91.             ' getting the specified control's name.
  92.             retLength = SendMessage(wnd, msg, size, bufferMem)
  93.  
  94.             ' Read the control's name from the specific shared memory
  95.             ' for the buffer.
  96.             Call CopyMemoryA(bytearray, bufferMem, 1024)
  97.  
  98. Error_Handler_9x:
  99.             errNum = Err
  100.             errDescription = Error$
  101.  
  102.             ' Unmap and close the file.
  103.             UnmapViewOfFile (bufferMem)
  104.             CloseHandle (fileHandle)
  105.  
  106.             If errNum <> 0 Then
  107.                 On Local Error GoTo 0
  108.                 Error errNum 'errDescription
  109.             End If
  110.         On Local Error GoTo 0
  111.  
  112.     End If
  113.  
  114.     If retLength <> 0 Then
  115.     ' Get the string value for the Control name.
  116.     GetWindowsFormsID = ByteArrayToString(bytearray, retLength)
  117.     End If
  118.  
  119. End Function
Reply

Tags
.net, automate, control, vb6, win32


Similar Visual Basic 4 / 5 / 6 bytes