मैं एक सी #/जावा पृष्ठभूमि से आया था, इसलिए मैं एक सी ++ डीएल बनाने के तरीके को समझने की कोशिश कर रहा हूं जो सी # डीएल के समान व्यवहार करता है।सी ++ कक्षा को डीएलएल के रूप में कैसे निर्यात करें?
मैंने __declspec(dllexport)
और __declspec(dllimport)
के साथ प्रयोग किया है, लेकिन मैंने इसे केवल स्थिर तरीकों पर काम करने में कामयाब रहा है। मुझे यकीन है कि यह मेरी सीमित समझ के कारण है।
मैं सी ++ में कक्षाओं को कैसे निर्यात कर सकता हूं (पूरी तरह से निजी सदस्यों सहित) और संदर्भ के अंत में उन्हें तुरंत चालू करने में सक्षम हूं क्योंकि मैं सी # के साथ होगा? ऑनलाइन संसाधन/ट्यूटोरियल के लिए कुछ पॉइंटर भी ऐसा करेंगे।
मैंने एमएफसी डीएलएल टेम्पलेट का उपयोग शुरू किया, और ईमानदारी से मुझे नहीं पता कि उनमें से 9 0% किसके लिए हैं और मैं CWinApp से क्यों विरासत में हूं। मैंने कक्षा को CCppPracticeLibraryApp
के साथ टैग करने का प्रयास किया लेकिन यह अब संकलित नहीं होगा।
// CppPracticeLibrary.h : main header file for the CppPracticeLibrary DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
#ifdef CCppPracticeLibraryApp_EXPORTS
#define CCppPracticeLibraryApp_API __declspec(dllexport)
#else
#define CCppPracticeLibraryApp_API __declspec(dllimport)
#endif
// CCppPracticeLibraryApp
// See CppPracticeLibrary.cpp for the implementation of this class
//
class CCppPracticeLibraryApp : public CWinApp
{
public:
CCppPracticeLibraryApp();
static CCppPracticeLibraryApp_API void SayHelloWorld();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
परिभाषा फ़ाइल:
//CppPracticeLibrary.cpp: DLL के लिए प्रारंभ दिनचर्या को परिभाषित करता है।
#include "stdafx.h"
#include "CppPracticeLibrary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define CCppPracticeLibraryApp_EXPORTS
BEGIN_MESSAGE_MAP(CCppPracticeLibraryApp, CWinApp)
END_MESSAGE_MAP()
// CCppPracticeLibraryApp construction
CCppPracticeLibraryApp::CCppPracticeLibraryApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
void CCppPracticeLibraryApp::SayHelloWorld()
{
printf("Hello world");
}
// The one and only CCppPracticeLibraryApp object
CCppPracticeLibraryApp theApp;
// CCppPracticeLibraryApp initialization
BOOL CCppPracticeLibraryApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
क्लाइंट/संदर्भित विधि
// TestConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "TestConsoleApplication.h"
#include "CppPracticeLibrary.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
/*CCppPracticeLibraryApp* testCallingLibrary = new CCppPracticeLibraryApp();
testCallingLibrary->SayHelloWorld();*/
CCppPracticeLibraryApp::SayHelloWorld();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
मैं उपरोक्त कोड में निम्नलिखित लाइनों uncomment करने में सक्षम होना चाहते हैं:
/*CCppPracticeLibraryApp* testCallingLibrary = new CCppPracticeLibraryApp();
testCallingLibrary->SayHelloWorld();*/
कृपया अपना कोड दिखाएं ... –
@ bash.d कोड के साथ अपडेट किया गया। – Alwyn