Generation and invocation of dynamic libraries for C + + (VS2012) DLLs

Source: Internet
Author: User
Tags win32

DLL dynamic-link library generation:

First open VS2012, new--Project--WIN32 console Application (project name: ConsoleApplication1)--Empty Project

Create a new source file Source.cpp

First define the function with the console EXE configuration type and debug it with the main function

You can then pass the project--Properties--Configuration Properties--general--project defaults--under Configuration type

Select the dynamic library (. dll) option

Then click Build to generate a DLL file (ConsoleApplication1.dll) in the Debug directory of the project

extern "C"_declspec (dllexport)voidmyfun (); #include"iostream"using namespacestd;voidmyfun () {cout<<"Hello World"<<Endl;}intMain () {myfun (); return 0;}

VS2012 DLL dynamic-link library invocation

First build a Win32 console empty project, build a source file yourself Source.cpp

Add header file include<windows.h>

Defines a function pointer type typedef void (*dllfun) (Note the return value type, the same as the parameter order type)

And then through HINSTANCE hdll; Create an instance handle

hDLL = LoadLibrary ("Xxxx.dll"); Loading the dynamic library

" here the string type is LPSTR, when it is the Unicode character set, it will not work, so in the project---property--General---The default character set:" Unicode "to support multi-character extension can "

Then define a function address variable dllfun myFun1;

MYFUN1 = (dllfun) GetProcAddress (hDLL, "myfun"); Gets the entry address of the Myfun function inside the dynamic library

Then MyFun1 ();

Finally release memory

Freeliabrary (hDLL);

#include"iostream"#include"windows.h"#include"time.h"using namespaceStd;typedefvoid(*Dllfun) ();intMain () {hinstance hdll; hDLL= LoadLibrary ("E:\\c++dll_lib\\consoleapplication1\\debug\\consoleapplication1.dll"); if(hDLL = =NULL)        {FreeLibrary (hdll); cout<<"Dll loaded is fail ..."<<Endl;        GetChar (); return-1;    } Dllfun myFun1; MyFun1= (Dllfun) GetProcAddress (hDLL,"Myfun"); if(MyFun1 = =NULL)        {FreeLibrary (hdll); cout<<"dlladdress loaded is fail ..."<<Endl;        GetChar (); return-1;    } myFun1 ();    FreeLibrary (hDLL); return 0;}

Generation and invocation of dynamic libraries for C + + (VS2012) DLLs

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.