Calling C# Methods in a Dll file from a unmanaged C++ Program

  • Thread starter Thread starter Grant Stanley
  • Start date Start date
G

Grant Stanley

Im writting a program in unmanaged C++, and want to access a MS SQL
Database. I've done quite a lot of database access stuff in C# before so I'm
wanting to use C# for the database access in the program.

I want to write a C# DLL file where I can call methods from the C++ program
so that it can get data out of the database. The C++ program is only needing
to get a few things from the database so it's going to be simpler to just
use C# for this, and im not trying to create a wrapper for a lot of .NET
database functions or anything like that.

So how do I go about writting a C# Dll and accessing it from unmanaged C++?

Thank You for any help,
Grant
 
Use tlbexp to generate a Com based wrapper for your .Net code. With the
wrapper, you can simply call your .Net code as though it was a COM object.
 
Kevin is absolutly correct that you need a COM callable wrapper to use .NET
code from the unmanged world...

I am just wondering why you can´t use C++ to access the database in the
first place? Somehow it seems kind of wrong to access the db through .NET if
you still are going to need C++ for the most part and only needs a few parts
from a db??

Anyway good luck!
/Oscar
 
IL has attributes for exporting managed methods as C style method exports. If
you were
savvy, and didn't want to write a CCW you could probably work at the IL level to
export
your methods.
 
The reason is that I know how to access the SQL database from .NET but I
have no idea how to do it easily in C++

If its as easy as from C# then I will certainly try. I guess I should look
into it.

Grant
 
Back
Top