How do you call methods in a C# COM object from C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • richard190958
    New Member
    • Jul 2008
    • 3

    How do you call methods in a C# COM object from C

    Hi All,
    I need your help...
    I have a large application written in C (compiled using VS 6), which needs to access functions within a library written in C#. Having done some research and it appears that calling C# from C is possible, if the C# is assembled with the "Register for COM interop" setting.
    I have installed Visual Studio 2005, and compiled a sample C# program, however, my C program needs a header file, with the function prototypes etc.
    I understand that you use the MIDL compiler to create the required header files, however, this in turn requires a IDL file, which is not being created.
    I am taking the correct approach?
    What steps do I need to take to generate the header files needed by my C program?
    Thanks In Advance
    Richard
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    It was my understanding that in VB6 you would just use the CreateObject (or whatever it was called) function to create an instance of your C#(.Net) classes from your DLL that has the register for COM or whatever.

    I have never done it though, so I am curious as to how it is done as well.

    Comment

    • richard190958
      New Member
      • Jul 2008
      • 3

      #3
      Hi,
      I have already found an example of creating an instance of an object in C. The following was taken from the MSDN library:
      Code:
      HRESULT hr;
      struct IFoo *pIFoo;
      struct IGoo *pIGoo;
      
      printf("Hello, world!\n\n");
      hr = CoInitialize(NULL);
      if (FAILED(hr)) {
         printf("CoInitialize Failed: %x\n\n", hr);
         exit(1);
      }
      else {
         printf("CoInitialize succeeded\n");
      }
      
      hr = CoCreateInstance(&CLSID_MyObject, NULL, CLSCTX_ALL, 
                  &IID_IFoo, (void **)&pIFoo);
      if (FAILED(hr)) {
         printf("CoCreateInstance Failed: %x\n\n", hr);
         goto Uninit;
      }
      else {
         printf("CoCreateInstance succeeded\n");
      }
      Having created the object, the required functions can be called by using the pointer within the vTable like so:
      Code:
      pIFoo->lpVtbl->Func1(pIFoo);
      What I am missing, is the header file, which would contain the structures needed by the compiler, which should look like:
      Code:
      typedef struct IFooVtbl
      {
          BEGIN_INTERFACE
          
          HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 
              IFoo __RPC_FAR * This,
              /* [in] */ REFIID riid,
              /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
              
          ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 
              IFoo __RPC_FAR * This);
          
          ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 
              IFoo __RPC_FAR * This);
          
          HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Func1 )( 
              IFoo __RPC_FAR * This);
          
          HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Func2 )( 
              IFoo __RPC_FAR * This,
              int inonly);
          
          END_INTERFACE
      } IFooVtbl;
      According to the Microsoft documentation, the MIDL compiler can generate the headers, however, I do not know how this works with C#, as there is no IDL file. As I stated in my initial post, I have assembled a sample C# program with the "Register for COM interop", and trusted that there would be a method of creating the header file needed by my C program.

      Comment

      Working...