调用windows API内联程序集时出现问题

Problem with call windows API inline assembly

本文关键字:问题 程序集 windows API 调用      更新时间:2023-10-16

我想反调试和编写一个函数,像下面的代码调用API调试"IsDebuggerPresent"检查:

#include "windows.h"
bool checkdbg(){
    int i = 1;
    __asm{
        call IsDebuggerPresent //gọi api debug
        test eax, eax
        jne L1
        mov i,0
        L1 : 
    }
    if(i == 0) return false;
    else return true;
}

但是当编译时,VS2010找不到IsDebuggerPresent调用。

#include "Windows.h"
bool checkdbg() {
    __asm {
        call IsDebuggerPresent //gọi api debug
        test eax, eax
        jne L1
        mov i,0
        L1 : 
    }
    if(i == 0) return false;
    else return true;
}
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
  checkdbg();
  return ERROR_SUCCESS;
}

可以很好地编译。它可能是在抱怨,因为你错过了一个入口。您在项目选项中提供的子系统规定了需要什么main,无论是WinMain(用于Windows子系统)还是main(用于控制台子系统)。

我还建议使用:

#include <Windows.h>

相对于:

#include "Windows.h"

请看这里的解释。你还应该这样做:

#include <Windows.h>
BOOL checkdbg() {
    int i = 1;
    __asm{
        call IsDebuggerPresent
        ret
    }
}
int main() {
  checkdbg();
  return ERROR_SUCCESS;
}

返回值在eax中是预期的,所以你也可以返回,否则你所做的就是检查返回值是否为真,然后返回真。如果为false,则返回false。就回来了。你所做的唯一一件事就是有效地从BOOL转换为BOOL。我不明白你为什么要用内联ASM。

为什么不直接从C调用API呢?

#include <windows.h>
#pragma comment(lib, "kernel32.lib")
bool checkdbg() {
    return IsDebuggerPresent();
}

不要使用

call balabala
使用

call dword ptr [balabala]