Click here to Skip to main content
15,868,164 members
Articles / Programming Languages / C#
Article

How to create a DLL library in C and then use it with C#

Rate me:
Please Sign up or sign in to vote.
4.66/5 (92 votes)
13 Mar 20052 min read 672K   9.6K   133   90
An easy-to-use example of how to create a DLL library in C and then use it with C#.

Image 1

Introduction

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.

  1. Start Visual Studio .NET.
  2. Go to File->New->Project.
  3. Select Visual C++ Project, and from the “Templates”, select “Win32 Project”.
  4. Give the name to your project. This will be the name of your final DLL (in my case: TestLib).
  5. Press OK.
  6. Select DLL from “Application Type” (“Application Settings” tab).
  7. Check “Empty Project” (we need to create our project from scratch, right?), and press Finish.

OK, now we should attach an empty source file to our blank project.

  1. Start Solution Explorer (if it’s not displayed).
  2. Right click to the “Source Files”, Add->Add New Item then select “C++ File” and give the name to it.
  3. Press “Open”.

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#.

Creating a simple C# application:

  1. Start Visual Studio .NET.
  2. Go to File->New->Project.
  3. Select Visual C# Project. From the “Templates”, you can either select “Console Application”, or an “Empty Project” just like it was described above.
  4. Give the name to your application.
  5. Press OK.

Into the specified class, insert the following two lines:

C#
[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:

C#
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.InteropServices namespace 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:

Image 2

That’s all, really simple isn’t it?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Other
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Pradeep Kumar G M20-Mar-12 21:57
Pradeep Kumar G M20-Mar-12 21:57 
QuestionWe need to create our project from scratch, right? Pin
Sam Hobbs8-Feb-12 13:29
Sam Hobbs8-Feb-12 13:29 
QuestionThanks Pin
Jayawant Karale29-Nov-11 19:11
Jayawant Karale29-Nov-11 19:11 
GeneralMy vote of 5 Pin
weifirst3-Aug-11 21:03
weifirst3-Aug-11 21:03 
Questioni want to call my function inside the dll and from my application Pin
Mohsin Yousuf 7866-Jul-11 18:48
Mohsin Yousuf 7866-Jul-11 18:48 
Questioni want to call my function inside the dll and from my application Pin
Mohsin Yousuf 7866-Jul-11 18:47
Mohsin Yousuf 7866-Jul-11 18:47 
GeneralMy vote of 5 Pin
shynet22-Apr-11 10:24
shynet22-Apr-11 10:24 
GeneralMy vote of 5 Pin
Martin Hofmann19-Jan-11 4:33
Martin Hofmann19-Jan-11 4:33 
simple, easy to understand, works! Thanx
GeneralMy vote of 5 Pin
ir17528-Oct-10 8:04
ir17528-Oct-10 8:04 
GeneralMy vote of 5 Pin
Somanth csad16-Aug-10 23:59
Somanth csad16-Aug-10 23:59 
Generalhi.. this dint work for me. [modified] Pin
aju89628-Jul-10 2:09
aju89628-Jul-10 2:09 
GeneralRe: hi.. this dint work for me. [modified] Pin
DaveGordon16-Aug-13 5:26
DaveGordon16-Aug-13 5:26 
GeneralThank you Pin
Omar Gameel Salem21-Jun-10 23:56
professionalOmar Gameel Salem21-Jun-10 23:56 
GeneralGreat Job Pin
Abdulkarim Zrik7-Jun-10 22:20
Abdulkarim Zrik7-Jun-10 22:20 
Generalhi brother Pin
Gogu Ologu1-Jun-10 13:39
Gogu Ologu1-Jun-10 13:39 
GeneralMy vote of 2 Pin
firas_hamzeh13-May-10 3:57
firas_hamzeh13-May-10 3:57 
GeneralRe: My vote of 2 Pin
Jeremy Jones32123-Jun-10 8:49
Jeremy Jones32123-Jun-10 8:49 
GeneralRe: My vote of 2 Pin
firas_hamzeh23-Jun-10 10:04
firas_hamzeh23-Jun-10 10:04 
GeneralRe: My vote of 2 Pin
DaveGordon16-Aug-13 5:27
DaveGordon16-Aug-13 5:27 
Generalgreat article and thanks Pin
mickyzhang12-Jan-10 6:30
mickyzhang12-Jan-10 6:30 
General"An attempt was made to load a program with an incorrect format" [modified] Pin
lonik.r21-Jul-09 22:50
lonik.r21-Jul-09 22:50 
GeneralRe: "An attempt was made to load a program with an incorrect format" Pin
valleyman8613-Dec-09 8:29
valleyman8613-Dec-09 8:29 
QuestionHow to use this .dll and .exe in a System, which having only .net frame work, (not Visual Studio) Pin
Member 377636727-Nov-08 1:14
Member 377636727-Nov-08 1:14 
QuestionDo you Think it is working ?? Pin
Member 339892412-Oct-08 20:15
Member 339892412-Oct-08 20:15 
AnswerRe: Do you Think it is working ?? Pin
Jonghoon Han19-Nov-08 5:34
Jonghoon Han19-Nov-08 5:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.