Solved

Get info from call back function

Posted on 1999-01-13
8
134 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:ymailhot
  • 4
  • 4
8 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1356256
There are several possibilites...
I would suggest simply sending a message. If you need only to transport two integers, you can use the message WM_USER+1, otherwise (if you want to transport more data), you'll have to look at the WM_COPYDATA message.

In your application do something like this:

type Form1 = class(TForm);
  ...
  private
    procedure MessageFromDll(var Message: TMessage); message WM_User+1;
  end;

procedure TForm1.MessageFromDll(var Message: TMessage);
begin
  // Message.wParam=555 / Message.lParam=777
  Message.result:=999;
end;

In the dll use this:

PostMessage(appForm1Handle,WM_User+1,555,777);
// This sends the message and does NOT wait for the result

or

intResult:=SendMessage(appForm1Handle,WM_User+1,555,777);
// This sends the message and returns intResult=999

Everything clear? Or have you more answers about this? Please ask...

Regards, Madshi.
0
 

Author Comment

by:ymailhot
ID: 1356257
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.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1356258
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.
0
 

Author Comment

by:ymailhot
ID: 1356259
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 20

Expert Comment

by:Madshi
ID: 1356260
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.
0
 

Author Comment

by:ymailhot
ID: 1356261
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.
0
 

Author Comment

by:ymailhot
ID: 1356262
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.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1356263
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now