c#调用c语言dll,形参有char*,无法获得char*内容。但是在c++中调用dll无问题。说明dll正确,C#中代码有问题。
直接解决方法:未解决,dll没问题,就是调用后存储在webtoken中读取有问题,求大神告知解决方法:
变相解决方法:修改dll文件,将形参char*作为返回值char*返回。
原先的c函数声明:
#ifndef _DLLMAIN_H
#define _DLLMAIN_H
#ifndef DLL_EXPORT
#define _LIBAPI __declspec(dllexport)
#else
#define _LIBAPI __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
_LIBAPI void getWebToken(const char* appkey, const char* counter, char* webtoken)
#ifdef __cplusplus
}
#endif
#endif /* _DLL_H_ */
原先的调用方式:无法获得webtoken中的字符串。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
usin

本文探讨了在C#中调用C语言DLL时遇到的问题,特别是当形参涉及char*时,无法正确获取内容。尽管DLL在C++中工作正常,但在C#中出现错误。解决方案包括直接修改DLL,将char*作为返回值,以及在C#中将char*替换为IntPtr。总结了几个关键点:const char*转换为string,char*作为参数或返回值时使用IntPtr,以及如何在不能直接使用ref IntPtr时获取char*内容。

6万+

被折叠的 条评论
为什么被折叠?



