Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用DllImport从int8指针获取C#字符串类型_C#_C++_C_Dllimport - Fatal编程技术网

如何使用DllImport从int8指针获取C#字符串类型

如何使用DllImport从int8指针获取C#字符串类型,c#,c++,c,dllimport,C#,C++,C,Dllimport,我想使用用C编写的DLL函数,但我不知道如何编写DllImport函数来检索C#中的字符串。C函数签名如下所示: void ni845xStatusToString (int32 StatusCode, uInt32 MaxSize, int8 * pStatusString); [DllImport("Ni845x.dll")] public static extern void ni845xStatusToString(int StatusCode, int MaxSizeOfStatus

我想使用用C编写的DLL函数,但我不知道如何编写DllImport函数来检索C#中的字符串。C函数签名如下所示:

void ni845xStatusToString (int32 StatusCode, uInt32 MaxSize, int8 * pStatusString);
[DllImport("Ni845x.dll")]
public static extern void ni845xStatusToString(int StatusCode, int MaxSizeOfStatusStr, ref IntPtr StatusStrPtr);
IntPtr statusStrPtr = IntPtr.Zero; // = new StringBuilder(1048);
ni845xStatusToString(error, 1048, ref statusStrPtr);

string statusStr = Marshal.PtrToStringAnsi(statusStrPtr);   // <-- HERE returns empty string and statusStrPtr is not zero.  

MessageBox.Show("Error description: " + statusStr);
我想获取pStatusString指向的字符串。我的C#代码如下所示:

void ni845xStatusToString (int32 StatusCode, uInt32 MaxSize, int8 * pStatusString);
[DllImport("Ni845x.dll")]
public static extern void ni845xStatusToString(int StatusCode, int MaxSizeOfStatusStr, ref IntPtr StatusStrPtr);
IntPtr statusStrPtr = IntPtr.Zero; // = new StringBuilder(1048);
ni845xStatusToString(error, 1048, ref statusStrPtr);

string statusStr = Marshal.PtrToStringAnsi(statusStrPtr);   // <-- HERE returns empty string and statusStrPtr is not zero.  

MessageBox.Show("Error description: " + statusStr);
然后在我的函数中,我这样使用它:

void ni845xStatusToString (int32 StatusCode, uInt32 MaxSize, int8 * pStatusString);
[DllImport("Ni845x.dll")]
public static extern void ni845xStatusToString(int StatusCode, int MaxSizeOfStatusStr, ref IntPtr StatusStrPtr);
IntPtr statusStrPtr = IntPtr.Zero; // = new StringBuilder(1048);
ni845xStatusToString(error, 1048, ref statusStrPtr);

string statusStr = Marshal.PtrToStringAnsi(statusStrPtr);   // <-- HERE returns empty string and statusStrPtr is not zero.  

MessageBox.Show("Error description: " + statusStr);
IntPtr statusStrPtr=IntPtr.Zero;//=新StringBuilder(1048);
ni845xStatusToString(错误,1048,参考状态TRPR);

字符串statusStr=Marshal.PtrToStringAnsi(statusStrPtr);//检查两侧的调用约定并确保它们匹配。为什么不尝试类似的操作,然后在后面将其转换为字符串,或者在(statusStrPrt)末尾使用.ToString();代码的一部分<代码>uint statusStrAsInt=(uint)封送处理ReadInt32(statusStrPtr)不确定此链接是否有用,谢谢@DJKRAZE。当我执行此“uint statusStrAsInt=(uint)marshall.ReadInt32(statusStrPtr)”时出现此错误。错误如下:检测到FatalExecutionEngineError消息:运行时遇到致命错误。错误地址位于线程0x214c上的0x680afd8c。错误代码为0xc0000005。此错误可能是CLR或用户代码的不安全或不可验证部分中的错误。此错误的常见来源包括COM interop或PInvoke的用户封送错误,这可能会损坏堆栈。能否在.cs文件顶部显示您的使用情况。。?您是否有使用System.Runtime.InteropServices的
您最初使用StringBuilder的尝试是正确的。您接下来想到的不是,这个函数需要传递一个缓冲区。顺便说一句,这是一个典型的问题,所以用户在发布他们最后一次也是最不可能是正确的尝试时,试图让它发挥作用。不要那样做。如果需要支持,请联系国家仪器。