第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*

调用方法:

相关推荐
代钦塔拉1 小时前
第一篇:工业级 C++/Qt 项目头文件包含原则:告别循环依赖与编译玄学
开发语言·c++·qt
The Shio1 小时前
OptiByte 操练场:面向 IoT/嵌入式的协议可视化调试工具
网络·嵌入式硬件·物联网·c#·.net·业界资讯·iot
谷雨不太卷1 小时前
Linux基础IO
java·开发语言
神仙别闹2 小时前
基于PHP+MySQL实现在线考试系统
开发语言·mysql·php
fanzhonghong2 小时前
javaWeb开发之Maven高级
java·开发语言·spring boot·spring cloud·私服
luck_bor2 小时前
Lambda表达式 算法异常
java·开发语言
lsx2024062 小时前
SOAP Envelope 元素
开发语言
范范@2 小时前
day2-python基础语法
开发语言·python
qq_2518364572 小时前
基于java 私厨美食共享平台系统设计与实现(有源码)
java·开发语言·美食
杜子不疼.2 小时前
【C++ 在线五子棋对战】 - 工具类模块实现
开发语言·c++