使用 MSDASC.DLL 时应该如何修复 "At least one of the arguments for '...' cannot be marshaled" 错误

How should I fix "At least one of the arguments for '...' cannot be marshaled" error when using MSDASC.DLL

我有一个引用 COM 对象的 C# 桌面应用程序 Microsoft OLE DB Service Component 1.0 Type Library。这是允许用户构建和测试连接字符串的对话框。该代码多年来一直按预期运行。

我发现当执行 "Rebuild" 时,会出现 18 个未列举的警告。我相信这意味着在导入类型库时会创建警告。

它们都是以下形式:

Processing COM reference "MSDASC" from path "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll". The type library importer could not convert the signature for the member 'tagDBPROPIDSET.rgPropertyIDs'.
Processing COM reference "MSDASC" from path "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll". At least one of the arguments for 'DataLinks.RemoteCreateDBInstanceEx' cannot be marshaled by the runtime marshaler.  Such arguments will therefore be passed as a pointer and may require unsafe code to manipulate.

仔细分析,我发现只要添加对空 WinForms 项目的引用就会生成警告。不需要访问 MSDASC 库的代码。

作为解决方法,我从重建期间生成的 OBJ 树中复制了 Interop.MSDASC.dll,并将其复制到项目文件夹中。我删除了对 MSDASC 的引用,并在我的项目中向 Interop.MSDASC.dll 添加了一个。

现在,当我重建时,我没有看到警告。我从 OBJ 树中删除了 Interop.MSDASC.dll,它确实被重新创建了。

这是不是掩盖了我的问题?有没有更好的方法来使用对话框并抑制或回答警告?

更新: 这发生在 VS 2017 和 2019 中,但我相信它也发生在以前的版本中。

添加了错误列表的图片window:

它们确实是tlbimp警告。您可以检查是否 运行

TlbImp.exe "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll"

来自 VS 开发人员命令提示符(或来自 C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools 之类的路径,以适应您的机器)。

您应该会看到完全相同的警告。

如果您不使用警告中列出的成员,您就很安全了。

否则,如果您编辑 .csproj 并将 ResolveComReferenceSilent 键添加到第一个 PropertyGroup 元素,则可以删除所有 COM 引用警告,如下所示:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <PropertyGroup>
    ...
    <ResolveComReferenceSilent>True</ResolveComReferenceSilent>
    ...
  </PropertyGroup>

最后一个解决方案是手动使用 tlbimp.exe 并引用其输出。这里有一个例子:Suppress tlbimp warnings in visual studio