用C#调用外部DLL

1.有时候需要用C#调用外部的dll,例如c++写的dll,首先需要保证dll的编译环境与本项目的环境是相同的,例如都是x86位或者x64位

2.调用声明和dll内的声明一致:

function Test_Add(A,B:Integer):Integer;stdcall;external 'Test.dll';

例如c++写的函数是两个int的参数,调用方式为stdcall,返回为int类型,在C#调用时需要同样声明。

[DllImport("Test.dll", EntryPoint = "Test_Add", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]

public static extern int Add(int a, int b);

 

posted @ 2019-12-27 15:44  zhoushiya  阅读(1537)  评论(0编辑  收藏  举报