By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Calling c# from c

P: n/a
Hi All,

Is it possible to export a c# method into a dll in such a way that your
"normal" C application can then call this method?

To be clear: I am not asking how to use "DllImport" or PInvoke. My
question is the other way around.

Regards
Pieter Breed

Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"pi**********@gmail.com" <pi**********@gmail.com> wrote:
Hi All,

Is it possible to export a c# method into a dll in such a way that your
"normal" C application can then call this method?

To be clear: I am not asking how to use "DllImport" or PInvoke. My
question is the other way around.

Regards
Pieter Breed


I'm afraid the simple answer is no.

UNLESS you are willing to have COM invade your C application
and aren't afraid to manipulate the COM interfaces exposed
by the COM Callable wrapper (CCW; created with Regasm.exe)
manually.

Not pretty...

NET Framework Tools Assembly Registration Tool (Regasm.exe)
http://msdn.microsoft.com/library/de...lregasmexe.asp

..NET Framework Developer's Guide Registering Assemblies
with COM
http://msdn.microsoft.com/library/de...ieswithcom.asp

..NET Framework Developer's Guide: Exposing .NET Framework
Components to COM
http://msdn.microsoft.com/library/de...nentstocom.asp
If your going down that road you may want to invest in:

COM and .NET Interoperability
by Andrew Troelsen
769 pages
Apress; 1st edition (April 20, 2002)
ISBN: 1590590112
http://www.amazon.com/exec/obidos/ASIN/1590590112
http://www.apress.com/book/bookDisplay.html?bID=81
http://www.apress.com/book/supplemen...bID=81&sID=371

It contains a concise rundown on how COM works (and why it
was put together that way). And of course it discusses many
of the .NET interop attributes that influence how the CCW is
generated (which only helps if you own the source to the
..NET assembly).
Jul 21 '05 #2

P: n/a
Thanks for your reply.

Now just for curiosity; It seems to me as if it should be possible to
do this. (calling c# dll via c-style dll exports/calling conventions).
I would imagine that you could /start/ the dll the same way that the OS
/starts/ a normal .net exe.

What I mean by this is that the dll that you just made with c# should
as a compiler extra export some functions in the 'extern "C" ' style,
really by just wrapping some attributed functions.

When you call these functions with the c calling convention from just
about anywhere else, they will know to invoke the mscore dll, start the
runtime, do the parameter translation thing etc and run the "exported"
function/method /normally/ as if it really was a c dll as seen from the
outside.

You would obviously be limited in that you will not have access to your
c#/.net classes as such in that you will be confined to calling public
static methods, but this /is/ usefull.

At least from my limited perspective this should/can be implimented
similarly to how normal COM Interop is implimented since really the
same process is taking place.

Friendly Regards
Pieter Breed

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.