« 資格試験対策研修にて | トップページ | 京都南座 坂東玉三郎 中国・昆劇合同公演 »

VC++6.0でDLLロードにLoadLibrary APIを使う

先日のエントリの続き。LoadLibrary APIを使用した、明示的なDLLのロード。

LoadLibrary APIを使う方法はGetProcAddressで関数ポインタを取得する必要がある。慣れていないと、この処理で少し手間取るだろう。Free処理も忘れずに。

hello.exe

hello.cpp

#include

#include

#include "Hellodll.h"

void main()
{
HINSTANCE hDll;
const char* (*Hello)(void);

hDll = LoadLibraryEx( "hellodll", NULL, 0 );
if ( hDll == NULL ) {
fprintf( stdout, "Can't load the DLL !!!\n" );
exit(1);
}

Hello = (const char*(*)(void))GetProcAddress( hDll, "Hello" );
if ( Hello == NULL ) {
fprintf( stdout, "Hello function not in hellodll.dll !!!\n" ) ;
exit(1);
}

printf("%s", Hello());

FreeLibrary(hDll);
}

hellodll.dll

hellodll.cpp

#include "Hellodll.h"

extern "C" int APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpRsv)
{
return TRUE;
}

HELLODLL_API const char* __stdcall Hello()
{
return "Hello World!\n";
}

hellodll.h

#ifndef __Hellodll_H #define __Hellodll_H

#ifdef HELLODLL_EXPORTS
#define HELLODLL_API __declspec(dllexport)
#else
#define HELLODLL_API __declspec(dllimport)
#endif

#include

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

HELLODLL_API const char* __stdcall Hello();

#ifdef __cplusplus
}
#endif // __cplusplus

#endif //__Hellodll_H


|

« 資格試験対策研修にて | トップページ | 京都南座 坂東玉三郎 中国・昆劇合同公演 »

パソコン・インターネット」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)


コメントは記事投稿者が公開するまで表示されません。



トラックバック

この記事のトラックバックURL:
http://app.cocolog-nifty.com/t/trackback/127316/40533362

この記事へのトラックバック一覧です: VC++6.0でDLLロードにLoadLibrary APIを使う:

« 資格試験対策研修にて | トップページ | 京都南座 坂東玉三郎 中国・昆劇合同公演 »