1
我想使用kernel32.dll的lstrcpy从C#中的指针获取字符串,但它不工作。 lstrlenA正在工作,它给了我字符串的长度,所以我至少碰到了kernel32.dll。 lstrcpy正在我转换的VB6应用程序中工作,所以我知道它可以工作,但我不知道它为什么不在这里。lstrcpy没有更新传递字符串
字符串s永远不会被实际字符串填充,它只是返回初始填充字符串。
[DllImport("kernel32.dll", EntryPoint = "lstrlenA", CharSet = CharSet.Ansi)]
private static extern int lstrlen(int StringPointer);
[DllImport("kernel32.dll",EntryPoint = "lstrcpyA", CharSet = CharSet.Ansi)]
private static extern int lstrcpy(string lpString1, int StringPointer);
private static string StringFromPointer(int pointer)
{
//.....Get the length of the LPSTR
int strLen = lstrlen(pointer);
//.....Allocate the NewString to the right size
string s = "";
for (int i = 0; i < strLen; i++)
s += " ";
//.....Copy the LPSTR to the VB string
lstrcpy(s, pointer);
return s;
}
为什么你不使用Marshal.PtrToStringAnsi? http://msdn.microsoft.com/en-us/library/7b620dhe(v=VS.100).aspx – dtb 2010-04-30 17:06:58
Marshal.PtrToStringAnsi的作品!如果你回答它,我会将其标记为接受的答案。 – 2010-05-04 15:01:01