【发布时间】:2017-08-10 18:16:24
【问题描述】:
我正在尝试将 SetupGetInfInformation 函数从 Windows 的 SetupAPI 编组到 C#。
我已经定义了必要的(编组)结构,如下所示:
internal const uint INFINFO_INF_NAME_IS_ABSOLUTE = 2;
[StructLayout(LayoutKind.Sequential)]
internal struct SP_INF_INFORMATION
{
public uint InfStyle;
public uint InfCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] VersionData;
}
[DllImport("setupapi.dll", SetLastError = true)]
internal static extern bool SetupGetInfInformation(
[In] string InfSpec,
[In] uint SearchControl,
//[In, Out] ref SP_INF_INFORMATION ReturnBuffer,
[In, Out] ref IntPtr ReturnBuffer,
[In] uint ReturnBufferSize,
[In, Out] ref uint RequiredSize
);
然后我尝试使用该函数,首先通过为 ReturnBufferSize 参数传递 0 来请求所需的大小缓冲区:
IntPtr ip = new IntPtr();
bool result = SetupAPI.SetupGetInfInformation(
@"D:\TestDriverFile\intcoed.inf",
SetupAPI.INFINFO_INF_NAME_IS_ABSOLUTE, // 2
ref ip,
0,
ref i
);
此方法成功运行,并且针对此特定 INF 始终返回 422,因此我相信这一切正常运行。
但是,下一步(适当地分配缓冲区并再次调用函数以用请求的数据填充缓冲区)是我遇到困难的地方。
目前,我正在尝试这个(按照上述):
ip = Marshal.AllocHGlobal((int)i);
result = SetupAPI.SetupGetInfInformation(
@"D:\TestDriverFile\intcoed.inf",
SetupAPI.INFINFO_INF_NAME_IS_ABSOLUTE,
ref ip,
i,
ref i
);
这始终使 vshost.exe 崩溃,并显示“vshost32.exe 已停止工作”对话框,该对话框允许我调试或关闭程序,但没有其他有用的信息。
我还尝试更改编组的 SetupGetInfInformation 函数签名以反映 SP_INF_INFORMATION(而不是 IntPtr,请参阅上面注释掉的行),这样做仍然可以始终如一地在第一次调用 SetupGetInfInformation 并收到 422强>。然后我尝试在缓冲区中为其分配足够的空间,并在第二次调用中传递它,如下所示:
SetupAPI.SP_INF_INFORMATION buf = new SetupAPI.SP_INF_INFORMATION();
// Make the first call, passing in ref buf, receive 422 as a response.
buf.VersionData = new byte[i];
result = SetupAPI.SetupGetInfInformation(
@"D:\TestDriverFile\intcoed.inf",
SetupAPI.INFINFO_INF_NAME_IS_ABSOLUTE,
ref buf,
i,
ref i
);
这也会以与上述相同的方式使 vshost.exe 崩溃。
在我看来,我显然没有为第二次调用分配适当的缓冲区,但我也可能缺少其他必需的项目。
有人介意给我指出正确的方向吗?我还没有在 StackOverflow 上找到有关此特定功能的任何帮助,并且搜索正确编组可变大小的数组并使用 Marshal 分配/转移内存很有帮助(和教育),但还没有让我过去问题。
编辑:
感谢 Dave Cluderay 和 Simon Mourier。我已接受 Simon 的解决方案作为答案,但想提供我完成的代码以(完全)展示如何编组 SetupGetInfInformation 并释放非托管内存:
[StructLayout(LayoutKind.Sequential)]
public struct SP_INF_INFORMATION
{
public int InfStyle;
public int InfCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] VersionData;
}
[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetupGetInfInformation(
string InfSpec,
int SearchControl,
IntPtr ReturnBuffer,
int ReturnBufferSize,
ref int RequiredSize
);
public static void SetupGetInfInformation_NET(
string infPath,
ref SP_INF_INFORMATION infInfo
)
{
infInfo = new SP_INF_INFORMATION();
int size = 0;
IntPtr ip = new IntPtr();
try
{
if (!SetupAPI.SetupGetInfInformation(infPath, SetupAPI.INFINFO_INF_NAME_IS_ABSOLUTE, IntPtr.Zero, 0, ref size))
throw new Exception("Error calling SetupGetInfInformation() for required buffer size.", new Win32Exception(Marshal.GetLastWin32Error()));
if (size == 0)
return;
ip = Marshal.AllocHGlobal(size);
if (!SetupAPI.SetupGetInfInformation(infPath, SetupAPI.INFINFO_INF_NAME_IS_ABSOLUTE, ip, size, ref size))
throw new Exception("Error calling SetupGetInfInformation() to retrieve INF information.", new Win32Exception(Marshal.GetLastWin32Error()));
infInfo.InfStyle = Marshal.ReadInt32(ip, 0); // The first 4-byte int is for InfStyle.
infInfo.InfCount = Marshal.ReadInt32(ip, 4); // The second 4-byte int is for InfCount.
// Marshal the data from the unmanaged buffer to a managed buffer.
byte[] buf = new byte[size];
Marshal.Copy(ip, buf, 0, size);
// Initialize VersionData to be large enough to hold the VersionData from the managed buffer. We remove 8 bytes (4 for InfStyle, 4 for InfCount.)
infInfo.VersionData = new byte[size - 8];
// Copy the VersionData from the managed buffer into infInfo.VersionData, offsetting 8 bytes for InfStyle and InfCount.
Array.Copy(buf, 8, infInfo.VersionData, 0, size - 8);
}
finally
{
Marshal.FreeHGlobal(ip);
}
}
【问题讨论】:
-
也不熟悉此 API,但我认为将
[In, Out] ref IntPtr ReturnBuffer更改为[In, Out] IntPtr ReturnBuffer可能会有所帮助(即删除ref)。
标签: c# winapi pinvoke marshalling setupapi