1. C++でライブラリーを作る Linux編
2. pythonからC++のライブラリーを利用する Linux編
3. C++でライブラリー(DLL)を作る Windows編
4. pythonからC++のライブラリー(DLL)を利用する Windows編
5. C#からC++のライブラリー(DLL)を利用する
6. python・C#とC++間で複素数を受け渡す
7. C(C++)からライブラリー(DLL)を利用する 続編
8. pythonからC++のライブラリー(DLL)を利用する 続編
概要
前回( Windows Linux )はビルド時にライブラリー(DLL)を指定する方法でした。 ライブラリー(DLL)は、プログラムの開始・終了時に、自動的にロード・アンロードされます。
今回はソースコード内で明示的にライブラリー(DLL)をロード・アンロードします。
なおライブラリー(DLL)は前回( Windows Linux )と同じものを使用します。
Windowsの場合
DLLのロードは LoadLibrary で、アンロードは FreeLibrary で行います。
インポート関数のアドレスは GetProcAddress で得ることが出来ます。
今回はインポート関数と同じ関数型のポインターを作成し、 GetProcAddress 戻り値のコピー先にしました。
インポートライブラリーは必要ありません。
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#pragma warning(disable:4996)
typedef struct {
double dInA;
double dInB;
double dOut;
char sData[64];
} tagLibParameter;
typedef int (*defSum)(int , int );
typedef int (*defCalcX10)(double * , double * , int );
typedef int (*defTxRxMessage)(char * , char * , int );
typedef int (*defInOutStr)(tagLibParameter * );
typedef struct {
HINSTANCE pLib;
defSum LLibSum;
defCalcX10 LLibCalcX10;
defTxRxMessage LLibTxRxMessage;
defInOutStr LLibInOutStr;
} tagGlobalParameter;
tagGlobalParameter *GP;
static void LocalLoadLibrary(void);
int main(int argc, char *argv[])
{
//////////////////////////////////////////////////
GP = new tagGlobalParameter;
memset(GP, 0, sizeof(tagGlobalParameter));
LocalLoadLibrary();
//////////////////////////////////////////////////
int nA, nB, nSum;
nA = 1;
nB = 2;
//---------------------------
nSum = (GP->LLibSum)(nA, nB);
//---------------------------
printf("%d + %d = %d\n", nA, nB, nSum);
//================================================================
double dsOut[3], dsIn[3];
int nLen = (sizeof(dsOut)) / (sizeof(double));
dsIn[0] = 1.1;
dsIn[1] = 2.2;
dsIn[2] = 3.3;
//----------------------------------------------------------------
(GP->LLibCalcX10)(dsOut, dsIn, ((sizeof(dsIn))/(sizeof(double))));
//----------------------------------------------------------------
printf("in[%.1lf %.1lf %.1lf] out[%.1lf %.1lf %.1lf]\n",
(dsIn[0]), (dsIn[1]), (dsIn[2]),
(dsOut[0]), (dsOut[1]), (dsOut[2]) );
//================================================================
char sTxBuf[64], sRxBuf[256];
sprintf(sTxBuf, "App->DLL: Hello !");
//----------------------------------------------------
(GP->LLibTxRxMessage)(sRxBuf, sTxBuf, sizeof(sRxBuf));
//----------------------------------------------------
printf("%s\n", sRxBuf);
//================================================================
tagLibParameter LP;
memset(&LP, 0, sizeof(tagLibParameter));
(LP.dInA) = 1.1;
(LP.dInB) = 2.2;
//----------------------
(GP->LLibInOutStr)(&LP);
//----------------------
printf("%.1lf + %.1lf = %.1lf [%s]\n",
(LP.dInA), (LP.dInB), (LP.dOut), (LP.sData));
//////////////////////////////////////////////////
FreeLibrary(GP->pLib);
delete GP;
//////////////////////////////////////////////////
return(0);
}
static void LocalLoadLibrary(void)
{
(GP->pLib) = LoadLibrary((char *)"LocalLib.dll");
//--------
(GP->LLibSum) = (defSum)GetProcAddress((GP->pLib), "LLibSum");
(GP->LLibCalcX10) = (defCalcX10)GetProcAddress((GP->pLib), "LLibCalcX10");
(GP->LLibTxRxMessage) = (defTxRxMessage)GetProcAddress((GP->pLib), "LLibTxRxMessage");
(GP->LLibInOutStr) = (defInOutStr)GetProcAddress((GP->pLib), "LLibInOutStr");
}
Linuxの場合
関数名以外はWindowsと同じです。
ライブラリーのロードは dlopen で、アンロードは dlclose です。
インポート関数のアドレス取得は dlsym です。
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
typedef struct {
double dInA;
double dInB;
double dOut;
char sData[64];
} tagLibParameter;
typedef int (*defSum)(int , int );
typedef int (*defCalcX10)(double * , double * , int );
typedef int (*defTxRxMessage)(char * , char * , int );
typedef int (*defInOutStr)(tagLibParameter * );
typedef struct {
void *pLib;
defSum LLibSum;
defCalcX10 LLibCalcX10;
defTxRxMessage LLibTxRxMessage;
defInOutStr LLibInOutStr;
} tagGlobalParameter;
tagGlobalParameter *GP;
static void LocalLoadLibrary(void);
int main(int argc, char *argv[])
{
//////////////////////////////////////////////////
GP = new tagGlobalParameter;
memset(GP, 0, sizeof(tagGlobalParameter));
LocalLoadLibrary();
//////////////////////////////////////////////////
int nA, nB, nSum;
nA = 1;
nB = 2;
//---------------------------
nSum = (GP->LLibSum)(nA, nB);
//---------------------------
printf("%d + %d = %d\n", nA, nB, nSum);
//================================================================
double dsOut[3], dsIn[3];
int nLen = (sizeof(dsOut)) / (sizeof(double));
dsIn[0] = 1.1;
dsIn[1] = 2.2;
dsIn[2] = 3.3;
//----------------------------------------------------------------
(GP->LLibCalcX10)(dsOut, dsIn, ((sizeof(dsIn))/(sizeof(double))));
//----------------------------------------------------------------
printf("in[%.1lf %.1lf %.1lf] out[%.1lf %.1lf %.1lf]\n",
(dsIn[0]), (dsIn[1]), (dsIn[2]),
(dsOut[0]), (dsOut[1]), (dsOut[2]) );
//================================================================
char sTxBuf[64], sRxBuf[256];
sprintf(sTxBuf, "App->DLL: Hello !");
//----------------------------------------------------
(GP->LLibTxRxMessage)(sRxBuf, sTxBuf, sizeof(sRxBuf));
//----------------------------------------------------
printf("%s\n", sRxBuf);
//================================================================
tagLibParameter LP;
memset(&LP, 0, sizeof(tagLibParameter));
(LP.dInA) = 1.1;
(LP.dInB) = 2.2;
//----------------------
(GP->LLibInOutStr)(&LP);
//----------------------
printf("%.1lf + %.1lf = %.1lf [%s]\n",
(LP.dInA), (LP.dInB), (LP.dOut), (LP.sData));
//////////////////////////////////////////////////
dlclose(GP->pLib);
delete GP;
//////////////////////////////////////////////////
return(0);
}
static void LocalLoadLibrary(void)
{
(GP->pLib) = dlopen("./libLocalLib.so", RTLD_LAZY);
//--------
(GP->LLibSum) = (defSum)dlsym((GP->pLib), "LLibSum");
(GP->LLibCalcX10) = (defCalcX10)dlsym((GP->pLib), "LLibCalcX10");
(GP->LLibTxRxMessage) = (defTxRxMessage)dlsym((GP->pLib), "LLibTxRxMessage");
(GP->LLibInOutStr) = (defInOutStr)dlsym((GP->pLib), "LLibInOutStr");
}
ビルドでライブラリーを指定する必要はありません。
ビルドと実行結果は次のようになります。
$ g++ -o test main.cpp
$ ./test
start DLL
1 + 2 = 3
in[1.1 2.2 3.3] out[11.0 22.0 33.0]
DLL->App: Hi ! rcvd[App->DLL: Hello !]
1.1 + 2.2 = 3.3 [2026 09/28 10:54:41 979]
end DLL