473,491 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I automate Winforms of .NET in VB6?

2 New Member
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?
Mar 1 '09 #1
1 2726
oliverikawood
2 New Member
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
Mar 9 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4807
by: Kate Gibbs | last post by:
I need to write a simple utility in VB.NET that reads an Excel file, gets some data from a sheet, then...it needs to automate internet explorer in a robot manner. I know that Application Centre...
1
290
by: Helen W | last post by:
The Knowledge Base article below explains how to talk to a running instance of an Office program: http://support.microsoft.com/default.aspx?scid=kb;EN-US;316126 Is there any way I can talk...
4
3709
by: Elhanan | last post by:
hi.. all a client of ours is considering to move from a dos application to windows desktop application. the application is for traveling agency, the database is rather large. their current...
5
4060
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
1
8509
by: Pieter | last post by:
Hi, In my application VB.NET 2005 application I placed a ReportViewer, and link it to a server-report: Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote...
4
3908
by: 3Cooks | last post by:
I have a windows application written in Visual Basic 6.0 that is going to be redeveloped in dotNET. We are trying to decide if we should deploy using Webforms or Winforms and I need advice from...
5
3804
by: brian.wilson4 | last post by:
Our group is currently comparing winforms vs webforms.....app is Corp LAN based - we have control of desktops.....Below is pros and cons list we have come up with - if anything strikes you as...
11
8749
by: gert365 | last post by:
I'm working on a scirpt to be used on a windows machine and I need to automate a user's input on the command prompt. For example I'm using os.system('mycommand') to excute the commands I want. ...
2
1492
by: =?Utf-8?B?QWxleGFuZGVyIFd5a2Vs?= | last post by:
Is it possible to automate a COM object ebmeded in an excel document run the process and return the results in a C# .NET application? Or better yet extract the com object some how and just run it...
23
4458
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.