Two methods of DLL programming export interface

Source: Internet
Author: User

Preface:

There are two ways to get around a DLL in a VC:

The first is to define a def file through a module , put the function interface to be exported, and the DEF file provides the linker with information about the program's everywhere, attributes, and so on.

The second type: Add __declspec (dllexport) to the function declaration;


Example of the first method:

First build a DLL project named: Mousehook_dll:

In the HookLoad.h species declaration two interface functions:

#pragma  oncebool   Starthookev (HWND hwnd); void    stophookev (void);


To implement a function about an interface in HookLoad.cpp:

BOOL   Starthookev (hwnd hwnd)  {  G_hwnd =hwnd;g_keyboradhook = SetWindowsHookEx (Wh_keyboard, Keyboardproc, Getmodulehandlea ("Mousehook_dll"), 0); return FALSE;}  void   Stophookev (void)  {  UnhookWindowsHookEx (g_keyboradhook);}  

In the Mousehook_dll.def:
LIBRARY mousehook_dllexportsstarthookev@2stophookev@3

This makes it possible toThe two function interfaces in Mousehook_dll.dll are exported;


Example of the second method:

First build a DLL project named: Mousehook_dll:

In the HookLoad.h species declaration two interface functions:

#pragma  once#ifndef dll_implement#define  dll_api _declspec (dllexport) #else # define  dll_api _declspec ( dllexport) #endifBOOL   DLL_API Starthookev (HWND hwnd); void    Dll_api stophookev (void);

In the implementation of HookLoad.cpp:

BOOL  DLL_API Starthookev (hwnd hwnd)  {  G_hwnd =hwnd;g_keyboradhook = SetWindowsHookEx (Wh_keyboard, Keyboardproc, Getmodulehandlea ("Mousehook_dll"), 0); return FALSE;}  void  Dll_api stophookev (void)  {  UnhookWindowsHookEx (g_keyboradhook);}

This allows you to export Starthookev and Stophookevtwo functions, this way does not require a module-defined. def file.

Hope is useful to everyone.


Two methods of DLL programming export interface

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.