Search code examples
c#.netcomdirectshow

How do i know if i'm calling the COM object method correctly


I import COM object from C# using ComImportAttribute.

For example, I import as follows:

[ComImport] 
[Guid(ComLibrary.Constants.IGraphBuilderGuid)] 
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IGraphBuilder : IUnknown {

    [PreserveSig]
    public HResult AddFilter(
        [In] IBaseFilter filter,
        [In, MarshalAs(UnmanagedType.LPWStr)] String name
    );

    [PreserveSig]
    public HResult RemoveFilter(
        [In] IBaseFilter filter 
    );

    [PreserveSig]
    public HResult EnumFilters(
        [Out] IBaseFilter[] filters 
    );
}

How can I check if the COM object method I'm calling is correct?

I try to take into account the type of interface (IUnknown or IDispatch) and describe the methods in the correct order, but the code in which I use imported objects does not give me the desired result, although similar code in C++ works as it should.

Therefore, I have suspicions that I am importing objects incorrectly again.


Solution

  • After some time, I managed to find the answer to my question. In order to see which COM function is called, let's use Visual Studio (I can't say for sure about other IDE`s) and the native code debugging mode.

    For example, we define the following COM interface (gap is specified incorrectly here):

    [ComImport]
    [Guid(Constants.IBaseFilterGuid)]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IBaseFilter {
    
        void _VtblGap0_8();
    
        [PreserveSig]
        public HResult QueryFilterInfo(
            [Out] out FilterInfo filterInfo
        );
    }
    

    First, you need to enable machine code debugging mode in the project properties.
    enter image description here

    After that, set a breakpoint on the function being checked (in our case, this is the QueryFilterInfo function).
    enter image description here

    Run the program in debug mode and press the Step Into (F11) button at the breakpoint. In the Source Not Available tab, click View Disassembly. enter image description here

    Аfter opening the Disassembly tab at the very top, you can see the name of the function that was called.
    enter image description here

    You may notice that the wrong function was called, and accordingly the gap-function is incorrect.

    P.S.
    English is not my native language, so in some moments I use a translator. Please forgive me