繁体   English   中英

如何在 C# 中调用 x64 汇编程序

[英]How to call a x64 Assembly procedure in C#

我正在做一个项目,目前具有以下结构:

  1. C# WPF 项目包含用户界面以及对外部方法的调用。
  2. 包含算法的 C++ DLL 项目。
  3. 包含算法的 ASM DLL 项目。

为简单起见,我们假设该算法不接受任何参数并返回两个预定义数字的总和。

下面是C++(第二个)项目中的函数签名和实现:

int Add(int x, int y)
{
    return x + y;
}

extern "C" __declspec(dllexport) int RunCpp()
{
    int x = 1, y = 2;

    int z = Add(x, y);

    return z;
}

下面是我在 C# 中调用函数的方式:

[DllImport("Algorithm.Cpp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int RunCpp();

这工作得很好 - 在 C# 中调用函数返回值 3,一切正常,没有抛出异常。

但是,我现在很难在 C# 代码中调用 ASM 过程。 我已经看到(并在一定程度上测试了自己)在 C# 代码中直接调用 MASM DLL 是不可能的。 但是,我听说可以在 C++ 中调用 ASM 并在 C# 中调用该函数。

1.我的第一个问题是 - 是否可以直接在 C# 中调用 ASM 代码? 当我尝试这样做时,我得到一个异常,基本上说二进制代码不兼容。
2.我试过用C++间接调用ASM DLL,虽然没有异常,但是返回的值是“随机”的,感觉像是内存中的余数,例如:-7514271。 这是我做错了什么,还是有另一种方法来实现这一目标?

下面是在 C++ 中调用 ASM 的代码:

typedef int(__stdcall* f_MyProc1)(DWORD, DWORD);

extern "C" __declspec(dllexport) int RunAsm()
{
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"Algorithm.Asm.dll");

    if (hGetProcIDDLL == NULL)
    {
        return 0;
    }

    f_MyProc1 MyProc1 = (f_MyProc1)GetProcAddress(hGetProcIDDLL, "MyProc1");

    if (!MyProc1)
    {
        return 0;
    }

    int x = 1, y = 2;

    int z = MyProc1(x, y);

    FreeLibrary(hGetProcIDDLL);

    return z;
}

这里,在C#中调用C++的代码:

[DllImport("Algorithm.Cpp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int RunAsm();

如果需要,这里是MyProc1的 ASM 代码:
主.asm:

MyProc1 proc x: DWORD, y: DWORD

mov EAX, x
mov ECX, y
add EAX, ECX
ret

MyProc1 endp

主文件:

LIBRARY Main
EXPORTS MyProc1

是否可以直接在 C# 中调用 ASM 代码?

这个例子有两个项目,C# 和基于程序集的 DLL。 看起来您已经知道如何让基于 C++ 的 DLL 工作。 项目名称与目录名称相同,xcs 为 C#,xcadll 为 dll

xcadll 属性 | 链接器 | 输入:xcadll.def

xcadll\\xcadll.def:

LIBRARY xcadll
EXPORTS DllMain
EXPORTS Example

xcadll\\xa.asm 属性(对于发布版本,不需要 /Zi):

General | Excluded From Build: No
General | Item Type: Custom Build Tool
Custom Build Tool | General | Command Line: ml64 /c /Zi /Fo$(OutDir)\xa.obj xa.asm
Custom Build Tool | General | Outputs: $(OutDir)\xa.obj

xcadll\\xa.asm:

        includelib      msvcrtd
        includelib      oldnames        ;optional
        .data
        .data?
        .code
        public  DllMain
        public  Example

DllMain proc                            ;return true
        mov     rax, 1
        ret     0
DllMain endp

Example proc                            ;[rcx] = 0123456789abcdefh
        mov     rax, 0123456789abcdefh
        mov     [rcx],rax
        ret     0
Example endp
        end

xcs\\Program.cs:

using System;
using System.Runtime.InteropServices;
namespace xcadll
{
    class Program
    {
    [DllImport("c:\\xcadll\\x64\\release\\xcadll.dll")] 
    static extern void Example(ulong[] data);

        static void Main(string[] args)
        {
            ulong[] data = new ulong[4] {0,0,0,0};
            Console.WriteLine("{0:X16}", data[0]);
            Example(data);
            Console.WriteLine("{0:X16}", data[0]);
            return;
        }
    }
}

对于调试,使用

    [DllImport("c:\\xcadll\\x64\\debug\\xcadll.dll")] 

xcs 属性 | 调试 | 启用本机模式调试(选中该框)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM