dll的代碼
#pragma option -zRSHSEG
#pragma option -zTSHCLASS
#include <vcl.h>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Messages.hpp>
#include <windows.h>
#include <DBGrids.hpp>
#include <DB.hpp>
#pragma hdrstop
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
extern "C" __declspec(dllexport) __stdcall BOOL InstallHook()(HWND AHandle,int AParam,HINSTANCE g_hinstDll);
HWND ghWnd=NULL;
HHOOK rHook;
HWND rHandle;
int count=0;
LPDWORD fegdds;
String ControlAtomString;
TAtom ControlAtom;
DWORD RM_GetObjectInstance;
TWinControl * FindControl1(HWND Handle);
//-------------------------------------------------------------
TWinControl * FindControl1(HWND Handle)
{
DWORD OwningProcess;
if (Handle!=0&& GetWindowThreadProcessId(Handle,&OwningProcess)!=0&& OwningProcess == GetCurrentProcessId() )
{
if (GlobalFindAtom(ControlAtomString.c_str()) == ControlAtom)
{
return (TWinControl *) GetProp(Handle, MakeIntAtom(ControlAtom));
}
else
{
return (TWinControl *)SendMessage(Handle, RM_GetObjectInstance, 0, 0);
}
}
return NULL;
}
//--------------------------------------------
LRESULT CALLBACK Hook(int code, WPARAM wParam, LPARAM lParam)
{
if (wParam==WM_LBUTTONUP&&count==0)
{
AnsiString ss="掛鉤";
HWND hEdit = FindWindowEx(ghWnd, NULL, "TEdit", NULL);
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)(ss.c_str()));
TWinControl* vWinControl;
int i,j;
AnsiString S,tmpGridStr;
TDataSet* DataSet;
if(ControlAtomString.IsEmpty())
{
//ControlAtomString = Format("ControlOfs%.8X%.8X", ARRAYOFCONST(((WORD)GetWindowLong(VSnifferInfo->rHandle, GWL_HINSTANCE) , (WORD)GetWindowThreadProcessId(VSnifferInfo->rHandle,OUT fegdds))));
ControlAtomString = Format("ControlOfs%.8X%.8X", ARRAYOFCONST(((WORD)GetWindowLong(rHandle, GWL_HINSTANCE) , (WORD)GetWindowThreadProcessId(rHandle,OUT fegdds))));
//ControlAtomString = Format("ControlOfs%.8X%.8X", ARRAYOFCONST(((WORD)GetModuleHandle(NULL) , (WORD)GetCurrentThreadId)));
ControlAtom = GlobalAddAtom(ControlAtomString.c_str());
RM_GetObjectInstance = RegisterWindowMessage(ControlAtomString.c_str());
}
vWinControl=FindControl1(rHandle);
if(vWinControl)
{
for(i=0;i<vWinControl->ComponentCount-1;i++)
{
if(AnsiString(vWinControl->Components[i]->ClassName()).AnsiCompare("TDBGrid")==0)
{
tmpGridStr="";
DataSet=TDBGrid(vWinControl->Components[i]).DataSource->DataSet;
tmpGridStr=DataSet->Fields->FieldByNumber(0)->AsString+DataSet->Fields->FieldByNumber(1)->AsString;
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)(tmpGridStr.c_str()));
}
else
{
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"找不到dbgrid");
}
}
}
else
{
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)"找不到表單");
}
count++;
}
return (CallNextHookEx(rHook, code, wParam, lParam));
}
//------------------------------------------
extern "C" __declspec(dllexport) __stdcall
void SetWindowHandle(HWND ahWnd)
{
ghWnd = ahWnd;
}
//-----------------------------------------------
extern "C" __declspec(dllexport) __stdcall BOOL InstallHook()(HWND AHandle,HINSTANCE g_hinstDll)
{
BOOL Result=false;
rHandle=AHandle;
rHook=SetWindowsHookEx(WH_MOUSE, (HOOKPROC)Hook, g_hinstDll, GetWindowThreadProcessId(AHandle,OUT fegdds));
return Result;
}
//--------------------------------------------
extern "C" __declspec(dllexport) __stdcall
BOOL UninstallHook()
{
return(UnhookWindowsHookEx(rHook));
}
//--------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
主程式的代碼
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "OutputChineseData.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
typedef void __stdcall (*sethwnd)(HWND ahWnd);
typedef BOOL __stdcall (*inhook)(HWND AHandle,HINSTANCE g_hinstDll);
typedef BOOL __stdcall (*unhook)();
HANDLE hDllHandle=LoadLibrary("GDBGD.dll");
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text="";
HWND hWinTtest = FindWindow("TForm1", "DBGridForm1");
if(hWinTtest)
{
sethwnd shwnd=(sethwnd)GetProcAddress(hDllHandle,"SetWindowHandle");
HWND hWnd=this->Handle ;
shwnd(hWnd);
inhook ish=(inhook)GetProcAddress(hDllHandle,"InstallHook");
ish(hWinTtest,hDllHandle);
}
else
{
Edit1->Text="無";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
unhook uh=(unhook)GetProcAddress(hDllHandle,"UninstallHook");
uh();
}
//---------------------------------------------------------------------------
問題是掛上鉤子后,運行到vWinControl=FindControl1(rHandle);獲取的表單是空的,所以提示找不到表單,應該是函式FindControl1出問題了,后來檢查由于函式中的 OwningProcess == GetCurrentProcessId()的兩個值是不一樣的,所以回傳的是false,導致得不到表單的實體,這個函式也是看網上有delphi的版本自己轉的,其實沒從根本上理解,實在搞不懂是為什么了,請大家幫忙,謝謝!!
沒分了只有31分了,希望能有人幫忙
uj5u.com熱心網友回復:
怎么沒人啊?uj5u.com熱心網友回復:
好像最好用同步。uj5u.com熱心網友回復:
哥啊,能詳細一點嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107084.html
標籤:基礎類
上一篇:這道題怎么做啊
