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

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

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 <windows.h>

/*  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 <iostream>
//–
//http://www.infernodevelopment.com/simple-c-dll-loading-message-box
#include<windows.h>
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;
}

 

 

 

2 thoughts on “code blocks 撰寫DLL和動態載入使用

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *