3

我编写了一个托管 C++ 类,它具有以下功能:

void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
{
    strErrorMessage = (char*) Marshal::StringToHGlobalAnsi(_managedObject->GetLastError()).ToPointer();
}

如您所见,这是将最后一个错误的托管字符串复制到非托管世界(char*)的简单方法。

从我的非托管类中,我调用这样的方法:

char err[1000];
ofer->GetLastError(err);

在托管 C++ 方法处设置断点表明字符串已成功转换为char*. 但是,一旦我返回非托管类,内容err[1000]就会丢失,并且再次为空。

4

3 回答 3

2

您正在分配传递参数 (strErrorMessage) 的值,而不是将 Marshal::StringToHGlobalAnsi 返回的缓冲区内容复制到该地址。

正确的实现应该是:

void EndPointsMappingWrapper::GetLastError(char* strErrorMessage, int len) 
{ char *str = (char*) Marshal::StringToHGlobalAnsi(_managedObject->GetLastError()).ToPointer(); 
 strncpy(strErrorMessage,str,len);
 strErrorMessage[len-1] = '\0';
 Marshal::FreeHGlobal(IntPtr(str));
}

长度是传递的缓冲区的大小。

strncpy() will copy at the most len bytes. If there is no null byte among the first n bytes of the str, the destination string won't be null terminated. For that reason we force the '\0' in the last byte of the buffer.

于 2008-11-06T19:36:02.267 回答
0

我们使用以下 C++ 类为我们进行转换,它工作正常。您应该能够修改您的方法来使用它。

文件

public ref class  ManagedStringConverter
{
public:
  ManagedStringConverter( System::String^ pString );
  ~ManagedStringConverter();

  property char* PrimitiveString
  {
     char* get() { return m_pString; }
  }

  /// <summary>
  /// Converts a System::String to a char * string.  You must release this with FreeString.
  /// </summary>
  static const char* StringToChar( System::String^ str );

  /// <summary>
  /// Converts a System::String to a __wchar_t * string.  You must release this with FreeString.
  /// </summary>
  static const __wchar_t * StringToWChar( System::String^ str );

  /// <summary>
  /// Frees memory allocated in StringToChar()
  /// </summary>
  static void FreeString( const char * pszStr );

private:
  char* m_pString;
};

CPP 文件

ManagedStringConverter::ManagedStringConverter( System::String^ pString )
{
  m_pString = const_cast<char*>( ManagedStringConverter::StringToChar( pString ) );
}

ManagedStringConverter::~ManagedStringConverter()
{
  ManagedStringConverter::FreeString( m_pString );
}

// static
const char * ManagedStringConverter::StringToChar( System::String^ str )
{
  IntPtr^ ip = Marshal::StringToHGlobalAnsi( str );
  if ( ip != IntPtr::Zero )
  {
     return reinterpret_cast<const char *>( ip->ToPointer() );
  }
  else
  {
     return nullptr;
  }
}

// static
const __wchar_t * ManagedStringConverter::StringToWChar( System::String^ str )
{
  IntPtr^ ip = Marshal::StringToHGlobalUni( str );
  if ( ip != IntPtr::Zero )
  {
     return reinterpret_cast<const __wchar_t *>( ip->ToPointer() );
  }
  else
  {
     return nullptr;
  }
}

// static
void ManagedStringConverter::FreeString( const char * pszStr )
{
  IntPtr ip = IntPtr( (void *)pszStr );
  Marshal::FreeHGlobal( ip );
}
于 2008-11-06T19:28:05.637 回答
0

问题是 StringToHGlobalAnsi 创建了一个新的未管理内存,并且不会复制到您打算使用的内存中,而您分配给 strErrorMessage。
要解决此问题,您应该执行以下操作:

void EndPointsMappingWrapper::GetLastError(char** strErrorMessage) 
{ 
  *strErrorMessage = (char*) Marshal::StringToHGlobalAnsi(_managedObject->GetLastError()).ToPointer(); 
}

用法应该如下所示:

char* err;
GetLastError(&err);

//and here you need to free the error string memory

有关更多信息,请查看此msdn 文章

于 2008-11-06T19:30:21.357 回答