C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

时间:2023-02-20 08:30:18
// 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);
}
}
}

C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

1.用StringBuilder接收Char*参数 需要定义为[MarshalAs(UnmanagedType.LPStr)]StringBuilder,否则就是乱码。

2.用ref byte memory接收Char*参数  不能使用ref IntPtr方式接收,否则返回值一直为空。

3.使用返回值Char*  直接使用IntPtr方式接收即可。

由于博客园一次只让上传10M大小的文件,vs2015新建的C++项目70M大小,压缩后也达到20M,无法上传C++代码。

C++项目创建方式:

C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

_CRT_SECURE_NO_WARNINGS 输入这个,否则编译不过。!!!

运行文件点击这里下载。

C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)的更多相关文章

  1. C&num; 调用 C&plus;&plus; dll的两种方式

    目录: 1.非托管方式 2.托管方式 3.介绍 extern "C" 4.介绍   DllImport 1.非托管方式 第一种,非托管方式:调用类和方法https://www.co ...

  2. C&num;调用Delphi Dll返回字符串的示例(使用Move才能拷贝字符串)

    //----------------------Delphi------------------- procedure GetSqlData(ASource: PChar; ADest: PChar; ...

  3. C&num; 调用外部dll(转)

    C# 调用外部dll   一.      DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...

  4. C&num;调用外部DLL介绍及使用详解

    一.      DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一,打开Windows系统文件 ...

  5. c&num;调用c&plus;&plus;开发的dll const char&ast; 返回值接收问题

    原文:c#调用c++开发的dll const char* 返回值接收问题 用c#调用视频接口相关的dll,dll使用c++开发. c++接口定义如下: PLATFORM const char* Pla ...

  6. C&num; 调用 C&plus;&plus; Dll 类型转换的方式 全

    摘要:C#引用C++ Dll 所有类型转换的方式         //C++中的DLL函数原型为         //extern "C" __declspec(dllexport ...

  7. C&num;调用C&plus;&plus; dll中返回值为字符串的函数问题

    C#调用C++ dll函数,如果返回值为字符串,我们使用string去接收就会报错,因为C++返回的是char*,是个指针,所以c# 要用 IntPtr 来接收. C++: //预编译的标头 .h e ...

  8. 调用DLL的2种方式

    [调用DLL的2种方式] DLL在生成的时候会有dll.lib2个文件,另外包含相应的.h. 1.静态方式,通过lib来引用dll,以及引入.h. 2.只通过dll来使用,前提是知道内部的函数符号.

  9. 一道前端面试题:定义一个方法将string的每个字符串间加个空格返回,调用的方式&&num;39&semi;hello world&&num;39&semi;&period;spacify&lpar;&rpar;&semi;

    偶然在群里看到了这道题:定义一个方法将string的每个字符串间加个空格返回,调用的方式'hello world'.spacify(); 这道题主要是对JavaScript对象原型的考察.

随机推荐

  1. &period;NET Core 构建配置文件从 project&period;json 到 &period;csproj

    从 .NET Core SDK 1.0 Preview 3 build 004056 开始,.NET Core 弃用 project.json,回归 .csproj,主要原因是为了兼容 MSBuild ...

  2. activity通过onActivityResult间数据交互

    首先要创建2个activity 分别为MainActivity和OneActiivity MainActivity代码如下: package com.tp.soft.app; import andro ...

  3. 解决discuz论坛搬家:&OpenCurlyDoubleQuote;Table &OpenCurlyQuote;common&lowbar;syscache’ is read only”问题

    解决discuz论坛搬家:“Table ‘common_syscache’ is re http://www.zixuephp.com/wzht/discuz/20141203_11562.html ...

  4. zxing 生成二维码

    一.zxing介绍 zxing是google提供生成.解析一维码.二维码的开源库. 二.使用 2.1 maven pom 配置 <dependency> <groupId>co ...

  5. 面向对象重写(override)与重载(overload)区别

    一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重写(重新实现)父类中的方法. 重写(覆盖)的规则: 1.重写方法的参数列表必须完 ...

  6. 兼容性:Adapter(适配器模式)【PHP】

    Adapter(适配器模式) ---- 加个“适配器”以便于复用 将一个类的接口转换成客户希望的另一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 应用场景 如果 ...

  7. Perl文件名通配和文件查找

    在shell中使用*来对文件名进行通配扩展,在Perl中也同样支持文件名通配.而且perl中的glob通配方式和shell的通配方式完全一致,实际上perl的glob函数就是直接调用csh来通配的(如 ...

  8. Luogu P2426 【删数】

    状态定义: 一眼区间$DP$,从左右两边删不好定义状态,不如定义$dp[i][j]$表示$[i,j]$未删的最大值,转移就很自然了 转移: 从左边删$dp[i][j]=max(dp[i][j],dp[ ...

  9. Metasploit数据库问题汇总

    数据库在metaspoit中是相当重要的,当做一个大型渗透测试项目的时候,收集到的信息是相当大的,当和你的同伴一起协同作战的时候,你们可能 在不同的地方,所以数据共享很重要了!而且Metasploit ...

  10. &lbrack;Spring Boot&rsqb; &commat;Component&comma; &commat;AutoWired and &commat;Primary

    Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in s ...