DLL部分代码如下
#include "stdafx.h"
#include "stdio.h"#pragma data_seg("mydata")
 char fle[100];//="d:\\r.txt";
HHOOK h_Message=NULL;
FILE *fp=NULL;//记录文件句柄
bool InstallFlag=false;
int intI =NULL;
static HHOOK hkactive=NULL;HINSTANCE dllhwnd=NULL;
#pragma data_seg()
//#pragma comment(linker, "/SECTION:mydata,RWS")
BOOL APIENTRY DllMain( HANDLE hModule, 
  DWORD  ul_reason_for_call, 
  LPVOID lpReserved
  )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break; }
dllhwnd=(HINSTANCE)hModule; //保存DLL句柄
return TRUE;
}LRESULT  CALLBACK  CallWndProc(int  nCode,  WPARAM  wParam,  LPARAM  lParam) 
{
if(nCode>=0)
{ PMSG msg;
msg=(PMSG)lParam;
switch(msg->message)
{
case WM_LBUTTONDOWN:
char x[100];
wsprintf(x,"%d",intI);
MessageBox(NULL,x,NULL,MB_OK);
break;
}
}
return CallNextHookEx(h_Message,nCode,wParam,lParam);
}
extern "C" bool __declspec( dllexport ) _stdcall   InstallHook(LPCTSTR strfle)
{
// MessageBox(NULL,sfile,"",MB_OK);
 if (InstallFlag) //判断是否已经load
 return false;
 InstallFlag=true;
 intI=3;
lstrcpy(fle,strfle);
//MessageBox(NULL,fle,NULL,MB_OK);

h_Message=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)CallWndProc,dllhwnd,0);
int x=::GetLastError();
if (h_Message!=NULL)
//g_hShellHook = SetWindowsHookEx(WH_CBT, ShellDll_MainHook, hins, 0); 
return TRUE;
else
return false;
}extern "C"  bool __declspec( dllexport ) _stdcall   UnInstallHook()
{
bool h_MessageStatus=false;
h_MessageStatus=::UnhookWindowsHookEx(h_Message);
return h_MessageStatus;
}
调用DLL的EXE代码#include "stdafx.h"
#include "resource.h"
#include "aboutdlg.h"
#include "MainDlg.h"
#include "constant.h"
#include "cn.h"
BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
return IsDialogMessage(pMsg);
}BOOL CMainDlg::OnIdle()
{
return FALSE;
}LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow(); // set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE); // register object for message filtering and idle updates
m_TestMail.SubclassWindow(GetDlgItem(IDC_BUTTON1));
m_MailAddress.SubclassWindow(GetDlgItem(IDC_EDIT1));
m_About.SubclassWindow(GetDlgItem(ID_APP_ABOUT));
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this); UIAddChildWindowContainer(m_hWnd); return TRUE;
}LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: Add validation code 
CloseDialog(wID);
return 0;
}LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CloseDialog(wID);
return 0;
}void CMainDlg::CloseDialog(int nVal)
{ UnHook();
DestroyWindow();
::PostQuitMessage(nVal);
}LRESULT CMainDlg::OnBnClickedButton2(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: 在此添加控件通知处理程序代码 typedef  bool (__stdcall CALLBACK *INSTALLHOOK)(LPCTSTR);
char strDllname[MAX_PATH];
GetDllname(strDllname); //获得DLL名字
HMODULE hInst=LoadLibrary(strDllname);
MessageBox(strDllname);
char sz[]="c:\\r.txt";
UINT I=3;
INSTALLHOOK installhook;
if(hInst != NULL) 
{
installhook=(INSTALLHOOK)GetProcAddress(hInst,"InstallHook");
if (!installhook(sz))
{
MessageBox(CN_MSG_INSTALLHOOKERROR);
} }
else
{
MessageBox(CN_MSG_DLLERROR);
}
//FreeLibrary(hInst);    
return 0;
}LRESULT CMainDlg::OnBnClickedButton1(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// TODO: 在此添加控件通知处理程序代码 return 0;
}//----------------------------------------------------------------------------------------------
//Function GetDllname
// 获得DLL名字,DLL名字是程序主名+.DLL
//参数(out):strDllname,成功执行后包含有DLL名字
//----------------------------------------------------------------------------------------------
void CMainDlg::GetDllname(LPSTR strDllname)
{ char fp[MAX_PATH]; int index ;
GetModuleFileName(GetModuleHandle(NULL),fp,MAX_PATH); //获得执行文件的完整名称 CStringA str(fp),str1;
//分解出文件名称
index=str.ReverseFind('\\');
str1=str.Right(str.GetLength()-index-1);
//分解出文件主名称
index=str1.ReverseFind('.');
str=str1.Left(index);
//获得DLL的名称,dll名称和EXE名称主名称相同
str.Insert(str.GetLength(),Dllext);
wsprintf(strDllname,str.GetBuffer(str.GetLength()));}
//----------------------------------------------------------------------------------------------
//Function UnHook
// 停止hook
//----------------------------------------------------------------------------------------------
int CMainDlg::UnHook(void)
{
char strDllname[MAX_PATH];
DWORD rcode=0;
GetDllname(strDllname);
HMODULE hInst=LoadLibrary(strDllname);
typedef  bool (__stdcall CALLBACK *UNINSTALLHOOK)();
    UNINSTALLHOOK uninstallhook;
uninstallhook=(UNINSTALLHOOK)GetProcAddress(hInst,"UnInstallHook");
uninstallhook(); //停止HOOK
rcode=::GetLastError();
FreeLibrary(hInst);
return rcode;
}
我在InstallHook时传递了参数给DLL共享段中的fle,但在CallWndProc过程中,fle却被还原成空了.

解决方案 »

  1.   

    我映象中好像没有初值的不会在共享段。
     char fle[100]={0};
    这样不知道行不行。
      

  2.   

    char fle[100];//="d:\\r.txt";
    你需要把它全部赋值~
    否则编译器会把它放在一个单独的叫未初始化段的段里面~
    char fle[100]={
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
    };
      

  3.   

    上面写少了~
    你写100个NULL吧~
      

  4.   

    you must initial fle!
    else compiler will put it to NO_INIT data_seg,