VisualStudio2005ã®C++ã§ãDLLãä½ããã¨ã«ãªã£ãã®ã§ããã®ã¾ã¨ãã
注æï¼
VisualStudio2005ã使ã£ãDLLã®å®è£
æ¹æ³ã§ããä»ã®ç°å¢ã§ã¯ãã¶ãå½ã¦ã«ãªãã¾ããã
ã¾ãã/clr:pureでDLLを呼び出す場合の実装方法もよくわかっていない為ããã®è¾ºããç解ã®ä¸èªãã§ä¸ããã
DLLã¨ã¯ï¼
ãã¤ãããã¯ã»ãªã³ã¯ã»ã©ã¤ãã©ãªã®ç¥ã詳細ã¯ウィキペディア見れã
libã¨ã®éãã¯ï¼
libã¯éçã
libã¯ã³ã³ãã¤ã«æã«ãªã³ã¯ãããã
ãã®çºãæ´æ°æã«ã¯ãã¢ããªã®åã³ã³ãã¤ã«ãå¿
è¦ã
DLLã¯åçã
DLLã¯å®è¡æã«ãªã³ã¯ãããã
ãã®çºãæ´æ°æã«ã¯ãDLLã®ä¸æ¸ãã ãã§ãã¢ããªã®åã³ã³ãã¤ã«ãä¸è¦ã
è¤æ°ã®ã¢ããªã§èªã¿åºããã¦ããã¢ããªã¨ç¬ç«ãã1ã¤ã®ã¡ã¢ãªç©ºéãã使ããªãã®ã§ãlibããã¡ã¢ãªä¸æå©ã
ä»åã¯åã©ã¤ãã©ãªãé »ç¹ã«ã¢ãããã¼ããããäºå®ã®çºãDLLãå©ç¨ã
æé»ãªã³ã¯ãæ示ãªã³ã¯
DLLã®ãªã³ã¯ã«ã¯ãæé»ãªã³ã¯ãæ示ãªã³ã¯ãåå¨ããã
æ示ãªã³ã¯
ã³ã¼ãä¸ã§ãDLLã®ãã³ãã«ãåå¾ãã¦ãããããé¢æ°ãã¤ã³ã¿ãåå¾ããæ¹æ³ã
èªã¿åºãDLLãé¢æ°ãåçã«å¤æ´ã§ããã
æé»ãªã³ã¯
ãªã³ã«ä¸ã§ãDLLã®ãªã³ã¯ãè¨å®ããã³ã¼ãä¸DLLã®åå¨ãæèããã«DLLã使ç¨ããæ¹æ³ã
詳ããã¯ãこちらãåç
§ã
ä»åã¯ãå¿
è¦ãªDLLãåçã«å¤ãã訳ã§ããªãã®ã§ãæé»ãªã³ã¯ãé¸æã
CLR
CLRã¨ã¯.NETã®å
±éè¨èªåºç¤(CLI)ãå®è£
ãããã®ãè¦ã¯ãã¼ãã£ã«ãã·ã³ã
CLRãå©ç¨ãããã¨ã§ããã簡便ã«å®å
¨ãªã³ã¼ããæ¸ãããããã¼ãã£ã«ãã·ã³çµç±ã®çºãã©ããã¦ãéããªããã¡ã
ä»åã¯CLRä¸ä½¿ç¨ã®å ´åã¨ãCLRã使ç¨ããå ´åã®2ãã¿ã¼ã³ã®DLLã®å®è£
æ¹æ³ã¨ãå©ç¨å´ã§ã®æé»ãªã³ã¯ã®æ¹æ³ã解説ã
éCLRã®DLL
DLLå´
VisualStudio2005ã§ã
ãæ°ããããã¸ã§ã¯ãã®è¿½å ãâãwin32 ããã¸ã§ã¯ãã
次ã«åºã¦ããã¦ã£ãµã¼ãããã
ãã¢ããªã±ã¼ã·ã§ã³ã®è¨å®ãâãã¢ããªã±ã¼ã·ã§ã³ã®ç¨®é¡ãâãDLLã
ã§çæãããã
以ä¸å®è£
ã³ã¼ã
dllTest.h
// ã¦ã£ãµã¼ãã§DLLãä½ã£ãå ´åã // èªåçã«ãã³ã³ãã¤ã«ãªãã·ã§ã³ã«ã[ããã¸ã§ã¯ãå]_EXPORTSãã追å ãããã // ãã¯ããè¨å®ãããã¨ã§ãDLLå´ã§ã¯exportãå¼ã³åºãå´ã§ã¯ãimportãããããã«ãªãã #ifdef DLLTEST_EXPORTS #define CLASS_DECLSPEC __declspec(dllexport) // DLLã«export #else #define CLASS_DECLSPEC __declspec(dllimport) // DLLãimport #endif // DLLTEST_EXPORTS class CLASS_DECLSPEC DllTestClass { public: ãint getInt(); };
dllTest.cpp
#include "stdafx.h" #include "dllTest.h" #ifdef _MANAGED #pragma managed(push,off) #endif BOOL APIENTRY DLLMain( ãHMODULE hModule, ãDWORD ul_reason_for_call, ãLPVOID lpReserved ) { ãreturn TRUE; } ) int DllTestClass::getInt() { ãreturn 0; } #idfdef _MANAGED #pragma managed(pop) #emdif
DLLå©ç¨å´
DLLãæé»ã«å¼ã³åºãããããªã³ã«ã®è¨å®ãå¿
è¦ã
ä»åã¯DLLã¨ã¢ããªã¯åä¸ã½ãªã¥ã¼ã·ã§ã³ã®çºãä¾åé¢ä¿ãè¨å®ãã¦çµããã
ã¾ããã³ã³ãã¤ã«ã®çºã«dllTest.hãåå¾å´ã®ããã¸ã§ã¯ãã«ã³ãã¼ããå¿
è¦ããã
æ¯åã³ãã¼ãã¦ãè¯ãããæéãªã®ã§ã
ãããã¸ã§ã¯ãã®ããããã£ãâãæ§æããããã£ãâããã«ãã¤ãã³ããâããã«ãå¾ã®ã¤ãã³ããâãã³ãã³ãã©ã¤ã³ã
ã«ãå¿ è¦ãªããããã¡ã¤ã«ãã³ãã¼ããã³ãã³ããæã¡è¾¼ãã¨æ¥½ã
å¾ã¯ã以ä¸ã®ããã«DLLãæèããã«ä½¿ç¨ã§ããã
DllTestClass* _dllTestClass = new DllTestClass(); int a = _dllTestClass->getInt();
éCLRã®DLLã/clr:pureã®ã¢ããªã§èªã¿è¾¼ãã
éCLRã®DLLã«å¯¾ãã¦ãèªã¿åºãå´ãwindowsãã©ã¼ã ã¢ããªçã®/clr:pureã使ãã¢ããªã®å ´åãã³ã³ãã¤ã«æã«ä»¥ä¸ã®ãããªã¯ã¼ãã³ã°ãã¨ã©ã¼ãçºçããã
warning C4272ï¼(dllTestClass::getInt' : __delspec(dllimport)ã«è¨å®ããã¦ãã¾ããé¢æ°ãã¤ã³ãã¼ãããã¨ãã¯ãã¤ãã£ãå¼ã³åºãè¦ç´ãæå®ããªããã°ãªãã¾ããã
error LNK2028ï¼æªè§£æ±ºã®ãã¼ã¯ã³(0A00000C) "public: int __clrcall dllTestClass::getInt(void)" (?getInt@dllTestClass@@$$FQAMHXZ) ãé¢æ°"int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP#AAVString@System@@@Z)ã§åç §ããã¾ãã
error LNK2019ï¼æªè§£æ±ºã®å¤é¨ã·ã³ãã« "public: int __clrcall classTestSpace::classTest::getInt(void)" (?getInt@classTest@classTestSpace@@$$FQAMHXZ) ãé¢æ°"int __clrclaa main(cli::array^)" (?main@@$$HYMHP$01AP#AAVString@System@@@Z)ã§åç §ããã¾ãã
fatal error LNK1120: å¤é¨åç § 4 ãæªè§£æ±ºã§ãã
以ä¸ã®è¨å®ã/clr:pure ãã /clrã«ããã¨ã¨ã©ã¼ã¯ãªããªãã
ãããã¸ã§ã¯ãã®ããããã£ãâãå ¨è¬ãâãå ±éè¨èªã©ã³ã¿ã¤ã ãµãã¼ãã
/clr:pureã§ã¯ãã¢ã³ããã¼ã¸ãã³ã¼ã(ããªãã¡ãé/clr:pureãªã³ã¼ã)ã¯ã使ããªãã
ããã¯ã/clr:pureããç´ç²ã«CLRã®ã¿ã³ã¼ãã§ãããã¨ããæå³ããã
/clrã¯ãCLRã«å¯¾å¿ãã¦ããã¨ããã ããªã®ã§ãã¢ã³ããã¼ã¸ãã³ã¼ããå©ç¨ã§ããã
はてなで質問ãã¦ã¿ãã¨ãããã¢ã³ããã¼ã¸ãã³ã¼ãã§ãæ¡ä»¶ãæºããã°ã/clr:pureã§ä½¿ç¨ã§ããããã§ãã
詳細ã¯ãå¾æ¥è¿½è¨ã
å ·ä½çãªå®è£ æ¹æ³(CLRã®DLL)
windowsãã©ã¼ã ãªã©ãCLRã使ã£ãã³ã¼ããDLLã«ããå ´åã®æ¹æ³ã
DLLå´
VisualStudio2005ã§ã
ãã½ãªã¥ã¼ã·ã§ã³ã®ããããã£ãâã追å ãâãæ°ããããã¸ã§ã¯ããâãã¯ã©ã¹ã©ã¤ãã©ãªã
ã§ãçæãããã
å®è£
ã«ã¤ãã¦ã¯ç¹ã«æ³¨æç¹ç¡ãã
éCLRã§ã®__declspecã®ãããªãã®ãæ¸ãå¿
è¦ã¯ç¡ãã
å©ç¨å´
å
±éãããã¼(stdafx.h)çã§ã使ç¨ããDLLãusingããã
#ifdef _DEBUG #using "../Debug/testDll.dll" #else #using "../Release/testDll.dll"
ããã ãã§OKã
éDLLã¨ç°ãªããããããã¡ã¤ã«ãç¡ãã¦ããå®è£
å¯è½ã
çåç¹
2007/5/14 追è¨
- はてなでの回答ãå
容ã«åæ ã
id:kaorunãããã©ãããããã¨ããããã¾ããï¼ - ãããããã¦ãã¦ãã®é¨åããéCLRã®DLLã/clr:pureã®ã¢ããªã§èªã¿è¾¼ããã«å¤ãã¦å 容ãå¤æ´ã
- ãCLRã§DLLå®è£ ãã追å ã
- ãã¨æ§æçãç´°ã ã¨å¤æ´ã
ãããªæãã¼ã