我正在尝试从 kernel32.dll 调用此函数(GetPackageId):http: //msdn.microsoft.com/en-us/library/windows/desktop/hh446607 (v=vs.85).aspx
我将结构和导入定义如下:
[StructLayout(LayoutKind.Sequential)]
public struct PACKAGE_ID
{
uint reserved;
uint processorArchitecture;
PACKAGE_VERSION version;
String name;
String publisher;
String resourceId;
String publisherId;
}
[StructLayout(LayoutKind.Explicit)]
public struct PACKAGE_VERSION
{
[FieldOffset(0)] public UInt64 Version;
[FieldOffset(0)] public ushort Revision;
[FieldOffset(2)] public ushort Build;
[FieldOffset(4)] public ushort Minor;
[FieldOffset(6)] public ushort Major;
}
[DllImport("kernel32.dll", EntryPoint = "GetPackageId", SetLastError = true)]
static extern int GetPackageId(IntPtr hProcess,out uint bufferLength,out PACKAGE_ID pBuffer);
并这样称呼它:
PACKAGE_ID buffer = new PACKAGE_ID();
result = GetPackageId(hProcess, out bufferLength, out buffer);
但是我得到的返回值为 122 (ERROR_INSUFFICIENT_BUFFER)。我对 PInvoke 比较陌生,不太确定如何从这里开始。我需要在调用函数之前初始化字符串吗?