Click here to Skip to main content
15,029,464 members
Articles / Programming Languages / C#
Posted 13 Mar 2005


132 bookmarked

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.64/5 (90 votes)
13 Mar 20052 min read
An easy-to-use example of how to create a DLL library in C and then use it with C#.

Image 1


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:


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
    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?


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


About the Author

Vladimir Tskhvaradze
United States United States
No Biography provided

Comments and Discussions

Questionshort & sweet Pin
Member 1474882420-Feb-20 0:40
MemberMember 1474882420-Feb-20 0:40 
QuestionWhat about a 3rd party C library binary? Pin
oniDino3-May-19 13:09
MemberoniDino3-May-19 13:09 
PraiseExcellent! Pin
Member 128897715-Sep-18 1:46
MemberMember 128897715-Sep-18 1:46 
QuestionAlready have .dll in C language .How to add this into WPF C# project in Visual Studio 2017? Pin
Neha Soni1-Aug-18 23:57
MemberNeha Soni1-Aug-18 23:57 
QuestionGreat One! Pin
Member 1322467424-Jun-18 18:01
MemberMember 1322467424-Jun-18 18:01 
Questionc# form applications Pin
saeed ghasemi3-Dec-17 3:54
Membersaeed ghasemi3-Dec-17 3:54 
QuestionGreat Article Pin
Jayawant Karale2-Nov-16 23:51
MemberJayawant Karale2-Nov-16 23:51 
Questionuse function to return string Pin
Member 1040463721-Feb-16 8:21
MemberMember 1040463721-Feb-16 8:21 
GeneralRe: use function to return string Pin
PIEBALDconsult21-Feb-16 8:43
professionalPIEBALDconsult21-Feb-16 8:43 
Generalthanks so much for this! Pin
4mla1fn23-Jul-15 18:18
Member4mla1fn23-Jul-15 18:18 
Bugdll is not found error (first project dll) Pin
er. Mahesh Nagar18-Dec-14 2:38
Memberer. Mahesh Nagar18-Dec-14 2:38 
Questiongot exception,help Pin
UtillYou17-Nov-14 21:35
MemberUtillYou17-Nov-14 21:35 
AnswerRe: got exception,help Pin
Leon Sandler26-Sep-17 9:08
MemberLeon Sandler26-Sep-17 9:08 
AnswerRe: got exception,help Pin
Leon Sandler26-Sep-17 9:09
MemberLeon Sandler26-Sep-17 9:09 
Questiondll in c# folder Pin
javadtaghia12-Oct-13 6:20
Memberjavadtaghia12-Oct-13 6:20 
AnswerRe: dll in c# folder Pin
Jason Newland27-Dec-16 11:26
MemberJason Newland27-Dec-16 11:26 
GeneralAgradecimento Pin
Member 1005714329-Aug-13 4:52
MemberMember 1005714329-Aug-13 4:52 
QuestionUnable to start program Pin
Jeetha P J29-Aug-13 1:39
MemberJeetha P J29-Aug-13 1:39 
AnswerRe: Unable to start program Pin
Jason Newland27-Dec-16 11:28
MemberJason Newland27-Dec-16 11:28 
GeneralMy vote of 5 Pin
So6_White11-Jun-13 15:04
MemberSo6_White11-Jun-13 15:04 
GeneralMy vote of 5 Pin
Joe DF28-Apr-13 9:13
MemberJoe DF28-Apr-13 9:13 
QuestionGreat! Pin
Joe DF24-Apr-13 19:02
MemberJoe DF24-Apr-13 19:02 
QuestionSimplest Method Pin
sourabhdev141-Mar-13 8:40
Membersourabhdev141-Mar-13 8:40 
AnswerRe: Simplest Method Pin
Jibesh1-Mar-13 9:25
professionalJibesh1-Mar-13 9:25 
GeneralMy vote of 5 Pin
karthik cad/cam19-Oct-12 19:35
Memberkarthik cad/cam19-Oct-12 19:35 

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.