Write C-language DLLs in VS2008 environment and downgrade in C + + and C # projects (reprint)

Source: Internet
Author: User
Tags win32

1. Writing DLLs

A) file-open-New project--win32, right Win32 project, fill in the project name, click "Next",

Application type selection: "DLL (D)", additional option: Empty item (E), then finish.

b) Write the header file (edrlib.h):

#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif

EXPORT void Edrcentertexta ();
EXPORT void Edrcentertextw ();
EXPORT int Inccounter ();
#ifdef UNICODE
#define Edrcentertext EDRCENTERTEXTW
#else
#define Edrcentertext Edrcentertexta
#endif

Annotations:

I. Define the __cplusplus representation to be called in a C + + program.

Ii.__declspec (dllexport) represents the way a function is called, which represents the default way to invoke a VS2008 project property. Change method:

Right-click the project-Properties--Configuration Properties--c/c++--Advanced, change the right "calling convention".

c) Write DLL file (edrlib.cpp):

#include "Windows.h"
#include "Edrlib.h"
counter for all applications that call the DLL to share
#pragma data_seg ("shared")
int counter=0;
#pragma COMMENT (linker, "/SECTION:SHARED,RWS")

int WINAPI DllMain (_in_ HANDLE _hdllhandle, _in_ DWORD _reason, _in_opt_ lpvoid _reserved)
{
return TRUE;
}

EXPORT void Edrcentertexta ()
{
MessageBox (Null,text ("Call DLL Function!") "), TEXT (" Assic version "), MB_OK);

}
EXPORT void Edrcentertextw ()
{
MessageBox (Null,text ("Call DLL Function!") "), TEXT (" Unicode Version "), MB_OK);

}

EXPORT int Inccounter ()
{
return ++counter;
}  

c) Compile the build DLL.

2. Calling DLL

Method One,

1. Create a new Win32 application empty project.

2. Copy the generated DLL, lib 2 files to the new directory.

3. Add Import Library: Tools--Project and Solution--vc++ directory, select "Library file" under "Show Directory Below" to add the Lib directory.

4. Header files: edrlib.h (as in writing DLLs)

5. Source file: Main.c

/* #define  cplusplus*/ //c++ call mode    
# include  "Windows.h"    
#include   "Edrlib.h"    
#pragma   Comment (lib, "Edrlib.lib")    
   
Int winapi winmain (__in  hinstance hinstance, __in_opt hinstance hprevinstance, __in_opt lpstr  lpcmdline, __in int nshowcmd )    
{   
   
     tchar buf[32];   
    wsprintf (buf,l "now,counter=%i", Inccounter ());    
    messagebox (null,buf,l "--", MB_OK);    
    edrcentertext ();    
    return 0;    
}  

Explain:
Can be annotated #pragma comment (lib, "TestDLL2.lib") this, but must set up the link library, method:

Right-click the project--Properties--Configuration Properties--linker--input--Additional dependencies: Edrlib.lib

6. Run the test successfully.

"The above content is transferred from: Http://blog.csdn.net/breezes2008/archive/2010/02/25/5326861.aspx"

3. Calling DLLs in C #

This example uses a C program to create a DLL, which is called from a C # program in the next example.


cmdll.c//Compile with:/ldint __declspec (dllexport) samplemethod (int i) {return i*10;}

If it is a Web project, copy the DLL to the bin directory.

If it is a desktop program, copy the DLL to the executable line file directory.

Reference code:

Using system;using System.runtime.interopservices;public class MainClass{[DllImport ("Cmdll.dll")] public static extern int SampleMethod (int x);   static void Main () {Console.WriteLine ("SampleMethod () returns {0}.", SampleMethod (5)); }}



from:http://apps.hi.baidu.com/share/detail/33471191

Write C-language DLLs in VS2008 environment and downgrade in C + + and C # projects (reprint)

Related Article

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.