Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a method from a DLL from C# program

Tags:

c

c#

dll

visual-c++

I have a C program and i have created a DLL file. I am using Windows Vista, and Visual C++.

Now I need to access a method from that DLL, from the Main() method of a C# code. What are steps of doing so?

So far I have added the DLL file as reference, thereafter what should I do?

This is only an example:

int main1( void ) {
  prinf("Hello World");
}

Please note that this class also makes us of other .lib functions, but i was able to successfully create a DLL out of it. (I don't know if this is relevant)

Now i need to access this method from my C# Main();

[STAThread]
static void Main()
{
  // I need to call that main1() method here

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}
like image 941
illep Avatar asked Jun 17 '11 22:06

illep


1 Answers

See using a class defined in a c++ dll in c# code which has a great accepted answer. And as Hans Passant wrote in the comments, you cannot add a native DLL as a reference to a C# project.

When I refer to one of my own native DLLs, I usually either add a dependency between the C# project and the project which generates the native DLL, or I add the DLL as a linked content file in the C# project, like so:

  1. Right-click on the project and choose Add > Existing Item.
  2. Browse and select the DLL you want, but don't click Add yet.
  3. Click on the tiny arrow at the right of the Add button and select Add As Link.
  4. Select the DLL that now appears in your C# project and go to its properties.
  5. Make sure you have Build Action set to Content.

This will copy the DLL to the bin\Debug folder of the C# project and make sure that, if you once decide to create a Setup project, you can easily reference all content files and include them in the Microsoft Installer package.

Now, to be able to see the functions written in your native DLL, you have to take care of exporting them (see Exporting C Functions for Use in C or C++ Language Executables and Exporting from a DLL Using __declspec(dllexport)). So you'd have to add an extern "C" block around your function declaration (I am assuming you wrote your code in a .cpp source file and this means that the compiler will emit mangled function names if you do not declare them as being extern "C"):

extern "C"
{
    __declspec (dllexport) void __cdecl Foo(const char* arg1);
}

...

void Foo(const char* arg1)
{
    printf ("Hello %s !", arg1);
}

The __declspec (dllexport) decoration means that the compiler/linker will have to make the function visible from outside of the DLL. And the __cdecl defines how parameters will be passed to the function (the standard "C" way of doing this).

In your C# code, you'll have to refer to your DLL's exported methods:

class Program
{
    [DllImport("mydll.dll")]
    internal static extern void Foo(string arg1);

    static void Main()
    {
        Program.Foo ("Pierre");
    }
}

You should read the Platform Invoke Tutorial which gives all the gory details.

like image 141
Pierre Arnaud Avatar answered Oct 10 '22 04:10

Pierre Arnaud