DLL Beginner's Guide (Non-MFC)

Source: Internet
Author: User
Tags define definition header win32

Environment: Visual C + + 2003, Windows

Source: DLL Project's source code and test project

I am learning DLLs, not to have any farsighted opinion of it; This article is just coding to let you see and wonder how the code works. In this article, I assume you know how to use your compiler features, such as setting directory paths, and so on.

To establish the project, select the Win32 console project (WIN32 Console application) and select the DLL and empty project options on the Application Settings tab (the Advanced tab). DLLs may not be as difficult as you think. First write your header (header file), called DLLTutorial.h. This file is just like any other header file, which is a prototype of some functions.

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
    DECLDIR int Add( int a, int b );
    DECLDIR void Function( void );
}
#endif

The previous two lines indicate that the compiler contains only this file once. extern "C" tells the compiler that this section can be used in C + +.

In VC + + Here are two ways to export functions:

1, using __declspec, a Microsoft-defined keyword.

2, create a module definition file (module-definition file that is. DEF). The first method is slightly simpler than the second method, but both work well.

__declspec (dllexport) exports the function symbol to a storage class in your DLL. I define DECLDIR to run this function when the following line is defined.

#define DLL_EXPORT

Also import functions if the following line

#define DLL_EXPORT

is not present in the source file. In this case, you will export the function add (int a, int b) and functions ().

Now you need to write a source file that will be called DLLTutorial.cpp.

#include <iostream>
#include "DLL_Tutorial.h"
#define DLL_EXPORT
extern "C"
{
    DECLDIR int Add( int a, int b )
    {
    return( a + b );
    }
    DECLDIR void Function( void )
    {
    std::cout << "DLL Called!" << std::endl;
    }
}

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.