如何在C#中从指针读取字符串到缓冲区

发布于 2024-08-15 20:44:04 字数 902 浏览 3 评论 0原文

如何从这个 C++ dll 调用中读取 C# 中的错误字符串?

//
//  PARAMETERS:
//      objptr
//          Pointer to class instance.
//
//      pBuffer
//          Pointer to buffer receiving NULL terminated error message string.   
//          If this value is zero, the function returns the required buffer size, in bytes,
//          and makes no use of the pBuffer. 
//
//      nSize
//          Size of receiving buffer.
//          If this value is zero, the function returns the required buffer size, in bytes,
//          and makes no use of the pBuffer. 
//
//  RETURN VALUES:
//      If pBuffer or nSize is zero, the function returns the required buffer size, in bytes.
//      If the function succeeds, the return value is number of bytes copied into pBuffer.
//      If function fails return value is 0.
//
extern unsafe int GetError(uint objptr, byte* pBuffer, int nSize);

谢谢!

How can I read the error string in C# from this C++ dll call?

//
//  PARAMETERS:
//      objptr
//          Pointer to class instance.
//
//      pBuffer
//          Pointer to buffer receiving NULL terminated error message string.   
//          If this value is zero, the function returns the required buffer size, in bytes,
//          and makes no use of the pBuffer. 
//
//      nSize
//          Size of receiving buffer.
//          If this value is zero, the function returns the required buffer size, in bytes,
//          and makes no use of the pBuffer. 
//
//  RETURN VALUES:
//      If pBuffer or nSize is zero, the function returns the required buffer size, in bytes.
//      If the function succeeds, the return value is number of bytes copied into pBuffer.
//      If function fails return value is 0.
//
extern unsafe int GetError(uint objptr, byte* pBuffer, int nSize);

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一遍 2024-08-22 20:44:04

如果将数据类型从 byte* 更改为 IntPtr,这可能会起作用:

Marshal.PtrToStringAnsi

string 构造函数之一(其中之一还包含 Encoding 参数):

字符串构造函数

If you change the data type from byte* to IntPtr this might work:

Marshal.PtrToStringAnsi

Or one of the string constructors (one of them also includes an Encoding parameter):

String Constructors

平定天下 2024-08-22 20:44:04
byte[] buffer = new byte[1000];
int size;
unsafe
{
  fixed ( byte* p = buffer )
  {
    size = GetError( ???, p, buffer.Length ); 
  }
}
string result = System.Text.Encoding.Default.GetString( buffer, 0, size );
byte[] buffer = new byte[1000];
int size;
unsafe
{
  fixed ( byte* p = buffer )
  {
    size = GetError( ???, p, buffer.Length ); 
  }
}
string result = System.Text.Encoding.Default.GetString( buffer, 0, size );
凶凌 2024-08-22 20:44:04

Marshal.AllocHGlobal(int) 加上 Marshal.PtrToStringAuto(IntPtr, int) 也许?

一些代码上下文会很好,因为我假设您已经使用 p/invoke 或其他一些技巧将 byte* 强制转换为 IntPtr

基本上,调用您的函数来获取缓冲区大小,使用 AllocHGlobal() 分配缓冲区,再次调用它,读入字符串,然后释放缓冲区(使用 Marshall.FreeHGlobal(IntPtr) )。当然,这是假设您可以使用 Marshall 的分配器。

Marshal.AllocHGlobal(int) plus Marshal.PtrToStringAuto(IntPtr, int) perhaps?

Some code context would be nice, as I'm assuming that you're already coercing byte* into an IntPtr using p/invoke or some other trickery.

Basically, call your function to get the buffer size, allocate a buffer using AllocHGlobal(), call it again, read the string in, then free the buffer (using Marshall.FreeHGlobal(IntPtr)). This is assuming you can use Marshall's allocator, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文