C + + Create Win32 DLL Diagram (VS2010) __c++

Source: Internet
Author: User

first, the creation of a DLL
Create Project: Win32->win32 project, Name: Mydll


Select DLL (D)-> complete.

1. New header file testdll.h
The testdll.h code is as follows:

1 2 3 4 5 6 7 8 9 10 11 12-13 #ifndef testdll_h_ #define TESTDLL_H_ #ifdef mylibdll #define mylibdll extern "C" _declspec (dllimport) #else #define Myli bdll extern "C" _declspec (dllexport) #endif mylibdll int Add (int plus1, int plus2); Can also write like this://extern "C" {//_declspec (dllexport) int Add (int plus1, int plus2); #endif



2. New source file Testdll.cpp
The Testdll.cpp code is as follows:

1 2 3 4 5 6 7 8 9 #include "stdafx.h" #include "testdll.h" #include <iostream> using namespace std; int Add (int plus1, int plus2) {int add_result = Plus1 + plus2; return add_result;}



3. New module definition file mydll.def
The MYDLL.DEF code is as follows:

1 2 3 LIBRARY "Mydll" exports Add @1




4. VS2010 automatically creates the Dllmain.cpp file, which defines the entry point of the DLL application.

The Dllmain.cpp code is as follows:
Dllmain.cpp: Defines the entry point for a DLL application.

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 #include "stdafx.h" BOOL apientry DllMain (hmodule hmodule, DWORD Ul_reason_for_call, LPVOID lpreserved) {switch (ul_reason_for_call) {case Dll_process_attach:case dll_thread_a Ttach:case dll_thread_detach:case Dll_process_detach:break; return TRUE; }


Finally, the compilation generates MyDLL.dll files and MyDLL.lib files.

1>------Started Build: Project: Mydll, configuration: Debug Win32------

1> Dllmain.cpp

========== Generation: 1 success, 0 failures, latest 0, skipping 0 ==========

1>------Started Build: Project: Mydll, configuration: Debug Win32------

1> Stdafx.cpp

1> Testdll.cpp

1> MyDLL.cpp

1> is generating code ...

1> is creating a library D:\Visual c++\ project \libaray\mydll\debug\mydll.lib and object D:\Visual c++\ engineering \libaray\mydll\debug


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.