鸿 网 互 联 www.68idc.cn

C++/CLI 托管C++之字符串封装【5】

来源:互联网 作者:佚名 时间:2015-01-01 15:54
【1】字符串作为输入参数C++导出函数://1.1 字符串作为输入参数EXPORTDLL_CLASS int Str_Output( WCHAR *pInStr ){if (NULL == pInStr){return(-1);}wprintf(LS

【1】字符串作为输入参数

C++导出函数:

//1.1 字符串作为输入参数 EXPORTDLL_CLASS int Str_Output( WCHAR *pInStr ) { if (NULL == pInStr) { return(-1); } wprintf(L"Str_Output %s\n", pInStr); return(0); }


CLI类封装 /// <summary> /// 1 字符串测试类 /// </summary> public ref class StrCls { public: /// <summary> /// 1.1 字符串作为输入参数 /// </summary> /// <param name="pInStr">字符串</param> /// <returns>成功返回0</returns> static Int32 StrOutput(String^ pInStr); /// <summary> /// 1.2 字符串作为参数,需要进行修改返回 /// </summary> /// <param name="pOutStr">字符串</param> /// <returns>成功返回0</returns> static Int32 StrChange(String^ %pOutStr); /// <summary> /// 1.3 字符串数组作为参数 /// </summary> /// <param name="ppStr">字符串数组</param> /// <returns>成功返回0</returns> static Int32 StrChangeArr(array<String^>^ %ppStr); };


CLI函数实现:
System::Int32 ExportCLI::StrCls::StrOutput( String^ pInStr ) { if (String::IsNullOrEmpty(pInStr)) { return(-1); } WCHAR *pStr = ManagedStr2UnmanagedUniStr(pInStr); HRESULT hr = Str_Output(pStr); //内存释放 if (NULL != pStr) { delete []pStr; pStr = NULL; } return(hr); }


C#测试:
//1. 字符串测试类 string pInStr = "1.1"; StrCls.StrOutput(pInStr); </pre><p></p><span style="color:rgb(51,51,255)"><strong><span style="font-family:Microsoft YaHei; font-size:18px">【2】字符串作为参数,,需要返回修改</span></strong></span><p><span style="font-family: 'Microsoft YaHei'; font-weight: bold; font-size: 18px;">C++导出函数:</span></p><p><span style="font-weight:bold"><span style="font-family: 'Microsoft YaHei'; font-size: 18px;"><span style="font-family:'Microsoft YaHei'"><span style="font-weight:bold"><span style="font-family:'Microsoft YaHei'"><span style="font-family:'Microsoft YaHei'"><span style="font-family:'Microsoft YaHei'"></span></span></span></span></span></span></span></p><pre name="code" class="cpp" style="font-size:18px;">//1.2 字符串作为参数,需要进行修改返回 EXPORTDLL_CLASS int Str_Change( WCHAR *pOutStr) { if (NULL == pOutStr) { return(-1); } int len = wcslen(pOutStr); for (int ix=0; ix<len-1; ix++) { pOutStr[ix] = 'a' + (ix) % 26; } pOutStr[len-1] = '\0\0'; wprintf(L"Str_Change %s\n", pOutStr); return(0); }


CLI函数实现:
System::Int32 ExportCLI::StrCls::StrChange( String^ %pOutStr ) { if (String::IsNullOrEmpty(pOutStr)) { return(-1); } WCHAR *pStr = ManagedStr2UnmanagedUniStr(pOutStr); HRESULT hr = Str_Change(pStr); pOutStr = UnmanagedStr2ManagedUniStr(pStr); //内存释放 if (NULL != pStr) { delete []pStr; pStr = NULL; } return(hr); }


C#测试:
string pOutStr = "1.2"; StrCls.StrChange(ref pOutStr);


【3】字符串数组作为参数,需要返回修改

C++导出函数:

//1.3 字符串数组作为参数 EXPORTDLL_CLASS int Str_ChangeArr( WCHAR **ppStr, int len ) { if (NULL == ppStr) { return(-1); } for (int ix=0; ix<len; ix++) { if (NULL != ppStr[ix]) { lstrcpyn(ppStr[ix], L"abc", wcslen(ppStr[ix])); } } wprintf(L"Str_ChangeArr \n"); return(0); }


CLI函数实现:
System::Int32 ExportCLI::StrCls::StrChangeArr( array<String^>^ %ppStr ) { if (ppStr == nullptr) { return(-1); } int len = ppStr->Length; //数据元素的个数 WCHAR **pp = new WCHAR *[len]; for (int ix=0; ix<len; ix++) { pp[ix] = ManagedStr2UnmanagedUniStr(ppStr[ix]); } HRESULT hr = Str_ChangeArr(pp, len); for (int ix=0; ix<len; ix++) { ppStr[ix] = UnmanagedStr2ManagedUniStr(pp[ix]); } //内存释放 for (int ix=0; ix<len; ix++) { delete [](pp[ix]); pp[ix] = NULL; } delete []pp; pp = NULL; return(hr); }


C#测试:
string[] ppStr = new string[2] { "1.31.......", "1.32......." }; StrCls.StrChangeArr(ref ppStr);


源代码:

https://coding.net/u/aoshilangCode/p/CLI/git


网友评论
<