Reputation: 30045
I got a situation where the client got 'C' program which does encryption of columns at client application side. And the same is now to be used in DataWarehousing project in one of the script component which supports C#.
Is there a way this C program can be compiled to dll and imported into C# [or] do we need to re-write this in C#?
Thanks
Upvotes: 2
Views: 187
Reputation: 26171
Mixing managed(c#) and unmanaged(c) code isn't too difficult, C# has a great feature called pinvoke to do this, also check out this tutorial on interfacing managed and unmanaged code, then you can just compile the dll as a normal C dll
Upvotes: 1
Reputation: 338
Yes, it is possible.
Create a new win32 dll project in visual studio. Lets say you want to create a C method called sum and make available trough the dll. First you create the method:
int _stdcall sum(int x , int y)
{
return x+y;
}
And if VS didnt create a .def file (it should have done so) then create a .def file and write the following in that file:
LIBRARY Example
DESCRIPTION Example library for C
EXPORTS
sum @1
This will create a accessible method called sum available when loading the dll. Now you have to compile and use it in your C# project using DLLImport.
Upvotes: 1
Reputation: 17624
Rather than using P/Invoke, I find it easier to create a C++/CLI class library project, where you write managed classes in C++ code which wrap the C code. Then from your C# project you can add a reference to the class library, and call the wrapper classes directly.
Upvotes: 0
Reputation: 1321
It can be compiled to a dll but you'll probably need to make some changes to the source code. Take a look at this tutorial. And for using your dll in C# check out P/Invoke here. That should be enough to get you going.
Upvotes: 1