Intercept message problems in the EDIT control (I found some information after the last question. Although the message is intercepted, I can get blank characters)

Source: Internet
Author: User
Intercept message problems in the EDIT control (I found some information when I asked a question last time. Although the message is intercepted, I can get a blank character) Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiAPI/html/delphi_20061117131358209.html
Next, let me briefly talk about the problem.
The Unit is a program with an Edit control.
This control is used to display the message returned by the command.
My goal is to intercept messages in the control, mainly to get the characters displayed in the control
The message is EM_RSPLACESEL.
Next I will show the Hook program I wrote based on others' codes, and the main program I wrote.

Hook. dll file 1

Library hook;

{Important note about DLL memory management: ShareMem must be
First unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
Functions that pass strings as parameters or function results. This
Applies to all strings passed to and from your DLL -- even those that
Are nested in records and classes. ShareMem is the interface unit
The BORLNDMM. DLL shared memory manager, which must be deployed along
With your DLL. To avoid using BORLNDMM. DLL, pass string information
Using PChar or parameter string parameters .}

Uses
SysUtils,
Classes,
WdSpyCommon in 'wdspycommon. pa ',
WdMsgSpy in 'wdmsgspy. pa ';

{$ R *. res}

Exports
StartSpyMessage,
StopSpyMessage;

Begin
End.

Hook. dll file 2

Unit wdSpyCommon;
{*************************************** ****
* Brief: Declaration file for data structures used by message Spy
* Autor: linzhenqun
* Date: 2005-9-25
* Email: linzhengqun@163.com
* Blog: http://blog.csdn.net/linzhengqun
**************************************** ****}
Interface
Uses
Windows, Messages;
Resourcestring
Err_ProcInvalid = 'The Message spy procedure are invalid .';
Err_ShareMem = 'could not create share memory .';
Const
Msg_Null_Value = 0;
Msg_Type_Sent = 1;
Msg_Type_Post = 2;
Msg_Type_Return = 3;
Type
{Monitored message structure}
PMsgInfo = ^ TMsgInfo;
TMsgInfo = packed record
Hwnd: HWND;
Message: UINT;
WParam: WPARAM;
LParam: LPARAM;
Time: DWORD;
Pt: TPoint;
LResult: LRESULT;
MsgType: UINT;
End;
Implementation
End.

Hook. dll file three hooks are set here

Unit wdMsgSpy;
{*************************************** ****
* Brief: Message Spy SDK
* Autor: linzhenqun
* Date: 2005-9-24
* Email: linzhengqun@163.com
* Blog: http://blog.csdn.net/linzhengqun
**************************************** ****}
Interface
Uses
Messages, Windows, Classes, SysUtils, wdSpyCommon;
Const
MApingFile_Name = 'msgspy _ FC819A73-2718-47E2-BF78-6810562CDA65 ';
Type
// Shared memory
PShareMem = ^ TShareMem;
TShareMem = record
HRevWnd: THandle; // window handle for storing received messages
HWndSpy: THandle; // monitored window handle
End;
{Start monitoring message, AHSpyWnd is the monitored window, And AHRevWnd is the window for receiving the monitored message}
Function StartSpyMessage (AHSpyWnd, AHRevWnd: THandle): Boolean; stdcall;
{Stop monitoring message}
Procedure StopSpyMessage; stdcall;
Var
HMsgProc, HWndProc, HWndRetProc: THandle; // handle of the corresponding hook
PSMem: PShareMem; // shared memory block
HMApFile: THandle; // handle of the memory ing file.
Implementation

