IsDebuggerPresent VC6.0编译时会报连接错误

来源:互联网 发布:java web 分层 编辑:程序博客网 时间:2024/05/29 09:04
IsDebuggerPresent函数可以用来检测本进程是否处于被调试状态,当然,这种方法的实用性不大。
此函数在winbase.h中声明如下:WINBASEAPI BOOL WINAPI IsDebuggerPresent(void);

如果本进程当前正在被调试则返回1,否则返回0。

直接调用此函数的源程序在用VC6.0编译时会报连接错误,原因是kernel32.lib中找不到_IsDebuggerPresent这个符号为了使用此函数,你不得不使用LoadLibrary动态加载kernel32.dll,或者使用GetModuleHandle获取kernel32.dll的映像地址,然后使用GetProcAddress取得IsDebuggerPresent的地址。

代码如下:

BOOL (*IsDebuggerPresent)();

HMODULE hModule=GetModuleHandle("kernel32.dll");
IsDebuggerPresent = (BOOL(*)())GetProcAddress(hModule, "IsDebuggerPresent");


反汇编kernel32.dll观察IsDebuggerPresent的代码可以看到:

7C813093  64:A1 18000000   mov     eax, dword ptr fs:[18]  |kernel32.IsDebuggerPresent
7C813099  8B40 30          mov     eax, dword ptr [eax+30]
7C81309C  0FB640 02        movzx   eax, byte ptr [eax+2]
7C8130A0  C3               ret