C# IntPtr到String和long?
我正在使用C#和.NET2.0框架 我使用此方法从C# IntPtr到String和long?,c#,.net-2.0,marshalling,C#,.net 2.0,Marshalling,我正在使用C#和.NET2.0框架 我使用此方法从IntPtr中获取字符串: void* cfstring = __CFStringMakeConstantString(StringToCString(name)); IntPtr result = AMDeviceCopyValue_Int(device, unknown, cfstring); if (result != IntPtr.Zero) { byte leng
IntPtr
中获取字符串:
void* cfstring = __CFStringMakeConstantString(StringToCString(name));
IntPtr result = AMDeviceCopyValue_Int(device, unknown, cfstring);
if (result != IntPtr.Zero)
{
byte length = Marshal.ReadByte(result, 8);
if (length > 0)
{
string s = Marshal.PtrToStringAnsi(new IntPtr(result.ToInt64() + 9L), length);
return s;
}
}
return String.Empty;
它可以工作,所有数据都以字符串形式保存在我的USB设备中(返回方法AMDeviceCopyValue
的数据)
现在我明白了:
本月的\r\r\0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0\0\0€+fŒ\a\0端口\0\0\0\0\0€%fŒ\a\0键\0\0\0\0\0€÷\t\0\0\0\0\0端口\0\0\0\0\0\0\0\0\0\0\0\0
这被保存为
long
-因此,如何将此IntPtr
转换为long
而不是string
?如果您只想读取long而不是string,请使用:
long l = Marshal.ReadInt64(result, 9);
return l;
可惜不是我想要的=(我不知道为什么,您的解决方案似乎是正确的我回答了这个问题:
如何将这个IntPtr转换为long而不是string
-提取result+offset 9指向的long at地址。那么您到底想要什么呢?我真的不知道AMDeviceCopyValue\u Int
返回的数据结构是什么,所以我无法在这方面帮助您。)egard.您是否使用了正确的函数来提取long
?这与Java中解决的过程相同:但不是我必须解决它或将此方法转换为c#。net此的内存结构是什么(在您的链接问题中)?通过快速的谷歌搜索,似乎AMDeviceCopyValue\u Int
返回一个指向字符串的指针(CFString
)。您的问题非常模糊且不准确。