【发布时间】:2009-05-14 20:37:54
【问题描述】:
我有一个奇怪的问题。 我有一个带有标签的表单,用于在程序中的某些点输出文本,而不是控制台输出。 给定以下代码:
result = SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref tBuff,
(uint)SPDRP.DEVICEDESC,
out RegType, ptrBuf,
buffersize, out RequiredSize);
if (!result)
{
errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
statusLabel.Text += "\nSetupDiGetDeviceRegistryProperty failed because "
+ errorMessage.ToString();
}
else
{
statusLabel.Text += "\nPtr buffer length is: " + ptrBuf.Length.ToString();
sw.WriteLine(tCode.GetString(ptrBuf) );
sw.WriteLine("\n");
// This is the only encoding that will give any legible output.
// Others only show the first character "U"
string tmp = tCode.GetString(ptrBuf) + "\n";
statusLabel.Text += "\nDevice is: " + tmp + ".\n";
}
我只得到标签上的一个硬件 ID 输出。这段代码在我循环的末尾。一开始,这让我觉得我的循环有点悬,但是当我决定将输出直接输出到一个文件时,我几乎得到了我想要的和循环外的输出。 谁能告诉我这里发生了什么? 我想要的只是从 []byte (ptrBuf) 中获取表示硬件 ID 的字符串。 请有人解释一下这里发生了什么? 我的工作环境是 MSVstudio 2008 express。在 Windows 7 中。
谢谢
【问题讨论】:
-
如果你在硬件 ID 之后,你不应该传递 SPDRP_HARDWAREID 而不是 SPDRP_DEVICEDESC 吗?