c# 调用 c++ 的dll

Fork Me On Github
zodream 编程技术 C# 2021年09月

c# 调用 c++ 的dll

c++ 程序的效率比 c# 的效率高很多

第一步,新建“c++动态链接库”项目

第二步,添加方法

c++
          
struct KeyItem
{
    std::uint32_t x, y, z;
};

extern "C" _declspec(dllexport) 
KeyItem FindKey(char* zipFile, char* zipFileName, char* plainFile, char* plainFileName)
{

}
12345678910

第三步,c#调用

复制 dll 到生成目录

c#
            
[StructLayout(LayoutKind.Sequential)]
struct KeyItem
{
    public uint x, y, z;
}

public static class CrackerDLL 
{

    [DllImport("cracker.dll", EntryPoint = "FindKey", CallingConvention = CallingConvention.Cdecl)]
    internal static extern KeyItem FindKey(string zipFile, string zipFileName, string plainFile, string plainFileName);
}
123456789101112

第四步,使用

c#
       

var res = CrackerDLL.FindKey("c.zip", "c.txt", "plain.zip", "plain.txt");

res.x
res.y
res.z
1234567

注意

生成平台必须选择一样的 x64x86,不能使用 Any CPU,否则会报错

点击查看全文
0 310 0