// Send the intercepted message structure to the target window
Procedure SendData (AMsgInfo: PMsgInfo); stdcall;
Var
Pcds: PCopyDataStruct;
Begin
New (pcds );
Pcds ^. cbData: = SizeOf (TMsgInfo );
Pcds ^. lpData: = AMsgInfo;
SendMessage (PSMem ^. HRevWnd, WM_COPYDATA, 0, LongInt (pcds ));
Dispose (pcds );
End;
{WH_GETMESSAGE hook process}
Function GetMsgProc (code: Integer; wP: WPARAM; lP: LPARAM): LRESULT; stdcall;
Var
LMsgInfo: PMsgInfo;
Begin
{Only the window handle of the intercepted message is the same as the monitored window handle,
Next, perform the next operation when the monitored window is not the Receiving Window; or when the monitored window
That is, when receiving a message window, the intercepted message is not WM_CopyData. Execute the next step.
This is done to avoid entering the endless cycle of sending and intercepting messages}
If code = HC_ACTION then
If (PMsg (lp) ^. hwnd = PSMem ^. HWndSpy) then
If (PSMem ^. HWndSpy = PSMem ^. HRevWnd) and (PMsg (lp) ^. message <> WM_COPYDATA ))
Or (PSMem ^. HWndSpy <> PSMem ^. HRevWnd) then
Begin
New (LMsgInfo );
LMsgInfo. hwnd: = PMsg (lp) ^. hwnd;
LMsgInfo. message: = PMsg (lp) ^. message;
LMsgInfo. wParam: = PMsg (lp) ^. wParam;
LMsgInfo. lParam: = PMsg (lp) ^. lParam;
LMsgInfo.pt: = PMsg (lp) ^. pt;
LMsgInfo. time: = PMsg (lp) ^. time;
LMsgInfo. lResult: = Msg_Null_Value;
LMsgInfo. MsgType: = Msg_Type_Post;
SendData (LMsgInfo );
Dispose (LMsgInfo );
End;
Result: = CallNextHookEx (HMsgProc, code, wP, lP );
End;

{WH_CALLWNDPROC hook process}
Function CallWndProc (code: Integer; wP: WPARAM; lP: LPARAM): LRESULT; stdcall;
Var
LMsgInfo: PMsgInfo;
Begin
If code = HC_ACTION then
If (PCWPStruct (lp) ^. hwnd = PSMem ^. HWndSpy) then
If (PSMem ^. HWndSpy = PSMem ^. HRevWnd) and (PCWPStruct (lp) ^. message <> WM_COPYDATA ))
Or (PSMem ^. HWndSpy <> PSMem ^. HRevWnd) then
Begin
New (LMsgInfo );
LMsgInfo ^. hwnd: = PCWPStruct (lp) ^. hwnd;
LMsgInfo ^. message: = PCWPStruct (lp) ^. message;
LMsgInfo ^. wParam: = PCWPStruct (lp) ^. wParam;
LMsgInfo ^. lParam: = PCWPStruct (lp) ^. lParam;
LMsgInfo ^. pt: = Point (0, 0 );
LMsgInfo ^. time: = Msg_Null_Value;
LMsgInfo ^. lResult: = Msg_Null_Value;
LMsgInfo ^. MsgType: = Msg_Type_Sent;
SendData (LMsgInfo );
Dispose (LMsgInfo );
End;
Result: = CallNextHookEx (HWndProc, code, wP, lP );
End;

{WH_CALLWNDPROCRET hook process}
Function CallWndRetProc (code: Integer; wP: WPARAM; lP: LPARAM): LRESULT; stdcall;
Var
LMsgInfo: PMsgInfo;
Begin
If code = HC_ACTION then
If (PCWPRetStruct (lp) ^. hwnd = PSMem ^. HWndSpy) then
If (PSMem ^. HWndSpy = PSMem ^. HRevWnd) and (PCWPRetStruct (lp) ^. message <> WM_COPYDATA ))
Or (PSMem ^. HWndSpy <> PSMem ^. HRevWnd) then
Begin
New (LMsgInfo );
LMsgInfo ^. hwnd: = PCWPRetStruct (lp) ^. hwnd;
LMsgInfo ^. message: = PCWPRetStruct (lp) ^. message;
LMsgInfo ^. wParam: = PCWPRetStruct (lp) ^. wParam;
LMsgInfo ^. lParam: = PCWPRetStruct (lp) ^. lParam;
LMsgInfo ^. pt: = Point (0, 0 );
LMsgInfo ^. time: = Msg_Null_Value;
LMsgInfo ^. lResult: = PCWPRetStruct (lp) ^. lResult;
LMsgInfo ^. MsgType: = Msg_Type_Return;
SendData (LMsgInfo );
Dispose (LMsgInfo );
End;
Result: = CallNextHookEx (HWndRetProc, code, wP, lP );
End;

