资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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://cdkjz.cn/article/pcheo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220