第9篇c#调用c++动态库报错处理

1.报错System.DllNotFoundException

编译c++库如果是x64,c#项目也选x64

c++

c#项目

2.报错System.BadImageFormatException

c#项目属性设置:目标平台x64

编译后,bin目录生成x64文件夹

拷贝c++库到此目录,c++如果依赖其它库也一并拷贝

3.System.AccessViolationException

c#传参数:string给c++函数,或接收返回字符串时,字符串需转换

c++的函数

std::string转char*

td::string str_data = "abcdefg";

char* charArr = new char[str_data.length() + 1];

strcpy(charArr, str_data.c_str());

char*转std:string

char* aa ="abdefg";

std::string str = aa;

char*可直接接收c# string

复制代码
char* __stdcall ImgRotate(char* charstr, int degree)
    {
        std::string imgPath = charstr;
       //const char* chardata = c_Obj.ImgRotate(imgPath, degree).c_str();
        std::string str_data = c_Obj.ImgRotate(imgPath, degree);
        char* charArr = new char[str_data.length() + 1];
        strcpy(charArr, str_data.c_str());
      //  strcpy_s(charArr, sizeof(charArr), str_data.c_str());

        return charArr;
        
    }

c#调用

复制代码
[DllImport("CdtxwVisionLibNet.dll")]
        private static extern int Add(int n1, int n2);
        [DllImport("CdtxwVisionLibNet.dll",EntryPoint = "ImgRotate",CharSet=CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr ImgRotate(string path,int degree);

c#IntPtr对应c++的char*

调用方法:

相关推荐
一百天成为python专家6 小时前
python爬虫之selenium库进阶(小白五分钟从入门到精通)
开发语言·数据库·pytorch·爬虫·python·深度学习·selenium
序属秋秋秋6 小时前
《C++进阶之STL》【红黑树】
开发语言·数据结构·c++·笔记·学习·stl
小白学大数据7 小时前
模拟登录与Cookie持久化:爬取中国汽车网用户专属榜单数据
开发语言·爬虫·python
hqxstudying7 小时前
Java类加载机制
java·开发语言
nece00111 小时前
PHP单独使用phinx使用数据库迁移
开发语言·php·数据库迁移·phinx
pusue_the_sun12 小时前
C语言强化训练(1)
c语言·开发语言·算法
Kookoos14 小时前
ABP + ClickHouse 实时 OLAP:物化视图与写入聚合
clickhouse·c#·linq·abp vnext·实时olap
mmz120715 小时前
动态规划2(c++)
开发语言·c++
接着奏乐接着舞。16 小时前
前端RSA加密遇到Java后端解密失败的问题解决
java·开发语言·前端