Article View: borland.public.cppbuilder.nativeapi
Article #12940Re: Control pointer from window handle
From: nicolasr
Date: Fri, 07 Nov 2003 18:39
Date: Fri, 07 Nov 2003 18:39
133 lines
3985 bytes
3985 bytes
Hi,
I tried to reproduce the behavior in a very simple test app,
I did not use shared memory or wait for events...
Here's the complete code:
"Client" Application with a button and the following handler:
//exported from the DLL
typedef void (*PPost)(HWND h, unsigned int message);
void __fastcall TForm1::Button1Click(TObject *Poster)
{
HWND h = FindWindow("TForm2", NULL);
if(h != NULL)
{
HINSTANCE hinst = LoadLibrary("HookDll.dll");
if(hinst != NULL)
{
PPost Post = (PPost)GetProcAddress(hinst, "_Post");
if(Post != NULL)
Post(h, WM_USER+1);
else
MessageBox(NULL, "GetProcAddress failed", "", 0);
FreeLibrary(hinst);
}
else
MessageBox(NULL, "LoadLibrary failed", "", 0);
}
else
MessageBox(NULL, "Window TForm2 not found", "", 0);
}
HookDll with the following code:
#include <vcl.h>
#pragma hdrstop
HINSTANCE theInst = NULL;
HHOOK hMessageHook = NULL;
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst,
unsigned long reason,
void* lpReserved)
{
if(reason == DLL_PROCESS_ATTACH)
{
MessageBox(NULL, "Process Attached", "", 0);
if(theInst == NULL)
theInst = hinst;
}
else if(reason == DLL_PROCESS_DETACH)
{
MessageBox(NULL, "Process Detached", "", 0);
}
return TRUE;
}
LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
{
if(code == HC_ACTION)
{
MSG* msg = (MSG*)lParam;
if(msg->message == WM_USER+1)
{
MessageBox(NULL, "Received the message", "", 0);
HWND h = (HWND)msg->lParam;
/* this works
char buffer[100];
GetClassName(h, buffer, 99);
//show the class name
MessageBox(NULL, buffer, "", 0);
*/
//because of this line LoadLibrary fails
TWinControl* ctrl = FindControl(h);
//((TForm*)ctrl)->Color = clRed;
}
}
return CallNextHookEx(hMessageHook, code, wParam, lParam);
}
extern "C" __declspec(dllexport)
void Post(HWND hwnd, unsigned int message)
{
//hook the target application
hMessageHook =
SetWindowsHookEx(WH_GETMESSAGE,
(HOOKPROC)GetMsgProc,
theInst,
GetWindowThreadProcessId(hwnd, NULL));
if(hMessageHook == NULL)
{
MessageBox(0, "Failed to hook", "", 0);
return;
}
PostMessage(hwnd, message, 0, (long)hwnd);
//instead of waiting for a return event
MessageBox(NULL, "Block client app", "", 0);
BOOL r = UnhookWindowsHookEx(hMessageHook);
if(r == 0)
MessageBox(0, "Failed to unhook", "", 0);
}
The "Server" app has a dummy form with classname "TForm2".
any ideas?
Nick
Technical Details
Message-ID:
<3fabe6c6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3fa70ef0@newsgroups.borland.com> <3fa77bef$1@newsgroups.borland.com> <3faa15d0$1@newsgroups.borland.com> <3fab013a$1@newsgroups.borland.com> <3fab02b9$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,eb9c48fffbb335b9 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-11-07 10:41:18 PST From: "nicolasr" <nicolasrNOSPAMATSIGNgmx.net> Newsgroups: borland.public.cppbuilder.nativeapi References: <3fa70ef0@newsgroups.borland.com> <3fa77bef$1@newsgroups.borland.com> <3faa15d0$1@newsgroups.borland.com> <3fab013a$1@newsgroups.borland.com> <3fab02b9$1@newsgroups.borland.com> Subject: Re: Control pointer from window handle Date: Fri, 7 Nov 2003 19:39:01 +0100 Lines: 136 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 80.133.180.47 Message-ID: <3fabe6c6$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1068230342 80.133.180.47 (7 Nov 2003 10:39:02 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail