Solved

Get info from call back function

Posted on 1999-01-13
8
137 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

831 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