Get HWND of Application (No Window)

Is it possible to get the HWND of my own window, without knowing the Caption or ClassName of the window. I can't use FindWindow because the program does not have an associated window, it just runs in the background. I need to get the HWND of myself (the running app needs to get the HWND of itself) so that a DLL i'm interfacing with can send a message to it.

Thanks in advance for your help.
[411 byte] By [Chazwazza] at [2007-11-20 2:47:46]
# 1 Re: Get HWND of Application (No Window)
I can't use FindWindow because the program does not have an associated window

So do you have window or not? Are you calling ::CreateWindow() in your code? if you do simply save that handle and thats it.

Cheers
golanshahar at 2007-11-9 13:26:02 >
# 2 Re: Get HWND of Application (No Window)
The program doesn't have a window. It just calls WinMain and runs the code.

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
// Code here
}

etc
Chazwazza at 2007-11-9 13:27:11 >
# 3 Re: Get HWND of Application (No Window)
Well, if you don't have a window, how can you have a handle to a window? Create an invisible window for this.

Viggy
MrViggy at 2007-11-9 13:28:09 >
# 4 Re: Get HWND of Application (No Window)
So to send messages to an application, the application has to have a window?? In that case, what is the easiest way to create an invisible window, I'm using just Win32 C++. Should I use CreateWindow()?
Chazwazza at 2007-11-9 13:29:11 >
# 5 Re: Get HWND of Application (No Window)
Check this out:
Message only windows (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowfeatures.asp)

These are lightweight windows with no painting etc. Perhaps it is suitable for you since you can just handle the messages you are interested in. You will still need a message pump. ( GetMessage/TranslateMessage/DispatchMessage ). Just create a sample Helloworld Win32 app and you should get a skeleton code. Simply replace the creation part to create a message only window and remove the WM_PAINT and other stuff.
kirants at 2007-11-9 13:30:10 >
# 6 Re: Get HWND of Application (No Window)
So to send messages to an application, the application has to have a window?? In that case, what is the easiest way to create an invisible window, I'm using just Win32 C++. Should I use CreateWindow()?

If you want to use ::SendMessage()/::PostMessage() then yes you must have a window for that.

You can use other IPC mechanism that doesnt use hwnd. For more info look here: Interprocess Communications (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/interprocess_communications.asp).

Cheers
golanshahar at 2007-11-9 13:31:15 >
# 7 Re: Get HWND of Application (No Window)
Thanks guys. I'll give it a go when I boot back into Windows (Dual booting SuSE atm).
Chazwazza at 2007-11-9 13:32:16 >
# 8 Re: Get HWND of Application (No Window)
Why does the following code not give me a HWND, I used Spy++ and searched for the name of my program "Big Brother" in both the Caption and Class Name, but I found nothing.

/* OTHER DECLARATIONS HERE */
TCHAR *szTitle = "Big Brother";
TCHAR *szWindowClass = "Big Brother";
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
InitInstance(hInstance, nCmdShow);
/* OTHER CODE HERE */
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

return TRUE;
}

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
switch (LOWORD(wParam))
{

}
return TRUE;

case WM_DESTROY:
PostQuitMessage(0);
return TRUE;

case WM_CLOSE:
PostQuitMessage(0);
return TRUE;
case WM_TIMER:
TimerCall();
return TRUE;
}

return FALSE;
}
Chazwazza at 2007-11-9 13:33:19 >
# 9 Re: Get HWND of Application (No Window)
You have to register the window class ( http://msdn2.microsoft.com/en-gb/library/ms633586.aspx) before CreateWindow.
ovidiucucu at 2007-11-9 13:34:14 >
# 10 Re: Get HWND of Application (No Window)
You have to register the window class ( http://msdn2.microsoft.com/en-gb/library/ms633586.aspx) before CreateWindow.

I should probably also use ShowWindow I just realised, because at the moment all I have is a handle, I havn't shown it. "ShowWindow(hWnd,SW_HIDE)"??
Chazwazza at 2007-11-9 13:35:13 >
# 11 Re: Get HWND of Application (No Window)
I should probably also use ShowWindow I just realised, because at the moment all I have is a handle, I havn't shown it. "ShowWindow(hWnd,SW_HIDE)"??

If you want to show it you need to use:

::ShowWindow(hWnd,SW_SHOW);

Cheers
golanshahar at 2007-11-9 13:36:21 >
# 12 Re: Get HWND of Application (No Window)
But It's a Message Only window, you can still see the Title and Class using SW_HIDE can't you, it doesn't need to be shown? (It's a message only window keep in mind)
Chazwazza at 2007-11-9 13:37:19 >
# 13 Re: Get HWND of Application (No Window)
use "STATIC" in the window class
Mitsukai at 2007-11-9 13:38:14 >
# 14 Re: Get HWND of Application (No Window)
use "STATIC" in the window class

I'm sorry, I don't understand, is that a reply to my question or something else? Can you please be more specific, I don't know where to put "STATIC".
Chazwazza at 2007-11-9 13:39:19 >
# 15 Re: Get HWND of Application (No Window)
It seems you are new to windows programming. Suggest, you read a good book like "Windows programming - Charles Petzold" The book is very good to have a good understanding of windows programming and most questions that would arise in your head, and also, those that don't, will be answered when you have gone through those basics.
kirants at 2007-11-9 13:40:23 >
# 16 Re: Get HWND of Application (No Window)
I'm not new to Windows Programming in general, I'm just used to using lanuages like VB and C# where most of the low level stuff is done for you, I've also never had a need to create a Message Only window until now. Although you are correct in saying I'm new to Windows Programming for C++, I'll be sure to check out that book.
Chazwazza at 2007-11-9 13:41:25 >
# 17 Re: Get HWND of Application (No Window)
Sorry. I should've said, Win32 SDK programming.
True, VB /C# can hide lot of stuff. And so does MFC to a large extent. So, it is difficult to understand why somethign is not working. Hence , suggest reading that book by Petzold. It is considered the bible. BTW, he has written a book written in a similar style for Programming Windows for .Net ( using C# ) also.
kirants at 2007-11-9 13:42:19 >