3

几年前,我继承了一个 C# 应用程序,该应用程序使用在非托管 C++ DLL 中定义的 COM 对象。从那以后,我一直在成功地调整对象接口,但是在(可能不相关的)VS2012 升级之后,函数签名的添加和更改突然受到随机异常(如 ExecutionEngineException 和 SEHException)的惩罚。

我的理解是该应用程序使用免注册 COM。没有 DLLRegisterServer 实现,我没有看到注册表中提到接口 guid,只有 C# 中的一个 ...

[ComImport,
Guid("C2427CB1-D6AE-49e8-B266-114F981C3353"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
SuppressUnmanagedCodeSecurity()]
public interface IDC
{

一个在 C++ 头文件中。

interface __declspec(uuid("C2427CB1-D6AE-49e8-B266-114F981C3353"))
IDC : IUnknown
{

不过可以肯定的是,我决定在这两个地方换一个新的 guid,并发现它完全阻止了 C# 识别该类:

System.InvalidCastException

Unable to cast COM object of type 'System.__ComObject' to interface type 'Apx.IDC'.
This operation failed because the QueryInterface call on the COM component for the
interface with IID '{the new guid}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

(上述错误显然通常是由混合公寓状态引起的,但交换 guid 不会导致这种情况)。所以我推断在某处必须有与我的更改相冲突的其他相关细节。但那可能在哪里呢?谢谢阅读...

编辑:

一个示例例外是...

First-chance exception at 0x000007feec748be4 (clr.dll) in Apex.exe: 0xC0000005:
Access violation reading location 0xffffffffffffffff.
The Common Language Runtime cannot stop at this exception. Common causes include:
incorrect COM interop marshalling, and memory corruption. To investigate further,
using native-only debugging.

An unhandled exception of type 'System.ExecutionEngineException' occurred in Apex.exe

为了

[PreserveSig] [return: MarshalAs(UnmanagedType.I1)]
bool LoadDisplayList(IntPtr fileHandle, IntPtr pDisplayList,
UInt16 version, IntPtr pComparison);

virtual bool __stdcall LoadDisplayList(HANDLE fileHandle, class CDisplayList * pDisplayList,
WORD version, CDisplayList * pComparison) = 0;

以堆栈跟踪结尾

[Native to Managed Transition]  
Apex.Graphics64.dll!CDisplayList::LoadRenderRecs(void * f=0x000000000000056c, CDisplayList * pComparison=0x0000000023ad2cf0)  Line 1772 C++
Apex.Graphics64.dll!CDisplayList::Load(void * f=0x000000000000056c, unsigned short version=0x0002, CDisplayList * pComparison=0x0000000023ad2cf0)  Line 1845 + 0x26 bytes   C++
Apex.Graphics64.dll!CBaseDC::LoadDisplayList(void * f=0x000000000000056c, CDisplayList * pList=0x0000000023ad2cf0, unsigned short version=0x0002, CDisplayList * pComparison=0x0000000023ad2cf0)  Line 1896 + 0x33 bytes    C++
[Managed to Native Transition]  
Apex.exe!Apex.DDC.LoadDisplayList(System.IO.FileStream file = {System.IO.FileStream}, Apex.DisplayList displayList = {Apex.DisplayList}, ushort version = 0x0002, Apex.DisplayList comparison = null) Line 1124 + 0xaf bytes    C#
Apex.exe!Apex.DisplayList.Load(System.IO.FileStream f = {System.IO.FileStream}, ushort loadVersion = 0x0002, Apex.INode stubsRoot = {Apex.ViewPort3D}, Apex.DisplayList comparison = null) Line 166 + 0x53 bytes    C#

确切的崩溃点有所不同 - 这里它实际上设法进入了 LoadDisplayList(),这比平时要好。由于崩溃表明堆损坏,我尝试将函数的签名剥离为无参数的 void 返回,将其内容减少为跟踪,并在对象创建后立即调用它 - 仍然会崩溃。如果我将相同的函数移到定义的顶部,它不会崩溃,在这种情况下,其他一些接口函数会崩溃,这让我认为它更有可能是 COM 问题而不是算法级内存损坏。

4

1 回答 1

1

我想我已经解决了这个问题。一旦我能够稍微进入 DLL,就会弹出一个说源与可执行文件不匹配的弹出窗口,这让我调查了另一个长期存在但以前没有问题的怪癖,即 DLL 的链接器/常规/输出文件不在项目的常规/输出目录(警告 MSB8012)。清理完这个异常后,我能够成功更改 guid,因此 VS2012 使用这些设置似乎与旧版本略有不同。道德...不要让警告徘徊。

于 2013-03-25T10:16:25.800 回答