Repository navigation
Description
Description
When using documentation at https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation (with AOT and disabled runtime marshalling), the code crashes with a Fatal error. Internal CLR error. (0x80131506) (System.ExecutionEngineException).
Reproduction Steps
Here's my .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>Here's my Program.cs:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
[assembly: DisableRuntimeMarshalling]
namespace ConsoleAotAuthor
{
internal partial class Program
{
static void Main()
{
var cw = new StrategyBasedComWrappers();
var foo = new Foo();
var unk = cw.GetOrCreateComInterfaceForObject(foo, CreateComInterfaceFlags.None);
GivePointerToComInterface(unk);
}
[LibraryImport("Dll1.dll")]
private static partial void GivePointerToComInterface(nint comObject);
}
[GeneratedComInterface, Guid("217104c3-ce28-4d13-a32e-ef65ee75edf1")]
public partial interface IFoo
{
void Method(int i);
}
[GeneratedComClass]
public partial class Foo : IFoo
{
public void Method(int i) => Console.WriteLine(".NET i:" + i);
}
}My dll1.cpp:
#include <windows.h>
BOOL __stdcall DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
return TRUE;
}
MIDL_INTERFACE("8c485499-003f-49c1-b8b6-a1ba8c53307c")
IFoo : public IUnknown
{
public:
virtual void STDMETHODCALLTYPE Method(int i) = 0;
};
void __stdcall GivePointerToComInterface(IFoo* foo)
{
foo->Method(12345678);
}
My dll1.def (to export the method):
LIBRARY
EXPORTS
GivePointerToComInterface PRIVATE
Expected behavior
I believe it should just run without problem.
Actual behavior
The code crashes with a Fatal error. Internal CLR error. (0x80131506) (System.ExecutionEngineException). I'm not sure if it should work as in the documentation or if it shouldn't, but crashing is a bit violent.
Regression?
Seems like a regresssion if the documentation can be trusted but can it? I've remarked the doc code doesn't compile as is as it's not using today mandatory CreateComInterfaceFlags.
Known Workarounds
The code works if we do this instead of passing the native pointer directly:
var iid = typeof(IFoo).GUID;
Marshal.QueryInterface(unk, iid, out var ppv);
GivePointerToComInterface(ppv);
Marshal.Release(ppv);
That's why I'm unsure it's not the good way to have this work? I can understand I could have to QI the IUnknown interface if there are multiple vtable on my object, so maybe it's always expected?
Configuration
.NET SDK 8.0.400, Visual Studio 17.11.1
Other information
No response
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Aug 26, 2024
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
AaronRobinsonMSFT commentedon Aug 26, 2024
@smourier This is "by design". See the documentation for
ComWrappers.GetOrCreateComInterfaceForObject(), particularly details on the Return type.The current COM source generator has no notion of "default" and we expect users to call
QueryInterfaceon the result if they desire a specific interface.Correct. The
ComWrappersAPI is low level and we try to avoid any unnecessary overhead for that purpose.I see the confusion in reference documentation/sample. We will update that to be clear that
GivePointerToComInterfaceis expecting anIUnknown*and not anIFoo*.smourier commentedon Aug 26, 2024
Ok, that makes sense now, I was wondering how that could work anyway.
IMHO, it would be nice to have some wrappers that would return other interfaces pointers, something like:
that would do the QI so I could call it like this:
As a "frequent COM flyer", It's quite perturbing to have to juggle between ComWrappers and Marshal classes, actually it would be nice with all that new code to be able to avoid using Marshal completely :-)
Just saying.