C & C++/MS VC++
MSVC++로 Dll 파일 제작 - 소스코드 기본틀
DWGoon
2009. 11. 5. 00:45
<소스코드>
<DLL모듈 소스코드>
<실행모듈 소스코드>
참고 : 제프리 리처의 Windows via C/C++ 5ed./한빛미디어/2008/김명신역
// MyDll.h #ifndef DW_MYDLL__H__ #define DW_MYDLL__H__ #if !defined(MYLIBAPI) #define MYLIBAPI extern "C" __declspec(dllimport) #endif // 위 매크로는 dll을 이용하는 소스코드(실행 모듈)에서 적용된다. // dll 제작에 사용되는 소스코드에서는 MYLIBAPI를 dllexport로 정의하게 된다. MYLIBAPI int result; MYLIBAPI int add(int a, int b); MYLIBAPI void printResult(); #endif // DW_MYDLL__H__
<DLL모듈 소스코드>
// MyDll.cpp #includeusing namespace std; #define MYLIBAPI extern "C" __declspec(dllexport) #include "mydll.h" // MYLIBAPI를 먼저 정의해주어 "mydll.h"에 있는 매크로 정의 부분(dllimport)을 // 건너뛰도록 한다. 이렇게 하면 dll을 사용하는 측에서 export/import 선언을 // 신경쓰지 않도록 하면서, 하나의 헤더파일을 실행 모듈 및 dll 모듈 파일에서 // 모두 사용할 수 있다. 이렇게 하나의 헤더파일을 사용해야 유지보수가 용이하다. int result; int add(int a, int b) { result = a+b; return result; } void printResult() { cout << result << endl; }
<실행모듈 소스코드>
#include "mydll.h" int main() { add(3, 5); printResult(); return 0; }
참고 : 제프리 리처의 Windows via C/C++ 5ed./한빛미디어/2008/김명신역