Win32 DLL的创建和使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ClamReason/article/details/84072319

Win32 DLL

0 创建Win32 DLL 项目

VS新建》项目》Win32项目》确定》下一步》DLL、导出符号》完成

1 导出头文件 A.h

#ifdef CHESS_LIB_EXPORTS  
#define CHESS_LIB_API __declspec(dllexport)   
#else  
#define CHESS_LIB_API __declspec(dllimport)   
#endif 

extern "C"  CHESS_LIB_API  int ExportedFunction(int depth);

2 项目属性增加CHESS_LIB_EXPORTS  的定义

3 源文件 A.cpp

CHESS_LIB_API int ExportedFunction(int depth)
{
    // 此处为普通函数体
    return 42;
}
4 引用DLL中的函数

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str = "ChessEngine.dll";
    HMODULE libraryHandle = ::LoadLibrary(str.c_str());

    if (libraryHandle != NULL)
    {
        typedef int (*EXECUTOR_FUNC)(int depth);//注意这里的写法

        EXECUTOR_FUNC function = (EXECUTOR_FUNC)GetProcAddress(libraryHandle, "ExportedFunction");
        char str[] = "123";
        cout<<function(1);
    }
    else
    {
        cout << "can not loadlibrary!" << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/ClamReason/article/details/84072319