C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)
// CDLLDemo.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "string.h"
#include <stdio.h>
#include <time.h> extern "C" __declspec(dllexport)
int ParseBaliseMsg2(const unsigned char *pMsgData, char *resTgm, char *resStr)
{
/*CString strInfo;
strcpy(resTgm, strMsg.GetBuffer());
strMsg = strFor1 + strMsg + strFor2;
strInfo += "erro!!!";
strcpy(resStr, strInfo.GetBuffer());*/
//memset(resStr, 0, 50);
printf("%s \r\n", pMsgData);
char *a = "ParseBaliseMsg2 hello word!";
strcpy(resStr, a);
printf("resStr is: %s \r\n", resStr); time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("The current date/time is: %s \r\n", asctime(timeinfo)); return ;
} extern "C" __declspec(dllexport)
char * ParseBaliseMsg3(const unsigned char *pMsgData, char *resTgm, int & retInt)
{
/*CString strInfo;
strcpy(resTgm, strMsg.GetBuffer());
strMsg = strFor1 + strMsg + strFor2;
strInfo += "erro!!!";
strcpy(resStr, strInfo.GetBuffer());*/
//memset(resStr, 0, 50);
printf("%s \r\n", pMsgData);
char *resStr = "ParseBaliseMsg3 hello word!";
printf("resStr is: %s \r\n", resStr); time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("The current date/time is: %s \r\n", asctime(timeinfo));
retInt = ;
return resStr;
} extern "C" __declspec(dllexport)
int ParseBaliseMsg4(const unsigned char *pMsgData, char *resTgm, char *resStr)
{
/*CString strInfo;
strcpy(resTgm, strMsg.GetBuffer());
strMsg = strFor1 + strMsg + strFor2;
strInfo += "erro!!!";
strcpy(resStr, strInfo.GetBuffer());*/
//memset(resStr, 0, 50);
printf("%s \r\n", pMsgData);
char *a = "ParseBaliseMsg4 hello word!";
strcpy(resStr, a);
printf("resStr is: %s \r\n", resStr); time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("The current date/time is: %s \r\n", asctime(timeinfo)); return ;
} extern "C" __declspec(dllexport)
int ParseBaliseMsg5(const unsigned char *pMsgData, char *resTgm, char *resStr)
{
/*CString strInfo;
strcpy(resTgm, strMsg.GetBuffer());
strMsg = strFor1 + strMsg + strFor2;
strInfo += "erro!!!";
strcpy(resStr, strInfo.GetBuffer());*/
//memset(resStr, 0, 50);
printf("%s \r\n", pMsgData);
char *a = "ParseBaliseMsg5 hello word!";
strcpy(resStr, a);
printf("resStr is: %s \r\n", resStr); time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("The current date/time is: %s \r\n", asctime(timeinfo)); return ;
} extern "C" __declspec(dllexport)
char* strcpyTest(char* dest, char* sour)
{
char* temp = dest;
while ('\0' != *sour)
{
*dest = *sour;
dest++;
sour++;
}
*dest = '\0';
return temp;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace DotNet_Use_C_Demo
{
public class TestCMethodHelper
{
[DllImport("CDLLDemo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern int ParseBaliseMsg2(string msg, string rmsg, ref byte memory); [DllImport("CDLLDemo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern IntPtr ParseBaliseMsg3(string msg, string rmsg, ref int rInt); [DllImport("CDLLDemo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern IntPtr ParseBaliseMsg4(string msg, string rmsg, [MarshalAs(UnmanagedType.LPStr)]StringBuilder t); [DllImport("CDLLDemo.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
private static extern IntPtr ParseBaliseMsg5([MarshalAs(UnmanagedType.LPStr)]StringBuilder msg, string rmsg, [MarshalAs(UnmanagedType.LPStr)]StringBuilder t); [DllImport("CDLLDemo.dll", EntryPoint = "strcpyTest", CallingConvention = CallingConvention.Cdecl/*, CallingConvention = CallingConvention.Cdecl*/)]
public static extern IntPtr strcpyTest(ref byte destA, string sourA); public static void TestMethod()
{
Byte[] bPara = new Byte[]; //新建字节数组
var r2 = ParseBaliseMsg2("abcd", "", ref bPara[]);
string strGet = System.Text.Encoding.Default.GetString(bPara, , bPara.Length); //将字节数组转换为字符串
Console.WriteLine("返回值:" + r2);
Console.WriteLine("传出值:" + strGet);
Console.WriteLine("***************************************************"); int retResult = ;
IntPtr pRet = ParseBaliseMsg3("", "", ref retResult);
string strRet = Marshal.PtrToStringAnsi(pRet);
Console.WriteLine("返回值:" + strRet);
Console.WriteLine("传出值:" + retResult);
Console.WriteLine("***************************************************"); //StringBuilder方式
StringBuilder sb = new StringBuilder();
var r4 = ParseBaliseMsg4("abcd", "", sb);
Console.WriteLine("返回值:" + r4);
Console.WriteLine("传出值:" + sb.ToString());
Console.WriteLine("***************************************************"); StringBuilder sb5 = new StringBuilder();
StringBuilder sb5E_para = new StringBuilder();
sb5E_para.Append("abcdedf123456");
var r5 = ParseBaliseMsg5(sb5E_para, "", sb5);
Console.WriteLine("返回值:" + r5);
Console.WriteLine("传出值:" + sb5.ToString());
} public static void CpyTest()
{
string strSour = "测试调用C++ dll"; Byte[] bPara = new Byte[]; //新建字节数组 IntPtr pRet = strcpyTest(ref bPara[], strSour);
string strGet = System.Text.Encoding.Default.GetString(bPara, , bPara.Length); //将字节数组转换为字符串
string strRet = Marshal.PtrToStringAnsi(pRet); Console.WriteLine("源字符串:");
Console.WriteLine(strSour); Console.WriteLine("传出值:");
Console.WriteLine(strGet); Console.WriteLine("返回值:");
Console.WriteLine(strRet);
}
}
}

1.用StringBuilder接收Char*参数 需要定义为[MarshalAs(UnmanagedType.LPStr)]StringBuilder,否则就是乱码。
2.用ref byte memory接收Char*参数 不能使用ref IntPtr方式接收,否则返回值一直为空。
3.使用返回值Char* 直接使用IntPtr方式接收即可。
由于博客园一次只让上传10M大小的文件,vs2015新建的C++项目70M大小,压缩后也达到20M,无法上传C++代码。
C++项目创建方式:



_CRT_SECURE_NO_WARNINGS 输入这个,否则编译不过。!!!
C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)的更多相关文章
- C# 调用 C++ dll的两种方式
目录: 1.非托管方式 2.托管方式 3.介绍 extern "C" 4.介绍 DllImport 1.非托管方式 第一种,非托管方式:调用类和方法https://www.co ...
- C#调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)
//----------------------Delphi------------------- procedure GetSqlData(ASource: PChar; ADest: PChar; ...
- C# 调用外部dll(转)
C# 调用外部dll 一. DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...
- C#调用外部DLL介绍及使用详解
一. DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一,打开Windows系统文件 ...
- c#调用c++开发的dll const char* 返回值接收问题
原文:c#调用c++开发的dll const char* 返回值接收问题 用c#调用视频接口相关的dll,dll使用c++开发. c++接口定义如下: PLATFORM const char* Pla ...
- C# 调用 C++ Dll 类型转换的方式 全
摘要:C#引用C++ Dll 所有类型转换的方式 //C++中的DLL函数原型为 //extern "C" __declspec(dllexport ...
- C#调用C++ dll中返回值为字符串的函数问题
C#调用C++ dll函数,如果返回值为字符串,我们使用string去接收就会报错,因为C++返回的是char*,是个指针,所以c# 要用 IntPtr 来接收. C++: //预编译的标头 .h e ...
- 调用DLL的2种方式
[调用DLL的2种方式] DLL在生成的时候会有dll.lib2个文件,另外包含相应的.h. 1.静态方式,通过lib来引用dll,以及引入.h. 2.只通过dll来使用,前提是知道内部的函数符号.
- 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify();
偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.
随机推荐
- 【[CQOI2018]解锁屏幕】
状压这个东西好像没有什么能优化的高级东西,像什么斜率优化,单调队列在状压的优化上都很少见 而最常见的状压优化就是预处理优化了, 这道题就预处理一下所有点对之间连线上的点,之后压成状态就能做到\(O(2 ...
- programming-languages学习笔记--第6部分
programming-languages学习笔记–第6部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} program ...
- 【CSS3】自定义滚动条样式 -webkit-scrollbar
好文推荐:http://m.blog.csdn.net/article/details?id=40398177 http://www.xuanfengge.com/css3-webkit-scroll ...
- 【noip 模拟赛curse,light,maze】 题解
2018.10.16 总结:考的不好 原因: 1.考的时候没状态,读题读不进去 2.考的时候不仔细,该得分没得到 T1:curse 1.咒语 (curse.pas/c/cpp) [题目描述] 亮亮梦到 ...
- git 从远程仓克隆到本地新分支
克隆远程仓代码到本地 git clone http://******:8080/DEV1-WMALL_DQ/WMALL_DQ_Front.git // git 库地址 当前分支是master ...
- 常用 超全局数组$_server
$_SERVER 是一个包含了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web 服务器创建.不能保证每个服务器都 ...
- C++的六个函数
一.构造函数 在C++中,构造函数是六个函数中的第一个,当一个对象被创建时,在它的整个周期中,是一个由生到死的 过程,即构造函数创建对象,析构函数析构对象.在对象被创建时,调用构造函数创建一个对象,这 ...
- MySQL->AUTO_INCREMENT[20180516]
MySQL表格中自增长主键AUTO_INCREMENT使用,实现序列的最简单的方式 创建一个AUTO_INCREMENT自增的表 mysql> create table seq_test( ...
- node创建服务器
//引入核心模块 const http = require('http'); //创建服务器 http.createServer((req,res)=>{ }).listen(3000); // ...
- Idea项目中常见错误及笔记(Old)
1.Idea基础设置: File-->settings--> 1>修改字体:Font 2>修改编码格式:File Encodings(全部UTF-8,右下方复选框勾中--防止程 ...