DLL导出

来源:互联网 发布:mysql恢复删除的数据 编辑:程序博客网 时间:2024/06/05 16:43

DLL导出

DllMain 类似于main函数或者winmain等入口函数,当加载、卸载、线程启动、线程终止时会调用,可在此申请资源或清理资源等。

DLL可以配合头文件和lib使用,或者使用LoadLibrary+GetProcAddress动态加载。

代码:
DLL:
dllmain.cpp 生成DLL项目时会自动生成.

#include "stdafx.h"BOOL APIENTRY DllMain( HMODULE hModule,                       DWORD  ul_reason_for_call,                       LPVOID lpReserved                     ){    switch (ul_reason_for_call)    {    case DLL_PROCESS_ATTACH:        OutputDebugStringA("DLL_PROCESS_ATTACH");        break;    case DLL_THREAD_ATTACH:        OutputDebugStringA("DLL_THREAD_ATTACH");        break;    case DLL_THREAD_DETACH:        OutputDebugStringA("DLL_THREAD_DETACH");        break;    case DLL_PROCESS_DETACH:        OutputDebugStringA("DLL_PROCESS_DETACH");        break;    }    return TRUE;}

head.h 导出一个函数和一个类

#pragma once#define _DLL_EXPORTS#ifdef _DLL_EXPORTS#define MYDLL_API __declspec(dllexport)#else#define MYDLL_API __declspec(dllimport)#endifMYDLL_API void MsgBox(const wchar_t*title, const wchar_t*content);class MYDLL_API testClass{public:    void outputMessage(const wchar_t*msg);};

sourse.cpp

#include "stdafx.h"#include "head.h" void MsgBox(const wchar_t*title, const wchar_t*content){    MessageBox(NULL, title, content, MB_OK);} void testClass::outputMessage(const wchar_t*msg) {     OutputDebugString(msg); }

测试代码:

#include "stdafx.h"#include "../../TestDll/TestDll/head.h"#pragma comment(lib,"../Debug/TestDll.lib")int main(){    MsgBox(_T("title"), _T("content"));    testClass A;    A.outputMessage(_T("aaaaaaaa"));    return 0;}

代码链接:
http://download.csdn.net/detail/yangyang031213/9900474

原创粉丝点击