C#调用C++ DLL 传出字符串,C++ DLL传参问题

本文介绍了一个跨语言API调用的例子,展示了如何从C#调用C++ DLL以传递和接收字符串数据。具体实现包括C++的.h和.cpp文件定义及实现,以及相应的C#调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ .h文件

VIDEO_DLL_API int __cdecl PlatformQueryRes(char* pcSvrIpAddress, char *pcLoginUserName, char** ppcResult);

C++ .cpp文件

int __cdecl PlatformQueryRes(char* pcSvrIpAddress, char *pcLoginUserName, char** ppcResult)
{
 if (!g_Global.m_bInitPlatformSDKFlag)
 {
  return Platform_ErrUnInit;
 }
 if (NULL == pcSvrIpAddress || NULL == pcLoginUserName || NULL == ppcResult)
 {
  return Platform_ErrInvalidParam;
 }
 string sResult;
 PlatformBase_ptr ptrPlatformBase = g_Global.GetPlatformObject(pcSvrIpAddress);
 if (ptrPlatformBase.get())
 {
  int iResult;
  iResult = ptrPlatformBase->QueryRes(pcLoginUserName, sResult); 
  if (iResult == Platform_ErrSuccess)
  {
   strcpy(*ppcResult, (char*)sResult.c_str());  
  }
  if (iResult == Platform_ErrQueryFail)
  {
   strcpy(*ppcResult, ""); 
  }
  return iResult;
 }else
 {
  return Platform_ErrQueryFail;
 }
}

C# .cs文件

public extern static int PlatformQueryRes(byte[] pcSvrIpAddress, byte[] pcLoginUserName, ref StringBuilder ppcResult);  

ref StringBuilder与char**相对应。

C#调用C++ DLL 传出字符串时,C++ DLL相应接口的传出字符串参数为char**。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值