Calling c# from c

  • Thread starter Thread starter pieter.breed
  • Start date Start date
P

pieter.breed

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
 
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/d...ml/cpgrfassemblyregistrationtoolregasmexe.asp

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

..NET Framework Developer's Guide: Exposing .NET Framework
Components to COM
http://msdn.microsoft.com/library/d.../cpconexposingnetframeworkcomponentstocom.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/supplementDownload.html?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).
 
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
 
Back
Top