Solved

Get info from call back function

Posted on 1999-01-13
8
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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