LoginSignup
25
35

More than 5 years have passed since last update.

VC++ DLLの呼び出しと解放

Posted at

はじめに

最近、仕事で久しぶりにVC++を使いました。
何とかなると思っていましたが、何とかなりませんでした。。。

DLLの呼び出しと解放についての備忘録を残します。

サンプル

DLL側

plus.cpp
extern "C" __declspec(dllexport) int plus(int x, int y)
{
    return (x + y);
}

DLLの呼び出し

呼び出し側

test.cpp
typedef int (*FUNC)(int x, int y);
  :
  :

まず、DLL内の関数の型を宣言します。(*FUNC)は任意の文字列でかまいません。

DLL取得

HMODULE hModule = LoadLibrary((LPCSTR)lpBuffer);
if (NULL == hModule)
{
    return 1;
}

lpBuffer変数は、DLLのパスを指定します。

関数アドレスの取得

FUNC func = (FUNC)GetProcAddress(hModule, "plus");
if (NULL == func)
{
    return 2;
}

関数の実行

int ret;

ret = func(1, 2);
printf("答えは %d\n", ret);

DLLの解放

FreeLibrary(hModule);

おわりに

なんてことはないことなのですが、しばらく使っていないと忘れていました。
単純なことほど忘れやすいのかもしれません。

25
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
35