एक DLL से एक सी ++ वर्ग के निर्यात के लिए एक सरल काम कर उदाहरण जोड़ना:
उदाहरण नीचे दिया गया है कि आप केवल कैसे dll और exe एक दूसरे को (आत्म व्याख्यात्मक) बातचीत कर सकते हैं का एक संक्षिप्त अवलोकन देता है, लेकिन इसे और अधिक चीजों की जरूरत है एक उत्पादन कोड में बदलने के लिए जोड़ने के लिए।
पूर्ण नमूना उदाहरण दो भाग
ए एक .dll पुस्तकालय (MyDLL.dll)
बी बनाना एक आवेदन जो .dll पुस्तकालय (एप्लिकेशन) का उपयोग करता है बनाना करने में बांटा गया है।
ए .dll प्रोजेक्ट फ़ाइल (MyDLL.dll):
1. dllHeader.h
#ifdef MYDLL_EXPORTS
#define DLLCALL __declspec(dllexport) /* Should be enabled before compiling
.dll project for creating .dll*/
#else
#define DLLCALL __declspec(dllimport) /* Should be enabled in Application side
for using already created .dll*/
#endif
// Interface Class
class ImyMath {
public:
virtual ~ImyMath() {;}
virtual int Add(int a, int b) = 0;
virtual int Subtract(int a, int b) = 0;
};
// Concrete Class
class MyMath: public ImyMath {
public:
MyMath() {}
int Add(int a, int b);
int Subtract(int a, int b);
int a,b;
};
// Factory function that will return the new object instance. (Only function
// should be declared with DLLCALL)
extern "C" /*Important for avoiding Name decoration*/
{
DLLCALL ImyMath* _cdecl CreateMathObject();
};
// Function Pointer Declaration of CreateMathObject() [Entry Point Function]
typedef ImyMath* (*CREATE_MATH)();
2. dllSrc.cpp
#include "dllHeader.h"
// Create Object
DLLCALL ImyMath* _cdecl CreateMathObject() {
return new MyMath();
}
int MyMath::Add(int a, int b) {
return a+b;
}
int MyMath::Subtract(int a, int b) {
return a-b;
}
बी आवेदन पीआर जो पहले से बनाए गए लोड और लिंक को निकालें।dll फ़ाइल:
#include <iostream>
#include <windows.h>
#include "dllHeader.h"
int main()
{
HINSTANCE hDLL = LoadLibrary(L"MyDLL.dll"); // L".\Debug\MyDLL.dll"
if (hDLL == NULL) {
std::cout << "Failed to load library.\n";
}
else {
CREATE_MATH pEntryFunction = (CREATE_MATH)GetProcAddress(hDLL,"CreateMathObject");
ImyMath* pMath = pEntryFunction();
if (pMath) {
std::cout << "10+10=" << pMath->Add(10, 10) << std::endl;
std::cout << "50-10=" << pMath->Subtract(50, 10) << std::endl;
}
FreeLibrary(hDLL);
}
std::cin.get();
return 0;
}
एक बहुत अच्छा जवाब लिखा बाद में: http://stackoverflow.com/a/22797419/1995714 –