Re: REALLY need help with SendMessage WM_GETCONTROLNAME

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks again for your help :) but it still return an empty string :(

Look like there is already a problem when calling :
ReturnMsg = SendMessage((IntPtr)hwndChild, SendMsg, SizePtr, NumRead);

MessageBox.Show("returnmsg=" + ReturnMsg);
MessageBox.Show("NumRead=" + NumRead);

Both value return 0 .....This is not the expected value...
Did you still see something wrong in my code?

Here is the code modified after your last comment :


[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter, string className, string windowTitle);
[DllImport("User32.dll")]
public static extern int FindWindow(String lpClassName,String
lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SetControlText( int hwnd, uint wMsg, int
wParam, string lParam);
[DllImport("user32.dll")]
public static extern UInt32
RegisterWindowMessage([MarshalAs(UnmanagedType.LPTStr)] String
lpString);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess,Int32
bInheritHandle,UInt32 dwProcessId);
[DllImport("kernel32")]
static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int
dwSize,uint dwFreeType);
[DllImport("kernel32")]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr
lpAddress,int dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32")]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr
lpBaseAddress,[Out] byte[] lpBuffer, UIntPtr nSize, IntPtr
lpNumberOfBytesRead);
System.Diagnostics.Process.Start("\"C:\\MyApp.exe\"", "");
int hwnd = 0;
IntPtr hwndChild = IntPtr.Zero;
hwnd = FindWindow(null, "Login");
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "TEdit", "");
if (hwndChild == IntPtr.Zero)
MessageBox.Show("Not found");
SetControlText((int)hwndChild, WM_SETTEXT, 0, "xxx"); //This work to
set the text so
hwndChild is valid and point to the right object
System.Diagnostics.Process[] processes;
processes = System.Diagnostics.Process.GetProcesses();
uint ProcessId;
IntPtr hProcess = IntPtr.Zero;
const short PROCESS_VM_OPERATION = 0x8;
const short PROCESS_VM_READ = 0x10;
const short PROCESS_VM_WRITE = 0x20;
foreach (System.Diagnostics.Process instance in processes)
{
if (instance.ProcessName == "PatientIndex")
{
ProcessId = (uint)instance.Id;
hProcess = OpenProcess(PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE, 0, (uint)instance.Id);
}
}
if (hProcess == IntPtr.Zero)
throw new ApplicationException("Failed to access process");
const int dwBufferSize = 65536;
byte[] bytearray = new byte[dwBufferSize];
const uint MEM_COMMIT = 0x1000;
const uint MEM_RELEASE = 0x8000;
const uint PAGE_READWRITE = 0x04;
IntPtr lpRemoteBuffer = IntPtr.Zero;
IntPtr threadId = IntPtr.Zero;
lpRemoteBuffer = VirtualAllocEx(hProcess, IntPtr.Zero, dwBufferSize,
MEM_COMMIT, PAGE_READWRITE);
if (lpRemoteBuffer == IntPtr.Zero)
throw new SystemException("Failed to allocate memory in remote
process");
System.UInt32 SendMsg;
SendMsg = RegisterWindowMessage("WM_GETCONTROLNAME");
long ReturnMsg;
IntPtr NumRead = IntPtr.Zero;
IntPtr SizePtr = (IntPtr)65536;
ReturnMsg = SendMessage((IntPtr)hwndChild, SendMsg, SizePtr, NumRead);

MessageBox.Show("returnmsg=" + ReturnMsg);
MessageBox.Show("NumRead=" + NumRead);
bool bSuccess;
UIntPtr BufferSize = (UIntPtr)dwBufferSize;
bSuccess = ReadProcessMemory(hProcess, lpRemoteBuffer, bytearray,
(UIntPtr)dwBufferSize, NumRead);
if (!bSuccess)
throw new SystemException("Failed to read from process memory");
string CtrlName = Encoding.Default.GetString(bytearray).TrimEnd('0');
MessageBox.Show(CtrlName); //This still return an empty string instead
of returning the name of the control : "PwEdit"
VirtualFreeEx(hProcess, lpRemoteBuffer, 0, MEM_RELEASE);


Thanks again!!

.



Relevant Pages

  • Re: Detecting a service that is running
    ... "OpenSCManagerA" (ByVal lpMachineName As String, ... "QueryServiceStatus" (ByVal hService As Long, ... public int dwControlsAccepted = 0; ... private static extern IntPtr OpenSCManager(string lpMachineName, ...
    (microsoft.public.vb.winapi)
  • Re: Detecting a service that is running
    ... "OpenSCManagerA" (ByVal lpMachineName As String, ... "QueryServiceStatus" (ByVal hService As Long, ... public int dwControlsAccepted = 0; ... private static extern IntPtr OpenSCManager(string lpMachineName, ...
    (microsoft.public.vb.general.discussion)
  • Re: Detecting a service that is running
    ... "OpenSCManagerA" (ByVal lpMachineName As String, ... public int dwControlsAccepted = 0; ... private static extern IntPtr OpenSCManager(string lpMachineName, ...
    (microsoft.public.vb.general.discussion)
  • Re: Detecting a service that is running
    ... "OpenSCManagerA" (ByVal lpMachineName As String, ... public int dwControlsAccepted = 0; ... private static extern IntPtr OpenSCManager(string lpMachineName, ...
    (microsoft.public.vb.winapi)
  • Re: LogonUser()
    ... // This sample can be run only on Windows XP. ... String lpszPassword, ... int dwLogonType, int dwLogonProvider, ref IntPtr phToken); ... private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr ...
    (microsoft.public.dotnet.security)