创建Windows DLL(Windows 核心编程 第5版)

  1. 创建头文件
/*************************************************************************
Module: MyLib.h
*************************************************************************/
#ifdef MYLIBAPI
// MYLIBAPI should be defined in all of the DLL's source code modules before this header file is included.
// All functions/variables are being exported
#define MYLIBAPI extern "C" _declspec(dllexport)
#else
// This header file is included by an EXE source code module
// Indicate that all functions/variables are being imported
#define MYLIBAPI extern "C" _declspec(dllimport)
#endif

//Define any data structures and symbols here

//Define exported variables here .(NOTE: Avoid exporting variables.)
MYLIBAPI int g_nResult;


//Define exported function prototypes here.
MYLIBAPI int Add(int nLeft, int nRight);
// End of File /
  1. 创建源文件
/*****************************************************************
Module: MyLibFile1.cpp
******************************************************************/
// Include the standar Windows and C-Runtime header files here.
#include <windows.h>
#include "pch.h"

//This DLL source code file exports functions and variables
#define MYLIBAPI extern "C" _declspce(dllexport)

//Include the exported data structures,symbols,functions,and variable.
#include "MyLib.h" //必须有

// Place the code for this DLL source code file here
//这里不需要MYLIBAPI标识符的原因是编译器在解析头文件的时候会记住应该导出哪些变量或函数
int g_nResult;

int Add(int nLeft, int nRight)
{
    g_nResult = nLeft + nRight;
    return (g_nResult);
}
//End of File
posted @ 2022-04-28 14:13  源世  阅读(51)  评论(0编辑  收藏  举报