C#调用C++DLL-创新互联-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#调用C++DLL-创新互联
1.C++与C#对应类型关系C#调用C++DLL

C/C++

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比万源网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式万源网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖万源地区。费用合理售后完善,十载实体公司更值得信赖。

C#

short

short

int

int

long

int

bool

bool

char(Ascii码字符)

byte

float

float

double

double

short

short

wchar_t *

String/char[]

wchar_t

char

const float *

Float[]

2.C#声明DLL方法

原C++方法:METISAPI double CalcTagSimilarities(const wchar_t * str_src, const wchar_t * str_dst,

      const wchar_t delimiter, const float * weights, const size_t weights_size);

C#声明:

[DllImport("Metis_Maths.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CalcTagSimilarities", CharSet = CharSet.Unicode)]

       public static unsafe extern double CalcTagSimilarities(char* srcStr, char* targetStr, char _delimiter, float* weight, int weightCount);

       [DllImport("Metis_Maths.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CalcTagSimilarities", CharSet = CharSet.Unicode)]

       public static extern double CalcTagSimilarities(string srcStr, string targetStr, char _delimiter, float[] weight, int weightCount);

可选的 DllImportAttribute 属性:

CharSet 指示用在入口点中的字符集,如:CharSet=CharSet.Ansi;

SetLastError 指示方法是否保留 Win32"上一错误",如:SetLastError=true;

ExactSpelling 指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配,如:ExactSpelling=false;

PreserveSig指示方法的签名应当被保留还是被转换,如:PreserveSig=true;

CallingConvention指示入口点的调用约定, 如:CallingConvention=CallingConvention.Winapi;

几个注意点:

1)指定接口入口数据集Chatset为Charset.Unicode,调动的Dll规定只处理unicode编码的字符串,当前环境默认编码是gb2312,需要显示的指定编码格式,否则会出现中文乱码等现象。

  用extern c 来指明导出函数的时候使用C语言方式编译和连接,这样保证函数定义的名字和导出的名字相同,确保程序可以找到正确的入口点。


网站栏目:C#调用C++DLL-创新互联
文章源于:http://scgulin.cn/article/pcheo.html