How to Create a dll library in C and then use it with C #

Source: Internet
Author: User

Http://www.codeproject.com/csharp/UseCDLLlibinCS.asp

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 shoshould 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)VoidDisplayhellofromdll () {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:

[Dllimport (<SPAN class = "CPP-string"> "testlib. DLL "</span>)] <SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> static </span> <SPAN class = "CS-keyword"> extern </span> <SPAN class = "CS-keyword"> void </span> displayhellofromdll ();

In C #, keywordExternIndicates that the method is implemented externally.

Your code shocould 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 thatSystem. runtime. interopservicesIs required for operations with the DLL.

According to msdn:

"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 previusly built DLL intoDebug/ReleaseDirectory of the current application. The DLL shocould be in the same directory as your main application.

Now start an application. If everything you 've made was correct, you shoshould get something like this:

 

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

About Vladimir tskhvaradze
Hi,

my name is Vladimir. i'm 23.
I graduated from Tbilisi State University with BS degree in physics.

(it's wrong to think that physicists aren't good enough for software development and programming... it's a common misconception, right ?)

I'm brain1_c and brain1_c ++ fundamentals certified programmer.
my interests range from physics to programming and electronics. currently, I work on microchip PIC applications, where as you may know, programming is a necessity.

if you have any questions or comments feel free to contact me:
vova_ts@hotmail.com

click here to view Vladimir tskhvaradze's online profile.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.