Calling managed code from C

  • Thread starter Thread starter Ken Allen
  • Start date Start date
K

Ken Allen

We have recently created a new architecture for a product that now
implements all of our user-space code in C#, which makes it much faster
and easier to develop! There are a number of services which are
installed on a central server and some client applications that
communicate using remoted objects with business rule methods. This is fine.

I was just informed that one of our major clients (one who is eager to
adopt the new implementation and is even funding parts of it) has
decided that they want to continue to use some of their legacy
applications with the new system. They are willing to change the code in
their applications to talk to the new system -- but all of theor legacy
applications are written in plain C! Not C++, just C.

I am wondering if it would be possible to develop a managed C++ DLL that
they could link into their code that would permit them to make C type
calls into the DLL, which could then use managed C++ to communicate with
the C# remoted objects. Their code does not work with COM or anything
else, so plain C methods is the preferred interface. Can this be done?
How? Where can I find information on this? Are there other alternatives?

-ken
 
Yes, we've done something similar.

Create your managed C++ dll and export functions just as you normally would.
The calling code won't know and won't care that those functions eventually
call managed code.

-Eric
 
Is not managed C++ 'compile' into MSIL? Yest the C code can call it
cleanly? Excellent!

-ken
 
Eric said:
Yes, we've done something similar.

Create your managed C++ dll and export functions just as you normally
would. The calling code won't know and won't care that those
functions eventually call managed code.

I assume that you have performed the necessary actions to protect
yourself from the mixed code loader lock bug? If not, then you'll find
that at some point code in your managed C++ DLL will cause a deadlock.

Richard
 
Create your managed C++ dll and export functions just as you normally
I assume that you have performed the necessary actions to protect yourself
from the mixed code loader lock bug? If not, then you'll find that at some
point code in your managed C++ DLL will cause a deadlock.
Hoho...
And what is this infamous bug I didn't know about?

(I didn't know about it, perhaps, because I never used this functionality
yet,
but I rate it as hypra cool! (the fonctionality, not the bug!))
 
This seems to be specifically addressing a DLL that contains a mixture
of managed and unmanaged code. In this specific case I would expect the
DLL to contain only managed (C++) code, with some of the methods exposed
as C functions, so it would not be mixed mode.

Am I missing something?

-ken
 
Never mind, I finish read it carefully.
And, as they explained, that should be fixed by 2.0 ;-)

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
Lloyd Dupont said:
BTW, is the problem still there with .NET 2.0 ?

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
"Eric Pearson" <http://blog.ericpearson.org
http://www.geekswithblogs.com/ericpearson> wrote in message
 
You're right, problem only exists if you have managed and unmanged
(mixed-mode). However, if you use the CRT, MFC, ATL or STL, then you are in
this situation (like I am). Trust me, the deadlock is a horrible issue to
deal with.

Ken Allen said:
This seems to be specifically addressing a DLL that contains a mixture
of managed and unmanaged code. In this specific case I would expect the
DLL to contain only managed (C++) code, with some of the methods exposed
as C functions, so it would not be mixed mode.

Am I missing something?

-ken
 
Ken said:
This seems to be specifically addressing a DLL that contains a mixture
of managed and unmanaged code. In this specific case I would expect
the DLL to contain only managed (C++) code, with some of the methods
exposed as C functions, so it would not be mixed mode.

Hmmm, "some of the methods exposed as C functions" you can *only* do
that as mixed mode. You're susceptible to the mixed mode loader bug!

BTW you don't have to have unmanaged code visible in your source code to
be mixed mode. By default 1.1 managed C++ is compiled as mixed mode (run
dumpbin on the DLL and if the CRT is used - which it will be - then your
DLL is mixed mode). You specifically have to take steps to make sure
that the CRT is not used to make it all IL code.

Richard
 
Back
Top