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 120;
}

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 = 130;
    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 140;
}

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 150;
}


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[100];    //新建字节数组
            var r2 = ParseBaliseMsg2("abcd", "", ref bPara[0]);
            string strGet = System.Text.Encoding.Default.GetString(bPara, 0, bPara.Length);    //将字节数组转换为字符串
            Console.WriteLine("返回值:" + r2);
            Console.WriteLine("传出值:" + strGet);
            Console.WriteLine("***************************************************");

            int retResult = 0;
            IntPtr pRet = ParseBaliseMsg3("1234", "", 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[100];    //新建字节数组

            IntPtr pRet = strcpyTest(ref bPara[0], strSour);
            string strGet = System.Text.Encoding.Default.GetString(bPara, 0, 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方式接收即可。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈尼嘟嘟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值