Home > Back-end >  How do i know if i'm calling the COM object method correctly
How do i know if i'm calling the COM object method correctly

Time:12-16

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.

CodePudding user response:

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 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

  • Related