Function StartSpyMessage (AHSpyWnd, AHRevWnd: THandle): Boolean;
Begin
Result: = False;
Try
If (HMsgProc <> 0) or (HWndProc <> 0) or (HWndRetProc <> 0) then
Exit;
PSMem ^. HWndSpy: = AHSpyWnd;
PSMem ^. HRevWnd: = AHRevWnd;
HMsgProc: = SetWindowsHookEx (WH_GETMESSAGE, @ GetMsgProc, HInstance, 0 );
HWndProc: = SetWindowsHookEx (WH_CALLWNDPROC, @ CallWndProc, HInstance, 0 );
HWndRetProc: = SetWindowsHookEx (WH_CALLWNDPROCRET, @ CallWndRetProc, HInstance, 0 );
If (HMsgProc = 0) or (HWndProc = 0) or (HWndRetProc = 0) then
Begin
StopSpyMessage;
Exit;
End;
Except
Exception. Create (err_ProcInvalid );
End;
Result: = True;
End;

Procedure StopSpyMessage;
Begin
UnhookWindowsHookEx (HMsgProc );
UnhookWindowsHookEx (HWndProc );
UnhookWindowsHookEx (HWndRetProc );
HMsgProc: = 0;
HWndProc: = 0;
HWndRetProc: = 0;
End;

Initialization
// Create a shared memory block
HMApFile: = OpenFileMApping (FILE_MAP_ALL_ACCESS, False, MApingFile_Name );
If hMApFile = 0 then
HMApFile: = CreateFileMApping ($ FFFFFFFF, nil, PAGE_READWRITE, 0,
SizeOf (TShareMem), MApingFile_Name );
PSMem: = MApViewOfFile (hMApFile, FILE_MAP_WRITE or FILE_MAP_READ, 0, 0 );
If PSMem = nil then
Begin
CloseHandle (hMApFile );
Exception. Create (err_ShareMem );
End;

Finalization
// Release shared memory blocks
UnMApViewOfFile (PSMem );
CloseHandle (hMApFile );
End.

Friendship points ;;;;;;;;

The problem is solved. You can modify the Hook. dll code.

Because of Interception Problems, I modified it in the dll code. After intercepting the message, I directly used
SendMessage: send the message to my program.

Change

{WH_CALLWNDPROCRET hook process}
Function CallWndRetProc (code: Integer; wP: WPARAM; lP: LPARAM): LRESULT; stdcall;
Var
LMsgInfo: PMsgInfo;
Begin
// If code = HC_ACTION then.
If (PCWPRetStruct (lp) ^. hwnd = PSMem ^. HWndSpy) then
If (PSMem ^. HWndSpy = PSMem ^. HRevWnd) and (PCWPRetStruct (lp) ^. message <> WM_COPYDATA ))
Or (PSMem ^. HWndSpy <> PSMem ^. HRevWnd) then
Begin
New (LMsgInfo );
LMsgInfo ^. hwnd: = PCWPRetStruct (lp) ^. hwnd;
LMsgInfo ^. message: = PCWPRetStruct (lp) ^. message;
LMsgInfo ^. wParam: = PCWPRetStruct (lp) ^. wParam;
LMsgInfo ^. lParam: = PCWPRetStruct (lp) ^. lParam;
LMsgInfo ^. pt: = Point (0, 0 );
LMsgInfo ^. time: = Msg_Null_Value;
LMsgInfo ^. lResult: = PCWPRetStruct (lp) ^. lResult;
LMsgInfo ^. MsgType: = Msg_Type_Return;

// SendData (LMsgInfo); do not use this line
SendMessage (PSMem ^. HRevWnd, PCWPRetStruct (lp) ^. message, PCWPRetStruct (lp) ^. wParam, PCWPRetStruct (lp) ^. lParam) // added to the modification
Dispose (LMsgInfo );
End;
Result: = CallNextHookEx (HWndRetProc, code, wP, lP );
End;

Then, remove the other two hook functions, including installing the hook and leaving only the third hook,
Now, after I enter the command in that unit program, the returned message includes characters
All of them are shown in my program. Although I didn't understand the purpose of these parameters, it solves a problem.

In my program, I have to use the message mechanism to intercept messages of my processes and process messages,
This is the only way to do this, even though it is complex.

Well, I can take it by myself. I will ask you some questions later.

No, just give it a try ~~

Pass.

Passing ~~~

The landlord should not be so stingy

Well, I can take it by myself. I will ask you some questions later.
----------------------------------------------
Lower his sexual desire !!!!!!!!!!!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.