// 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. maven 编译替换占位符

    首先开启资源配置的插件,由此插件替换占位符 <plugin> <groupId>org.apache.maven.plugins</groupId> <art ...

  2. TabActivity 、fragemnt+fragment 和 Fragment+Viewpager 优缺点

    1 TabActivity : 1 过时了 . 2 activity . 是作为android的四大组件...                   重量级的家伙   ViewGroup   : 特别麻 ...

  3. CSU 1598 最长公共前缀 (简单KMP或者暴力)

    Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 226     Solved: ...

  4. toad调用存储过程,存储过程调用sql 类

    1.定义一个sql 类Hello DROP JAVA SOURCE NEWXZXT."Hello"; CREATE OR REPLACE AND RESOLVE JAVA SOUR ...

  5. LCG(linear congruential generator): 一种简单的随机数生成算法

    目录 LCG算法 python 实现 LCG算法 LCG(linear congruential generator)线性同余算法,是一个古老的产生随机数的算法.由以下参数组成: 参数 m a c X ...

  6. 记一次jvm异常排查及优化

    为方便自己查看,根据工作遇到的问题,转载并整理以下jvm优化内容 有次接到客服反馈,生产系统异常,无法访问.接到通知紧急上后台跟踪,查看了数据库死锁情况--正常,接着查看tomcat 内存溢出--正常 ...

  7. Android程序项目结构(二)

    利用Android Studio创建完第一个Hello World项目后,我们会看到使用project模式的项目结构. 一..gradle和.idea 这两个目录放置的是Android Studio自 ...

  8. ORA-27300: OS system dependent operation:sendmsg failed with status: 105 ORA-27301: OS failure message: No buffer space available

    早上查看数据库alert日志,发现如下ORA-报错: kgxpvfynet: mtype: 61 process 6460 failed because of a resource problem i ...

  9. Oracle数据库sql语句

    1.创建用户.赋权限.删除用户 create user test identified by test default test users temporary tablespace temp; gr ...

  10. Web | 一小时看懂前端基本语法

    自从H5出来之后,web前端的势头好像就有点燎原之势.国内互联网的发展就是这样,像之前的移动App(iOS.Android),简直是火的一塌糊涂.所以不管是培训机构也好,自学成才也好,都是一种途径,能 ...