社区
C#
帖子详情
c#调用c++dll时const char*类型应该怎么对应
devin---
2009-11-16 12:13:07
这个是c++写的函数const char* DownLoadFile(char* a, char* b)
如果用c#调用DllImport应该怎么写?主要是类型要正确,
我之前写的运行期一直报错,方法如下
[DllImport("assemble.dll")]
public static extern StringBuilder DownLoadFile(string a,string b);
请求大家的帮助,谢谢
...全文
1650
6
打赏
收藏
c#调用c++dll时const char*类型应该怎么对应
这个是c++写的函数const char* DownLoadFile(char* a, char* b) 如果用c#调用DllImport应该怎么写?主要是类型要正确, 我之前写的运行期一直报错,方法如下 [DllImport("assemble.dll")] public static extern StringBuilder DownLoadFile(string a,string b); 请求大家的帮助,谢谢
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
devin---
2009-11-16
打赏
举报
回复
报的错误为:尝试读取或写入受保护的内存。这通常指示其他内存已损坏
lzsh0622
2009-11-16
打赏
举报
回复
C++ C#
=====================================
WORD ushort
DWORD uint
UCHAR int/byte 大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte
UCHAR* string/IntPtr
unsigned char* [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr)
char* string
LPCTSTR string
LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string
long int
ulong uint
Handle IntPtr
HWND IntPtr
void* IntPtr
int int
int* ref int
*int IntPtr
unsigned int uint
COLORREF uint
lzsh0622
2009-11-16
打赏
举报
回复
string
qldsrx
2009-11-16
打赏
举报
回复
报什么错,是不是对应的文件没有找到,或者下载文件的存放路径未找到?
gomoku
2009-11-16
打赏
举报
回复
[Quote=引用楼主 dubin1003 的回复:]
char* DownLoadFile(char* a, char* b) const;
[/Quote]
这是个不好的导出函数的例子。返回的字符串不清楚是如何释放的,而且声明const后似乎调用方也不能释放。
如果不关心内存泄露,那你可以用:
[DllImport("assemble.dll")]
public static extern IntPtr DownLoadFile(string a,string b);
然后用Marshal.PtrToStringAnsi(IntPtr result)来得到返回字符串。
soaringbird
2009-11-16
打赏
举报
回复
返回值用byte*吧
c#
调用
c语言
dll
,形参有
char
*
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; using System.W
c#
调用
c++
dll
const
char
* String
类型
转换问题。传值,与接收返回值问题
C++
原型 extern "C" __declspec(
dll
export)
const
char
* GetUnicoide(
const
char
* gb2312) { int len = MultiByteToWide
Char
(CP_ACP, 0, gb2312, -1, NULL, 0); w
char
_t* wstr = new w
char
_t[len+1]; m...
C#
调用
C++
非托管
DLL
,
C++
中的
const
char
*
对应
C#
中的IntPtr
类型
。
C#
调用
C++
非托管
DLL
,
C++
中的
const
char
*
对应
C#
中的IntPtr
类型
。
C#
中
调用
C++
动态库,常用参数
类型
对应
关系
由于
C++
和
C#
类型
定义的不同,所以,
C#
中使用
C++
里的函数要将其
类型
对应
的转换过来。下面列出了常用的转换
类型
c#
调用
C++
的动态链接库
dll
,形参相关说明[
const
char
*/
char
*/int]
在
C#
项目中
调用
C++
生成的
dll
相关参数定义
调用
C++
声明的如下 形参有
char
*、
const
char
*、int 返回
类型
有
char
*、int extern "C" { #endif #if defined(_WIN32) # define __export __declspec(
dll
export) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && _
C#
111,129
社区成员
642,533
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章