Skip to content

执行汇编指令 - AsmCall

函数简介

在指定进程中执行汇编指令,支持多种执行模式。

接口名称

AsmCall

DLL调用

long AsmCall(long instance, long hwnd, string asmStr, int type, long baseAddr);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
hwnd长整数型窗口句柄或进程ID,根据type参数决定用途。
asmStr字符串汇编语言字符串,大小写均可,如 "mov eax,1"支持多行,每行一条指令,用换行符 \n\r\n 分隔;也支持直接输入机器码。
type整数型执行类型:
0: 在本进程中执行(创建线程),hwnd无效
1: 在hwnd指定进程内执行(创建远程线程)
2: 在已注入绑定的目标进程创建线程执行(需排队)
3: 同模式2,但在hwnd所在线程直接执行
4: 同模式0,但在当前线程直接执行
5: 在hwnd指定进程内执行(APC注入)
6: 直接在hwnd所在线程执行。
baseAddr长整数型汇编指令所在的地址,如果为0则自动分配内存。

示例

以下示例在本进程创建线程执行(type=0),多行汇编计算 10 + 20,返回值 30 位于 EAX(32 位)或 RAX(64 位);64 位进程请将示例中的 eax 改为 rax

传入 asmStr 的字符串在逻辑上等价于下方汇编文本(一行一条指令):

asm
mov eax, 10
add eax, 20
ret

各语言写法分两类:直接写多行字符串(C++ / C# / Python / Java / Aardio),或 逐行拼接 + 换行符(TC / 按键精灵 / 易语言 / 火山)。

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
const char* asmCode = R"(
mov eax, 10
add eax, 20
ret
)";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = ola.AsmCall(hwnd, asmCode, 0, 0);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
var asmCode = @"
mov eax, 10
add eax, 20
ret";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = ola.AsmCall(hwnd, asmCode, 0, 0);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
asm_code = """
mov eax, 10
add eax, 20
ret
"""  # 多行汇编,换行即指令分隔
# result 为 30(EAX/RAX)
result = ola.AsmCall(hwnd, asm_code, 0, 0)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String asmCode = """
    mov eax, 10
    add eax, 20
    ret
    """;  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = ola.AsmCall(hwnd, asmCode, 0, 0);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
asmCode := R"(
mov eax, 10
add eax, 20
ret
)";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
result := ola.AsmCall(hwnd, asmCode, 0, 0)
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let asmCode = R"(;
mov eax, 10
add eax, 20
ret
)";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
let result = ola.asm_call(&hwnd, &asmCode, 0, 0);
cpp
var ola = com("OlaPlug.OlaSoft")
var asmCode = "mov eax, 10" + "\n"
            + "add eax, 20" + "\n"
            + "ret"  // 每行一条指令,"\n" 为换行分隔
// result 为 30(EAX/RAX)
var result = ola.AsmCall(hwnd, asmCode, 0, 0)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
asmCode = "mov eax, 10" & vbCrLf _
        & "add eax, 20" & vbCrLf _
        & "ret"  ' 每行一条指令,vbCrLf 为换行分隔
' result 为 30(EAX/RAX)
result = ola.AsmCall(hwnd, asmCode, 0, 0)
text
.局部变量 ola, OLAPlug
.局部变量 asm, 文本型
.局部变量 result, 长整数型
ola.创建 ()
asm = “mov eax, 10” + #换行符
    + “add eax, 20” + #换行符
    + “ret”  ' 每行一条指令,#换行符 为分隔
' result 为 30(EAX/RAX)
result = ola.AsmCall(hwnd, asm, 0, 0)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var asmCode = `
mov eax, 10
add eax, 20
ret
`;  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
var result = ola.AsmCall(hwnd, asmCode, 0, 0);
text
变量 ola <类型 = OLAPlugServer>
变量 asmCode <类型 = 文本型>
ola = 新建 OLAPlugServer
asmCode = "mov eax, 10" + "\n"
        + "add eax, 20" + "\n"
        + "ret"  ' 每行一条指令,"\n" 为换行分隔
' result 为 30(EAX/RAX)
长整数 result = ola.AsmCall(hwnd, asmCode, 0, 0)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
const char* asmCode = R"(
mov eax, 10
add eax, 20
ret
)";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = ola.AsmCall(hwnd, asmCode, 0, 0);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
const char* asmCode = R"(
mov eax, 10
add eax, 20
ret
)";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = AsmCall(instance, hwnd, asmCode, 0, 0);
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long AsmCall(long ola, long hwnd, string asmStr, int type, long baseAddr);

long instance = CreateCOLAPlugInterFace();
var asmCode = @"
mov eax, 10
add eax, 20
ret";  // 多行汇编,换行即指令分隔
// result 为 30(EAX/RAX)
long result = AsmCall(instance, hwnd, asmCode, 0, 0);
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
asm_code = """
mov eax, 10
add eax, 20
ret
"""  # 多行汇编,换行即指令分隔
# result 为 30(EAX/RAX)
result = ola.AsmCall(instance, hwnd, asm_code, 0, 0)

返回值

返回值说明
≥ 0(含 0成功:32 位进程为 EAX,64 位进程为 RAX。返回 0GetLastError()0 表示执行成功且寄存器值为 0。
负数失败:绝对值为错误码(50xx),与 GetLastError() 一致。

完整错误码表与排查建议见 AsmCall 错误码说明

错误处理示例

cpp
long ret = ola.AsmCall(hwnd, asmCode, 0, 0);
if (ret < 0) {
    int err = ola.GetLastError();  // 例如 5005
    // 可选:ola.GetLastErrorString() 获取中文说明
} else {
    // ret 为 EAX/RAX
}

注意事项

项目说明
多行汇编格式多条指令用换行符 \n\r\n 分隔,每行一条指令;以线程方式执行时末尾通常需要 ret
错误的汇编指令可能导致程序崩溃错误的汇编指令可能导致程序崩溃,建议在测试环境中先验证。
不同执行模式适用于不同场景不同执行模式适用于不同场景,请根据需求选择合适的 type 参数。
权限需要 Asm 功能权限;在目标进程中执行需要相应进程权限。
type=2/3依赖 BindWindow 注入绑定;失败常见错误码 5020/5021
APC 注入模式使用 APC 注入模式(type=5)需要开启 memory 防护盾。
错误诊断失败时调用 GetLastErrorGetLastErrorString
返回值的解释取决于汇编指令的具体内容成功时返回值的含义取决于汇编指令对 EAX/RAX 的写入。