Click here to Skip to main content
13,351,239 members (49,933 online)
Click here to Skip to main content
Add your own
alternative version


128 bookmarked
Posted 13 Mar 2005

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

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 
GeneralRe: releasing resources Pin
Jason Newland27-Dec-16 12:42
memberJason Newland27-Dec-16 12:42 
QuestionUsing strings parameters in C++ DLL Pin
BillMcNeil1-Dec-05 9:05
memberBillMcNeil1-Dec-05 9:05 
GREAT ARTICLE !! You're right - nobody has written a simple example of this. It is MUCH appreciated.

What I'm trying to do is write a simple concatenation routine in a VC++ DLL that can be called from other applications. My code is as follows:

__declspec(dllexport) public void SampleConcat(<br />
   LPCTSTR str1,<br />
   LPCTSTR str2,<br />
   LPTSTR concatStr<br />
   DWORD concatLength) {<br />
<br />
   ???<br />
<br />

I have the following issues:

1. Another aricle in The Code Project ('The Complete Guide to C++ Strings, Part II - String Wrapper Classes') described a means of converting the LPCTSTR variable into a VC++ string variable, but that does not seem to work. Am I making the proper declaration for the function or do I need to have some other means of definning str1 and str2 setup? Is there a simple means of making the type conversion?

3. After I perform the concatenation, is there anything special I need to do in order to pass the value back out?

I'm using VC++ Express, which might also be contributing to the problem. I'm a bit of a green horn Unsure | :~ in C++ so if I'm missing something obvious, then forgive me.

Any help you can supply would be greatly appreciated.

Thanks !!
AnswerRe: Using strings parameters in C++ DLL Pin
Jason Newland27-Dec-16 12:47
memberJason Newland27-Dec-16 12:47 
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 
Generalexport/import a class... Pin
d00_ape20-Jun-05 5:13
memberd00_ape20-Jun-05 5:13 
QuestionCreate Dll library in C and then use it with C#, but can I create Dll Library in C# and then use it with C, VC/C++ ? Pin
LiTaiNien17-Jun-05 19:18
sussLiTaiNien17-Jun-05 19:18 
GeneralMarshalling Pin
pjcrosbie8-Apr-05 18:19
memberpjcrosbie8-Apr-05 18:19 
Questionhow to create reusable library(DLL),using recursive algorithms? Pin
dcvam3-Apr-05 18:39
memberdcvam3-Apr-05 18:39 
GeneralImportant: Handling bool returns Pin
Mark Jerde30-Mar-05 5:59
memberMark Jerde30-Mar-05 5:59 
GeneralC# calling a Cpp dll Pin
SHG28-Mar-05 22:25
memberSHG28-Mar-05 22:25 
Generalgreat article Pin
Nik Vogiatzis27-Mar-05 4:12
memberNik Vogiatzis27-Mar-05 4:12 
GeneralC# dll like C dll Pin
msali26-Mar-05 4:06
membermsali26-Mar-05 4:06 
GeneralRe: C# dll like C dll Pin
Roman Antchik28-Mar-05 13:15
memberRoman Antchik28-Mar-05 13:15 
GeneralRe: C# dll like C dll Pin
msali28-Mar-05 19:57
membermsali28-Mar-05 19:57 
GeneralRe: C# dll like C dll Pin
Roman Antchik28-Mar-05 20:41
memberRoman Antchik28-Mar-05 20:41 
QuestionHow about Strong Names Pin
msali26-Mar-05 4:01
membermsali26-Mar-05 4:01 
AnswerRe: How about Strong Names Pin
Roman Antchik28-Mar-05 20:46
memberRoman Antchik28-Mar-05 20:46 
GeneralCalling function which returns a string from Unicode DLL Pin
NikolaiT25-Mar-05 8:25
memberNikolaiT25-Mar-05 8:25 

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.

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