-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
COM interfaces should have attributed with [ComImport] #198
Comments
Had to search a bit to find another candidate than my usual static class Program
{
[STAThread]
static void Main()
{
Application.OleRequired();
PInvoke.CoRegisterClassObject(typeof(CustomObject).GUID, new CustomFactory(), (uint)CLSCTX.CLSCTX_INPROC_SERVER, (uint)REGCLS.REGCLS_MULTIPLEUSE, out var cookie).ThrowOnFailure();
PInvoke.CoCreateInstance(typeof(CustomObject).GUID, null, (uint)CLSCTX.CLSCTX_INPROC_SERVER, typeof(CustomObject).GUID, out var obj).ThrowOnFailure();
Debug.Assert(obj is CustomObject);
}
}
[ComVisible(true)]
[Guid("7E9C7DDD-CB25-4D98-AECB-5BCD3D66C935")]
class CustomFactory : IClassFactory
{
public unsafe void CreateInstance([MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, Guid* riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObject)
{
// being lazy here to prove the point, this is not a good implementation
if (riid is null || *riid != typeof(CustomObject).GUID)
throw new NotSupportedException();
ppvObject = new CustomObject();
}
public void LockServer(BOOL fLock)
{
// being lazy here to prove the point, this is not a good implementation
}
}
[ComVisible(true)]
[Guid("C680720D-AB01-4A89-88A4-6C241CF60B07")]
class CustomObject
{
} [edit] I realized after I made that repro that I'm using the CustomObject class GUID as if it were an interface GUID, which is not really correct, but thats trivially fixable and doesn't change behavior of the repro. The point of failure is registering the class factory, not creating the instance. |
Something strange is going on with the new interface generation, the interfaces mostly work even though they have no
ComImport
attribute (which they are supposed to have but for some reason basic operations work anyways)My quick and dirty trick for testing CCWs in the classic marshaler is to use
Marshal.GetIUnknownForObject
andMarshal.GetComInterfaceForObject
and then call into the vtable, faking a native caller. However for the new interface generationGetComInterfaceForObject
complains and throws an exception, since there is noComImport
attribute on the interface. Copy/pasting the generated source and addingComImport
makes it work and I can call into the vtable as expected.Now I know this is just a quick and dirty test and I want to explore this some more, I had similar errors in the past when doing handwritten projections so the problem might not be restricted to the
Marshal
helper function. However for today I'm running out of time since my usualIDataObject
scenario I use for exploring COM interactions doesn't work at all in the new generator, so I'm leaving this just as a note and will come back to it over the next days and see if I can build an actual repro that doesn't rely on manually callingMarshal
helper functions.The text was updated successfully, but these errors were encountered: