473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jul 29 '08 #1
2 1144
Plater
7,872 Expert 4TB
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.
Jul 29 '08 #2
Hi,
I have already found an example of creating an instance of an object in C. The following was taken from the MSDN library:
Expand|Select|Wrap|Line Numbers
  1. HRESULT hr;
  2. struct IFoo *pIFoo;
  3. struct IGoo *pIGoo;
  4.  
  5. printf("Hello, world!\n\n");
  6. hr = CoInitialize(NULL);
  7. if (FAILED(hr)) {
  8.    printf("CoInitialize Failed: %x\n\n", hr);
  9.    exit(1);
  10. }
  11. else {
  12.    printf("CoInitialize succeeded\n");
  13. }
  14.  
  15. hr = CoCreateInstance(&CLSID_MyObject, NULL, CLSCTX_ALL, 
  16.             &IID_IFoo, (void **)&pIFoo);
  17. if (FAILED(hr)) {
  18.    printf("CoCreateInstance Failed: %x\n\n", hr);
  19.    goto Uninit;
  20. }
  21. else {
  22.    printf("CoCreateInstance succeeded\n");
  23. }
Having created the object, the required functions can be called by using the pointer within the vTable like so:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. typedef struct IFooVtbl
  2. {
  3.     BEGIN_INTERFACE
  4.  
  5.     HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( 
  6.         IFoo __RPC_FAR * This,
  7.         /* [in] */ REFIID riid,
  8.         /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject);
  9.  
  10.     ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( 
  11.         IFoo __RPC_FAR * This);
  12.  
  13.     ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( 
  14.         IFoo __RPC_FAR * This);
  15.  
  16.     HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Func1 )( 
  17.         IFoo __RPC_FAR * This);
  18.  
  19.     HRESULT ( STDMETHODCALLTYPE __RPC_FAR *Func2 )( 
  20.         IFoo __RPC_FAR * This,
  21.         int inonly);
  22.  
  23.     END_INTERFACE
  24. } 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.
Jul 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marwan | last post by:
Hello I am using asynchronous delegates to make a call to a COM ActiveX object, but even though the call occurs on a separate thread, my UI is still blocking. If i put the thread to sleep in my...
1
by: Andrew Poulos | last post by:
I'm dynamically building some html OBJECT code and I use this function to and PARAM elements: // function to add a PARAM element to an element (parent, name, value) addParam = function(prt,n,v)...
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
7
by: Byron | last post by:
I have several user controls that have a few methods in common, such LoadFromForm() which populates an object from controls on the form. I want to call that method from the form in which the...
4
by: qualitynice | last post by:
HELP :-)... I'm creating an embedded activex object in an asp.net page using the HtmlGenericControl class. I'm doing this because when I tried to embed it directly in the aspx page, it stopped...
7
by: Bonzo | last post by:
>From within a function, I want to pass a/some parameters to another function, AND all arguments, passed into this function. e.g. function firstFunction(){ //this function may have been...
4
by: Paul | last post by:
Hi, I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.