純C/C++[CodeBlocks] 開發DLL(.a) [CB_DLL_PROJECT]

純C/C++[CodeBlocks] 開發DLL(.a) [CB_DLL_PROJECT]

純C/C++[CodeBlocks] 開發DLL(.a) [CB_DLL_PROJECT]


GITHUB:https://github.com/jash-git/CB_DLL_PROJECT.git


DLL

#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);
double DLL_EXPORT Add(double a, double b);//test_API

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
#include "main.h"
#include "stdio.h"
#define LogFile "BaseLib.log"
static int g_intDebug=1;
// a sample exported function
void WriteLog(const char *Msg,int state)
{
    if(g_intDebug==1)
    {
        FILE *pf='\0';
        if(state==1)
        {
            pf=fopen(LogFile,"w");
        }
        else
        {
            pf=fopen(LogFile,"a");
        }
        fprintf(pf,Msg);
        fprintf(pf, "\n");
        fclose(pf);
    }
}
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
double DLL_EXPORT Add(double a, double b)//test_API
{
    WriteLog("\t Add API...",0);
    return a + b;
}

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
}


DLL_test

#include <iostream>

using namespace std;

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


#ifdef __cplusplus
extern "C"
{
#endif

double DLL_EXPORT Add(double a, double b);//test_API

#ifdef __cplusplus
}
#endif
//---
int main()
{
    cout << "Hello world!" << "\t" <<Add(10,15)<< endl;
    return 0;
}

發表迴響

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