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

本文介绍了C#如何正确调用C++ DLL,处理返回的Char*类型,防止出现乱码。在C#中,通过[MarshalAs(UnmanagedType.LPStr)]StringBuilder接收Char*参数可以避免乱码;用ref byte memory接收时不能使用ref IntPtr;而直接用IntPtr接收返回的Char*是可行的。文章还提到了C++项目创建时的注意事项,并提供了运行文件的下载链接。
摘要由CSDN通过智能技术生成

c++ 代码部分:

// 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 
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值