Link to home
Start Free TrialLog in
Avatar of ymailhot
ymailhot

asked on

Get info from call back function

I implemented a system wide hook to detect when error dialogs from the WebBrowser are created. Therefore the callback function is in a DLL.  My main program needs to know when an attempt to create one of these windows occur. But only the callback function in the dll knows. How can the dll tell my main program that something occured so that the main program can display a message.
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ymailhot
ymailhot

ASKER

Thank you, it works.

In my DLL I have the following function:

NewWindowHookCallBack(pCode: Integer; pHandle: WPARAM; pWindow: LPARAM): LRESULT; stdcall;

From what I have read, I should be able to get the Class and the Window title from pWindow: LPARAM.  That would save me from using additional APIs to find this information.

LPARAM is a long pointer to a structure. I have been trying to access values from this structure but with no success. Would you have any example of that ?

Thank you.
Hmmm. Could you give me the code where you try to use LPARAM?
I guess (from what you've written) that you try to use LPARAM as a pointer in the main application after you got the message from the dll, right? If yes: That won't work...   :-(
Some theory:
Each win32 process has it's own memory/address context and can address the full 32bit pointer range from $0 to $FFFFFFFF. That means: The same pointer that points to some informations in process A, DOES NOT point to the same informations if you're using it in process B. In process B it points to a completely different memory location. Perhaps you'll even get an Access Violation.
So if you want to transport memory blocks from one process to another, you'll have to use the WM_COPYDATA message.

Regards, Madshi.
I am trying to access the LPARAM info from the DLL itself. I need to look at the class first to see if it is a #32770 dialog in which case I don't allow it to open. Secondly, because I want to allow some #32770 (that's the class of the dialogs), I will either look at the Window title (which I really don't like in case my application is used with other versions of Windows) or the size and position.

Here is my function (which you wrote most of):

function NewWindowHookCallBack(pCode: Integer; pHandle: WPARAM; pWindow: LPARAM): LRESULT; stdcall;
var
  vWindowClass: array[0..80] of char;
begin
  Result:=0;

  if pCode = HCBT_CREATEWND then
  begin
    GetClassName(pHandle, vWindowClass, sizeof(vWindowClass) - 1);
    if IsWindow(pHandle) and (StrComp(vWindowClass, '#32770') = 0) then
    begin
      PostMessage(FindWindow('TFormMainMenu', nil), WM_User + 1, 555, 777);
      Result:=1;
    end;
  end;
end;


I found the description of the structure pWindow points to in Win32.Hlp:

typedef struct tagCBT_CREATEWND { // cbtcw  
    LPCREATESTRUCT lpcs;
    HWND           hwndInsertAfter;
} CBT_CREATEWND;


AND lpcs:

typedef struct tagCREATESTRUCT { // cs  
    LPVOID    lpCreateParams;
    HINSTANCE hInstance;
    HMENU     hMenu;
    HWND      hwndParent;
    int       cy;
    int       cx;
    int       y;
    int       x;
    LONG      style;
    LPCTSTR   lpszName;
    LPCTSTR   lpszClass;
    DWORD     dwExStyle;
} CREATESTRUCT;


I was trying something like classename := pWindow^.lpcs.lpszClass
but no luck... I guess I need a refresher on pointers...

Thank you.
Hmm. "pWindow" is a LPARAM type, that is nothing but a simple integer. So if you write "pWindow^", Delphi doesn't know what to do with that, you see that?
So you'll have to write "classename := PCBTCreateWnd(pWindow)^.lpcs^.lpszClass". Now Delphi knows what to do.

Regards, Madshi.
Hi Madshi, what you gave me compiles and works except that for some components (TToolbar being one of them) it gives me a Access Violation. I am just trying it with a Thread Hook within my program (not a dll) for now and it looks as follows:

function NewWindowHookCallBack(pCode: integer; pMsg: WPARAM; pWindow: LPARAM): LRESULT; stdcall;
begin
  Result:=0;
  if pCode = HCBT_CREATEWND then
  begin
    if (StrComp(PCBTCreateWnd(pWindow)^.lpcs^.lpszClass, '#32770') = 0) then
      Result := 1;
  end;
  CallNextHookEx(whNewWindowThread, pCode, pMsg, pWindow);
end;


Any idea what causes that. My application has several form (5) that all get created. The one with the tollbar causes the problem. When I remove the toolbar it's fine.

Thank you.
I now know that the TComboBox component causes the problem. I created an empty form with the hook - no problem. As soon as I add a combobox, an access violation occurs.
Hmm. That's strange. I've no idea why it happens. Please check whether pWindow is <>nil and whether lpcs is <>nil. According to the documentation, it shouldn't, but who knows...
Perhaps you should use your original solution with GetClassName. Was something wrong with that one?

Regards, Madshi.