how to get control handle from dll
Is it possible to get a control handle of a particular control in the windows dialog when i inject a dll into the dialog? I have tried the same but it didnt work and for that I have also searched Microsoft articles via winAPI but did not get any successful results? I am not able to find the main dialog handle as well? Can anyone please teach me how to do the same. Thanks in advance.
Re: how to get control handle from dll
You can try to call FindWindows, which is explained more here on this link, with the name of window to get the handle on the main dialog window or even if you call the classname, which should be known by you, with Spy++ you can easily grab it as an argument. After that you can call EnumChildWindows, code for it is here, to search for the handles to the controls within the main dialog.
Re: how to get control handle from dll
You can also use IntPtr type to pass Handle. IntPtr type is being used in the Control.Handle Property, if you can see for example. C# functions that is the API can used by C++ classes but it can used directly in C#. You can use C++/CLI wrapper that calls C++ classes directly and exposes .NET interface to C# client, which will be the best way to use C++ code. Although C++/CLI is a better way, there are other way to C wrapper for C++ classes and using it from C#
Re: how to get control handle from dll
Check out the below sample code that will help you to get started:
Code:
#define Title 0x130
BOOL CALLBACK EnumChildProc
(
HWND hwnd,
LPARAM lParam
)
{
TCHAR szWindowText[256];
HWND* lpHwndReceiver = (HWND*)lParam;
GetWindowText
(
hwnd,
szWindowText,
sizeof(szWindowText)
);
if (_tcscmp(szWindowText, TEXT("start")) == 0)
{
LONG_PTR lValTemp = GetWindowLongPtr
(
hwnd,
GWLP_ID
);
if (lValTemp == Title)
{
*lpHwndReceiver = hwnd;
return FALSE;
}
}
return TRUE;
}
void GetStartButton1(HWND& hWndStartButtonReceiver)
{
hWndStartButtonReceiver = NULL;
HWND hWndShellTrayWindow = NULL;
hWndShellTrayWindow = FindWindow(TEXT("Shell_TrayWnd"), NULL);
if (hWndShellTrayWindow)
{
EnumChildWindows
(
hWndShellTrayWindow,
EnumChildProc,
(LPARAM)&hWndStartButtonReceiver
);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
HWND hWndStartButton = NULL;
GetStartButton1(hWndStartButton);
return 0;
}