// 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乱码)的更多相关文章

  1. C# 调用 C++ dll的两种方式

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

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

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

  3. C# 调用外部dll(转)

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

  4. C#调用外部DLL介绍及使用详解

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

  5. c#调用c++开发的dll const char* 返回值接收问题

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

  6. C# 调用 C++ Dll 类型转换的方式 全

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

  7. C#调用C++ 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的每个字符串间加个空格返回,调用的方式'hello world'.spacify();

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

随机推荐

  1. weblogic之CVE-2016-3510反序列化分析

    将反序列化的对象封装进了weblogic.corba.utils.MarshalledObject,然后再对MarshalledObject进行序列化,生成payload字节码.由于Marshalle ...

  2. jQuery .attr()和.removeAttr()方法操作元素属性示例

    今天主要和大家一起分享一下如何使用jQuery的.attr()和.removeAttr()方法读取,添加,修改,删除元素的属性.大家在平时的Web页面制作中都有碰到如何动态的获取元素的属性和属性值,或 ...

  3. spring boot 配置Rabbit

    单独安装Rabbit服务并设置启动,可以通过浏览器访问,一般访问地址是http://localhost:15672/ ,用户名密码看配置文件的用户名密码 1 实例化配置类注解 import org.s ...

  4. 点击底部input输入框,弹出的软键盘挡住input(苹果手机使用第三方输入法 )

    测试移动端页面的时候,偶然发现点击底部input输入框时,弹出的虚拟键盘偶尔会挡住input输入框. 输入框固定在页面底部,如图所示:   input固定底部设计图.png 点击底部input输入框唤 ...

  5. react 配置开发环境

    一:先自行下载安装node和npm 二:cnpm install create-react-app -g 三:create-react-app my-project 四:cd my-project  ...

  6. [笔记] FireDAC DataSet 导入及导出 JSON

    刚好需要将 FireDAC DataSet (TFDDataSet, TFDQuery...) 转成 JSON,网上找了一圈,原来从 XE6 开始就支持这个功能了: 储存: DataSet1.Save ...

  7. 初识hadoop之分布式文件系统(HDFS)

    Hadoop常用发行版: Apache Hadoop CDH  Cloudera Distributed Hadoop HDP  Hortonworks Data Platfrom 分布式文件系统(H ...

  8. Spark Streaming 进阶与案例实战

    Spark Streaming 进阶与案例实战 1.带状态的算子: UpdateStateByKey 2.实战:计算到目前位置累积出现的单词个数写入到MySql中 1.create table CRE ...

  9. 初识spark

    一. spark 概述 1.是什么: ​ Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎.2012年,它是由加州伯克利大学AMP实 验室开源的类 Hadoop MapRedu ...

  10. go字符串转换

    package main import ( "fmt" "strconv" ) /* 常用总结 1.str 转 int a, _ := strconv.Atoi ...