oleaut32.dll returns negative hresult in debugger, but the correct result in a different app and even in my built app

182 Views Asked by At

My Windows Forms app needs to find an active instance of SolidEdge.Application, and for this I've copied all the required code from https://github.com/SolidEdgeCommunity/SolidEdgeSpy. It uses a custom classes MarshalEx and NativeMethods, with a function like CLSIDFromString. Just like the SolidEdgeSpy example code, my code finds the correct CLSID. The function below yields different results, but I have no idea why:

    [DllImport("oleaut32.dll")]
    internal static extern int GetActiveObject(
        [MarshalAs(UnmanagedType.LPStruct)]
        Guid clsid,
        IntPtr pReserved,
        out IntPtr pUnk);

In the debugger it returns a negative value (hResult) -2147221021, but after building my app it works fine and my app is able to 'connect' to the application. It has worked in the past, but for some reason is not working anymore. Does anyone have a clue what has happend (i.e. what I have done wrong)?

Roman's RotView (thanks to @Hans Passant) lists my application, where the name matches the CLSID.RotView tool

1

There are 1 best solutions below

0
Jan Willem On

I found my solution on a different thread, now my custom MarshalEx contains its own GetActiveObject method, and it now works every time

No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#