search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

code blocks 撰寫DLL和動態載入使用 – jashliao部落格

code blocks 撰寫DLL和動態載入使用


 

資料來源:

         01.https://helloacm.com/tutorial-create-a-sample-dll-project-using-codeblocks-ide-in-cc/

         02.http://www.infernodevelopment.com/simple-c-dll-loading-message-box


GITHUB專案檔:https://github.com/jash-git/code-blocks-DLL-

 


DLL_H

#ifndef __MAIN_H__
#define __MAIN_H__

#include

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern “C”
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
 

DLL_C

#include “main.h”
//資料來源:https://helloacm.com/tutorial-create-a-sample-dll-project-using-codeblocks-ide-in-cc/
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, “DLL Message”, MB_OK | MB_ICONINFORMATION);
}

extern “C” DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

 

EXE_C

#include
//–
//http://www.infernodevelopment.com/simple-c-dll-loading-message-box
#include
typedef  void (*FuncPtr)(LPCTSTR);
//–
using namespace std;

int main()
{
    cout << “Hello world!” << endl;
    FuncPtr myDllFunc;
    BOOL linkSuccessFlag = FALSE, fFreeResult;
    HINSTANCE hinstLib = LoadLibrary(“CB_DLL.dll”);

    if (hinstLib != NULL) {
        myDllFunc = (FuncPtr) GetProcAddress(hinstLib, “SomeFunction”);

        if (myDllFunc != NULL)
        {
            linkSuccessFlag = TRUE;
            myDllFunc(“Hello, World! by jash”);
        }
        fFreeResult = FreeLibrary(hinstLib);
    }
    system(“pause”);
    return 0;
}

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