BOOL APIENTRY DllMain( HMODULE hModule,
  DWORD  ul_reason_for_call,
  LPVOID lpReserved
  )
{
if(DLL_PROCESS_ATTACH == ul_reason_for_call)
{
::MessageBox(NULL, "11111111111111", "222", 0);
ofstream out("c:\\1.txt", ios::app);
out<<"Dll Insert!"<<endl;
out.close();
DWORD dwThredId = 0;//MessageBox和写入文件是测试
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)InitDllThreadProc, NULL, 0, &dwThredId);
}
return TRUE;
}DLL只是在DLL_PROCESS_ATTACH的时候创建一个线程,在我的机器上工作正常,但是到了朋友机器上就不正常了,都是XP系统,我加入MessageBox和写入文件做测试,在朋友机器上发现没有弹出对话框,也没有向文件写入数据,感觉就是DllMain没有被调用(在我的机器上有弹出对话框,也有数据写入文件),但是我用工具查看进程导入的模块,发现DLL已经注入;

解决方案 »

  1.   

    模块环境初始化失败会出现这重情况, 如全局变量初始化...
    在 _DllMainCRTStartup 加断点, 调试
      

  2.   

    是用LoadLibrary*()显示加载,还是在工程中是用lib,然后隐式的加载这个dll?
      

  3.   

    我使用CreateRemoteThread远程注入这个DLL,用我自己写的代码注入不行,用别的工具注入DLL也不行,奇怪之处就是在本机上没有出现任何问题,而朋友机器上就不行,用工具查看发现DLL模块已经被导入但就是不执行DllMain中的代码;DLL全局变量只是几个DWORD和HANDLE,初始化为0和NULL,这个应该不会失败吧.
      

  4.   

    不要在DLLMAIN 返回TURE以前执行任何界面相关的代码,譬如MessageBox
    还有就是要在DLLMAIN里面创建线程最好先DisableThreadLibraryCalls;
      

  5.   

    不建议在DLLMAIN里面创建线程,最好提供一个输出函数实现你的功能.
      

  6.   

    You must remember that DLLs use DllMain functions to initialize themselves. When your DllMain function executes, other DLLs in the same address space probably haven't executed their DllMain functions yet. This means that they have not initialized, so you should avoid calling functions imported from other DLLs. In addition, you should avoid calls to LoadLibrary(Ex) and FreeLibrary from inside DllMain because these functions can create dependency loops.The Platform SDK documentation states that your DllMain function should perform only simple initialization, such as setting up thread-local storage (discussed in Chapter 21, "Thread-Local Storage"), creating kernel objects, and opening files. You must also avoid calls to User, Shell, ODBC, COM, RPC, and socket functions (or functions that call these functions) because their DLLs might not have initialized yet or the functions might call LoadLibrary(Ex) internally, again creating a dependency loop.Also be aware that the same problems exist if you create global or static C++ objects because the constructor or destructor for these objects is called at the same time as your DllMain function.
      

  7.   

    情况是这样的,那个MessageBox和ofstream只是为了检查DllMain有没有被调用,实际使用会注释掉的;我需要在DLL加载的时候处理一些事情,原来是直接写在DllMain中,但是这样做偶尔会出错,所以我就在DllMain中创建一个线程来执行那部分代码,之后就再也没有报错了;怪就怪在在我的机器上没问题,在朋友机器上就不行,又没办法在朋友机器上调试(不是一个地方的).现在是开发的时候好好的,实际使用就死翘翘了.都是使用的XP系统,并且DLL是加载成功了(我用工具查看被注入进程的导入模块)但是DllMain没有被调用;
      

  8.   

    DllMain 里面最好不要做过多的处理
    因为加栽你的DLL的时候程序可能还没有加栽你在DLLMain中调用函数需要的DLL
    如果DLL被加栽进去的话DLLMAIN是会被调用的
    你用MessageBox 来判断是否加栽是不合适的