Click here to Skip to main content
12,622,652 members (29,292 online)
Click here to Skip to main content
Add your own
alternative version


119 bookmarked

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

, 13 Mar 2005
Rate this:
Please Sign up or sign in to vote.
An easy-to-use example of how to create a DLL library in C and then use it with C#.


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:

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

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Joe DF28-Apr-13 10:13
memberJoe DF28-Apr-13 10:13 
QuestionGreat! Pin
Joe DF24-Apr-13 20:02
memberJoe DF24-Apr-13 20:02 
QuestionSimplest Method Pin
sourabhdev141-Mar-13 9:40
membersourabhdev141-Mar-13 9:40 
AnswerRe: Simplest Method Pin
Jibesh1-Mar-13 10:25
memberJibesh1-Mar-13 10:25 
The link provided by you is no were related to this article. This article says how to use a C/C++ dll in C# and your link says C# dll to a C# application. out of topic link. I down vote your comment. sorry.

modified 1-Mar-13 15:31pm.

GeneralMy vote of 5 Pin
raj23karthik19-Oct-12 20:35
memberraj23karthik19-Oct-12 20:35 
GeneralMy vote of 5 Pin
Member 200288416-Oct-12 23:06
memberMember 200288416-Oct-12 23:06 
Questionopencv c program run in c# Pin
sampathbc1231-Aug-12 20:17
membersampathbc1231-Aug-12 20:17 
GeneralMy vote of 5 Pin
Menon Santosh27-Jul-12 4:09
memberMenon Santosh27-Jul-12 4:09 
GeneralMy vote of 5 Pin
AVardhan16-May-12 6:04
memberAVardhan16-May-12 6:04 
GeneralMy vote of 5 Pin
5cw24-Apr-12 20:15
member5cw24-Apr-12 20:15 
QuestionHoe to get return from parameter of VC++ function Pin
Ramveer Singh from Gurgaon, Haryana13-Apr-12 1:35
memberRamveer Singh from Gurgaon, Haryana13-Apr-12 1:35 
AnswerRe: Hoe to get return from parameter of VC++ function Pin
Member 106523729-Mar-14 17:49
memberMember 106523729-Mar-14 17:49 
GeneralMy vote of 4 Pin
Pradeep Kumar G M20-Mar-12 22:57
memberPradeep Kumar G M20-Mar-12 22:57 
QuestionWe need to create our project from scratch, right? Pin
Sam Hobbs8-Feb-12 14:29
memberSam Hobbs8-Feb-12 14:29 
QuestionThanks Pin
Jayawant Karale29-Nov-11 20:11
memberJayawant Karale29-Nov-11 20:11 
GeneralMy vote of 5 Pin
weifirst3-Aug-11 22:03
memberweifirst3-Aug-11 22:03 
Questioni want to call my function inside the dll and from my application Pin
Member 77180056-Jul-11 19:48
memberMember 77180056-Jul-11 19:48 
Questioni want to call my function inside the dll and from my application Pin
Member 77180056-Jul-11 19:47
memberMember 77180056-Jul-11 19:47 
GeneralMy vote of 5 Pin
shynet22-Apr-11 11:24
membershynet22-Apr-11 11:24 
GeneralMy vote of 5 Pin
Martin Hofmann19-Jan-11 5:33
memberMartin Hofmann19-Jan-11 5:33 
GeneralMy vote of 5 Pin
ir17528-Oct-10 9:04
memberir17528-Oct-10 9:04 
GeneralMy vote of 5 Pin
Somanth csad17-Aug-10 0:59
memberSomanth csad17-Aug-10 0:59 
Generalhi.. this dint work for me. [modified] Pin
aju89628-Jul-10 3:09
memberaju89628-Jul-10 3:09 
GeneralRe: hi.. this dint work for me. [modified] Pin
DaveGordon16-Aug-13 6:26
groupDaveGordon16-Aug-13 6:26 
GeneralThank you Pin
Omar Gamil22-Jun-10 0:56
memberOmar Gamil22-Jun-10 0:56 
GeneralGreat Job Pin
Abdulkarim Zrik7-Jun-10 23:20
memberAbdulkarim Zrik7-Jun-10 23:20 
Generalhi brother Pin
Gogu Ologu1-Jun-10 14:39
memberGogu Ologu1-Jun-10 14:39 
GeneralMy vote of 2 Pin
firas_hamzeh13-May-10 4:57
memberfiras_hamzeh13-May-10 4:57 
GeneralRe: My vote of 2 Pin
Jeremy Jones32123-Jun-10 9:49
memberJeremy Jones32123-Jun-10 9:49 
GeneralRe: My vote of 2 Pin
firas_hamzeh23-Jun-10 11:04
memberfiras_hamzeh23-Jun-10 11:04 
GeneralRe: My vote of 2 Pin
DaveGordon16-Aug-13 6:27
groupDaveGordon16-Aug-13 6:27 
Generalgreat article and thanks Pin
mickyzhang12-Jan-10 7:30
membermickyzhang12-Jan-10 7:30 
General"An attempt was made to load a program with an incorrect format" [modified] Pin
lonik.r21-Jul-09 23:50
memberlonik.r21-Jul-09 23:50 
GeneralRe: "An attempt was made to load a program with an incorrect format" Pin
gentry_joey13-Dec-09 9:29
membergentry_joey13-Dec-09 9: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 2:14
memberMember 377636727-Nov-08 2:14 
QuestionDo you Think it is working ?? Pin
Member 339892412-Oct-08 21:15
memberMember 339892412-Oct-08 21:15 
AnswerRe: Do you Think it is working ?? Pin
Jonghoon Han19-Nov-08 6:34
memberJonghoon Han19-Nov-08 6:34 
QuestionHow Do i Create a MyLib Pin
vijaymunnoli12-Jun-08 1:09
membervijaymunnoli12-Jun-08 1:09 
QuestionWhy doesn't this work with Visual Studio 2005 Pin
canhestros3-Apr-08 4:44
membercanhestros3-Apr-08 4:44 
GeneralRuntime exception while loading dll [modified] Pin
parazitu3-Nov-07 3:52
memberparazitu3-Nov-07 3:52 
GeneralReally helped me. Pin
ramachandragowda2-Oct-07 6:31
memberramachandragowda2-Oct-07 6:31 
GeneralNice code Pin
bijulsoni16-Aug-07 14:23
memberbijulsoni16-Aug-07 14:23 
Questionwhat about using classes Pin
soorajvs200513-Aug-07 1:14
membersoorajvs200513-Aug-07 1:14 
QuestionDoes not work on other pc Pin
Rafan31-May-07 12:59
memberRafan31-May-07 12:59 
AnswerRe: Does not work on other pc [modified] Pin
Michael900028-Jun-08 14:18
memberMichael900028-Jun-08 14:18 
GeneralEmbedded Resource in .NET Pin
tanveerakl4-Jul-06 0:48
membertanveerakl4-Jul-06 0:48 
Generalreleasing resources Pin
fatih isikhan5-Dec-05 4:08
memberfatih isikhan5-Dec-05 4:08 
QuestionUsing strings parameters in C++ DLL Pin
BillMcNeil1-Dec-05 9:05
memberBillMcNeil1-Dec-05 9:05 
Generalextern not necessary Pin
Mark Zudeck27-Oct-05 9:55
memberMark Zudeck27-Oct-05 9:55 
GeneralNice article ! Pin
ddmcr17-Oct-05 11:01
memberddmcr17-Oct-05 11:01 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 14 Mar 2005
Article Copyright 2005 by Vladimir Tskhvaradze
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid