using C# DLL from plain C

  • Thread starter Thread starter dvtaylor
  • Start date Start date
D

dvtaylor

We write everything in plain old C. We have a potential customer that
wants to provide us a DLL written in C#. How can we call it's
functions?
 
It's not very hard at all.

You will have to make calls from unmanaged code into managed code. Its not
real hard, but I recommend at least VS2003. Basically all you will need to
do is a import your dll into the project, setup the name space, and use a
few special macros and keywords to setup your managed code.

Check out this article on Code Project on how to do it.
(http://www.codeproject.com/managedcpp/unmanaged_to_managed.asp)

Hope this helps...
 
Besides the wrapper solution as mentiond by Frisky, you can call C# from
unmanaged C through COM introp.
If you have a C++ system that has built-in support for COM (all VC++
compilers have this), all you have to do is design your C# classes you want
to expose to COM to be "COM friendly". From this you can build a typelib
(using regasm and/or tlbexp) that can be imported in your C code (#import
compiler directive), your code can now use all COM support offered by the
C++ compiler.
However when using other C++ compilers (without COM support features) it's
much harder to do but it's not impossible.

Willy.
 
Willy has a good idea about using COM Interop to do it.

From a pure "C" perspective you can't do it. "C" has no notion of
namespaces, and .Net relies on them heavily. I base this on trying one of my
good old C++ "Hello World" pieces written in managed code and changing the
extension of the file from ".cpp" to ".c". For 3 lines of code I 20 errors
and numerous warnings.

Is there a problem with having a single file of C++ code mixed in with your
"C"? What about an external DLL written in C++ that acts as a shim to the
"C" code. The C++ code you have to write is really a lot like newer "C" code
(not like old K&R stuff). Here is a sample:


#using <mscorlib.dll>
#using <system.dll>

using namespace System;

void main()
{
String * str = new String(L"Now is the time for all good men to come to the
aid of their country.");
str = str->ToUpper();

Console::WriteLine(str);
}

Note: You could just as easily import a DLL you wrote in C# and call it's
methods. I just used the Microsoft supplied System dll as an example.

(BTW: Don't take this the wrong way, but since I don't know how much of the
..Net stuff you are familiar with: The String is the System::String object
from the System.dll. Also, Console::WriteLine() is
System::Console::WriteLine() from System.dll. And, the String I new'ed will
automatically be garbage collected by the .Net framework since it is a __gc
class from the framework. So, I don't have to delete it.)

Otherwise, it looks like you are talking about Willy's idea of COM.

Hope this helps...
 
Frisky wrote:

We are talking about C calling managed code.

C does not understand *anything* about managed code, it does not know
what managed code is, or anything about how it manages objects.
Willy has a good idea about using COM Interop to do it.

This is the *only* way to call managed objects from C because it is the
only way to initialize the runtime correctly and get access to a managed
object that uses .NET's thunking layers.
From a pure "C" perspective you can't do it. "C" has no notion of
namespaces, and .Net relies on them heavily. I base this on trying

This is irrelevant because C cannot access managed objects. For a start,
all methods on managed objects use a special calling convention called
__clrcall and C does not understand this. (Try it and see <g>, BTW this
calling convention can be used in managed C++ in .NET 2.0, but not by
earlier versions).
one of my good old C++ "Hello World" pieces written in managed code
and changing the extension of the file from ".cpp" to ".c". For 3
lines of code I 20 errors and numerous warnings.

Again, this is irrelevant. The managed extensions are for *C++* not for
C, there is no concept of Managed C (from Microsoft).
Is there a problem with having a single file of C++ code mixed in
with your "C"? What about an external DLL written in C++ that acts as
a shim to the "C" code. The C++ code you have to write is really a
lot like newer "C" code (not like old K&R stuff). Here is a sample:

Let's me re-assert what the OP was saying - dvtaylor wanted to access
code in a managed assembly library written in C#, the call would be made
from C. Thus the call is made from unmanaged code to managed code. The
example from codeproject was managed code calling unmanaged code (and
not very well, since there is a memory leak in the code).

Richard
 
Back
Top