SetupVerifyInfFile-参数不正确。如何使用C#pinvoke

问题描述 投票:0回答:1

我在调用/实现SetupVerifyInfFile时遇到问题。当我调用VerifyInfFile函数时,收到异常“参数不正确”。

    [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool SetupVerifyInfFile(
        string FileName,
        IntPtr AltPlatformInfo,
        ref IntPtr InfSignerInfo);

    [StructLayout(LayoutKind.Sequential)]
    public struct SP_INF_SIGNER_INFO
    {
        public int CbSize;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string CatalogFile;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string DigitalSigner;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string DigitalSignerVersion;
    }

    public static void VerifyInfFile(string infPath)
    {
        SP_INF_SIGNER_INFO infSignerInfo = default;
        infSignerInfo.CbSize = Marshal.SizeOf(infSignerInfo);
        var infSignerPtr = Marshal.AllocHGlobal(infSignerInfo.CbSize);

        try
        {
            Marshal.StructureToPtr(infSignerInfo, infSignerPtr, false);
            if (!SetupVerifyInfFile(infPath, IntPtr.Zero, ref infSignerPtr))
                throw new Exception("Error calling SetupVerifyInfFile().", new 
                                     Win32Exception(Marshal.GetLastWin32Error()));
        }
        finally
        {
            Marshal.FreeHGlobal(infSignerPtr);
        }
    }
c# pinvoke
1个回答
0
投票

我可以看到两个明显的错误:

  1. 您作为ref IntPtr InfSignerInfo传递的第三个参数。将其作为ref传递是错误的。如文档所述,此参数是指向该结构的指针。因此,您需要删除ref
  2. 您没有为SP_INF_SIGNER_INFO指定字符集,因此将获得默认字符集ANSI。与您的函数声明中的CharSet.Auto不匹配。
© www.soinside.com 2019 - 2024. All rights reserved.