![]() |
Languages »
C# »
How To
Intermediate
How to create a DLL library in C and then use it with C#By Vladimir TskhvaradzeAn easy-to-use example of how to create a DLL library in C and then use it with C#. |
C++, C#.NET1.1, WinXP, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

After spending some time trying to implement this simple task, I started to search similar code examples over the Internet. I was really very surprised when found that all examples were slightly different from what I needed. Finally, I realized that there is no (at least spending 30 min in the net) easy-to-use example, that�s why I decided to write this article.
Assuming that you already know what a DLL is, let's begin with creating a simple one.
OK, now we should attach an empty source file to our blank project.
In the opened window, enter the following code:
#include <stdio.h> extern "C" { __declspec(dllexport) void DisplayHelloFromDLL() { printf ("Hello from DLL !\n"); } }
Please note that __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.
extern �C� (with brackets) is also very important, it shows that all code within brackets is available from �outside�. Although code will compile even without this statement, during runtime, you�ll get a very unpleasant error. So, do not forget to include it.
Build this application and your DLL will be ready-to-use.
Now it�s time to create an application which will use our DLL, as the main title explains. We will create this type of application using Microsoft�s C#.
Into the specified class, insert the following two lines:
[DllImport("TestLib.dll")]
public static extern void DisplayHelloFromDLL ();
In C#, keyword extern indicates that the method is implemented externally.
Your code should look something like this:
using System;
using System.Runtime.InteropServices; // DLL support
class HelloWorld
{
[DllImport("TestLib.dll")]
public static extern void DisplayHelloFromDLL ();
static void Main ()
{
Console.WriteLine ("This is C# program");
DisplayHelloFromDLL ();
}
}
Please, note that System.Runtime.InteropServices is required for operations with the DLL.
According to MSDN:
�The
System.Runtime.InteropServicesnamespace provides a collection of classes useful for accessing COM objects, and native APIs from .NET�
OK, now build this application, and then copy the previously built DLL into the Debug/Release directory of the current application. The DLL should be in the same directory as your main application.
Now start an application. If everything you�ve made was correct, you should get something like this:

That�s all, really simple isn�t it?
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 13 Mar 2005 Editor: Smitha Vijayan |
Copyright 2005 by Vladimir Tskhvaradze Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |