मेरे पास एक सीपीपी प्रोजेक्ट है, एक सीपीपी क्ली प्रोजेक्ट और सी # जीत फॉर्म प्रोजेक्ट है। मैं अपने मूल सीपीपी कोड से एक विधि को आग लगाना चाहता हूं और इसे सी # प्रोजेक्ट में पकड़ना चाहता हूं। मैं यह कैसे कर सकता हूँ?सीपीपी/क्ली फायर एक इवेंट
उत्तर
इस प्रश्न का उत्तर देने के लिए कई दृष्टिकोण हो सकते हैं, क्योंकि उन परियोजनाओं के बीच निर्भरता आवश्यकता महत्वपूर्ण है। मैं सबसे आम (मुझे लगता है) के मामले का उत्तर देने का प्रयास करूंगा: जिसमें आपके पास पहले से ही मूल सी ++ लाइब्रेरी है और आप उस लाइब्रेरी को सी # एप्लिकेशन में उपयोग करना चाहते हैं। उस परिदृश्य में सी # परियोजना मूल पुस्तकालय परियोजना पर निर्भर करती है। ऐसे मामले में आप देशी सी ++ घटनाओं को .NET ईवेंट में बदलने के लिए गेटवे क्ली/सी ++ लाइब्रेरी का उपयोग कर सकते हैं।
यहां एक संपूर्ण कोड नमूना है, लेकिन उससे पहले, कृपया ध्यान दें:
- यह कम से कम समाधान नहीं हो सकता है, लेकिन यह ठीक काम करता है। इसके अलावा यह मूल डेटा को .NET प्रकारों में बदलने पर अधिक नियंत्रण प्रदान कर सकता है।
- मैंने वीएस 2005 में इस दृष्टिकोण का उपयोग किया। मुझे नहीं पता कि उस विशिष्ट इंटरऑपरेबिलिटी उद्देश्य के लिए वीएस के नए संस्करणों में बेहतर उपकरण है या नहीं।
- यदि आपका मूल कार्यक्रम GUI थ्रेड के अलावा किसी थ्रेड से ट्रिगर किया गया है, तो that से सावधान रहें।
मूल निवासी लाइब्रेरी:
#ifndef _NATIVE_CODE_H_
#define _NATIVE_CODE_H_
//NativeCode.h
//A simple native library which emits only one event.
#include <stdlib.h>
#include <iostream>
using namespace std;
#define NATIVELIBRARY_API __declspec(dllexport)
//An argument class to wrap event parameters
class NativeEventArgs{
public:
//a 32bit integer argument
//any other primitives can be here, just be careful about the byte size
int argInt32;
//null terminated ascii string
const char* argString;
//null terminated wide/unicode string
const wchar_t* argWString;
};
//A simple mechanism to fire an event from native code.
//Your library may have a DIFFERENT triggering mechanism (e.g. function pointers)
class INativeListener
{
public:
virtual void OnEvent(const NativeEventArgs& args)=0;
};
//The actual native library code, source of native events
class NATIVELIBRARY_API NativeCode
{
public:
NativeCode()
:theListener_(NULL)
{}
//Listener registration method
void registerListener(INativeListener* listener) {
theListener_ = listener;
}
//this is the very first source of the event
//native code emits the event via the listener mechanism
void eventSourceMethod() {
//... other stuff
//fire the native event to be catched
if(theListener_){
//prepare event parameters
NativeEventArgs args;
wstring wstr(L"A wide string");
string str("A regular string");
//build-up the argument object
args.argInt32 = 15;
args.argString = str.c_str();
args.argWString = wstr.c_str();
//fire the event using argument
theListener_->OnEvent(args);
}
}
private:
//native code uses a listener object to emit events
INativeListener* theListener_;
};
#endif
गेटवे लाइब्रेरी नमूना:
//GatewayCode.h
//GatewayLibrary is the tricky part,
//Here we listen events from the native library
//and propagate them to .net/clr world
#ifndef _GATEWAY_CODE_H_
#define _GATEWAY_CODE_H_
#include "../NativeLibrary/NativeCode.h" //include native library
#include <vcclr.h> //required for gcroot
using namespace System;
using namespace System::Runtime::InteropServices;
namespace GatewayLibrary{
//.net equvelant of the argument class
public ref class DotNetEventArg{
internal:
//contructor takes native version of argument to transform
DotNetEventArg(const NativeEventArgs& args) {
//assign primitives naturally
argInt32 = args.argInt32;
//convert wide string to CLR string
argWString = Marshal::PtrToStringUni(IntPtr((void*)args.argWString));
//convert 8-bit native string to CLR string
argString = Marshal::PtrToStringAnsi(IntPtr((void*)args.argString));
//see Marshal class for rich set of conversion methods (e.g. buffers)
}
private:
String^ argString;
String^ argWString;
Int32 argInt32;
public:
//define properties
property String^ ArgString {
String^ get() {
return argString;
}
}
property String^ ArgWString {
String^ get() {
return argWString;
}
}
property Int32 ArgInt32 {
Int32 get() {
return argInt32;
}
}
};
//EventGateway fires .net event when a native event happens.
//It is the actual gateway class between Native C++ and .NET world.
//In other words, It RECEIVES NATIVE events, TRANSFORMS/SENDS them into CLR.
public ref class EventGateway {
public:
//ctor, its implementation placed below
EventGateway();
//required to clean native objects
~EventGateway();
!EventGateway();
//the SENDER part
//.net event stuff defined here
delegate void DotNetEventHandler(DotNetEventArg^ arg);
event DotNetEventHandler^ OnEvent;
private:
//our native library code
//notice you can have pointers to native objects in ref classes.
NativeCode* nativeCode_;
//the required device to listen events from the native library
INativeListener* nativeListener_;
internal: //hide from .net assembly
//the RECEIVER part, called when a native event received
void OnNativeEvent(const NativeEventArgs& args){
//you can make necessary transformation between native types and .net types
//create .net argument using native argument
//required conversion is done by DotNetEventArg class
DotNetEventArg^ dotNetArgs = gcnew DotNetEventArg(args);
//fire .net event
OnEvent(dotNetArgs);
}
};
}
//A concrete listener class. we need this class to register native library events.
//Its our second gateway class which connects Native C++ and CLI/C++
//It basically gets events from NativeLibary and sends them to EventGateway
class NativeListenerImp : public INativeListener {
public:
NativeListenerImp(gcroot<GatewayLibrary::EventGateway^> gatewayObj){
dotNetGateway_ = gatewayObj;
}
//this is the first place we know that a native event has happened
virtual void OnEvent(const NativeEventArgs& args) {
//inform the .net gateway which is responsible of transforming native event to .net event
dotNetGateway_->OnNativeEvent(args);
}
private:
//class member to trigger .net gateway.
//gcroot is required to declare a CLR type as a member of native class.
gcroot<GatewayLibrary::EventGateway^> dotNetGateway_;
};
////ctor and dtors of EventGateway class
GatewayLibrary::EventGateway::EventGateway()
{
nativeCode_ = new NativeCode();
//note; using 'this' in ctor is not a good practice
nativeListener_ = new NativeListenerImp(this);
//register native listener
nativeCode_->registerListener(nativeListener_);
}
GatewayLibrary::EventGateway::~EventGateway()
{
//call the non-deterministic destructor
this->!EventGateway();
}
GatewayLibrary::EventGateway::!EventGateway()
{
//clean up native objects
delete nativeCode_;
delete nativeListener_;
}
#endif
और सी # में अंतिम आवेदन (या किसी अन्य .net भाषा में):
पर जानकारी आपके उत्तर के लिए धन्यवाद। गेटवे लिस्टनर आईएमपी क्या है? विजुअल स्टूडियो इसे नहीं जानता है। (वीएस -2010) –
ठीक है, मैंने अपना जवाब संपादित किया है, यह 'मूल लिस्टनर आईएमपी होना चाहिए। ऐसा लगता है कि स्वरूपण करते समय मैंने गलती की थी। – xaero99
आपको बहुत बहुत धन्यवाद .. यह –
- 1. AsyncTask पर फायर इवेंट
- 2. jQuery - फायर चेंज इवेंट ऑनलोड
- 3. टेक्स्टबॉक्स पर फायर इवेंट फोकस
- 4. मार्करक्लस्टर के बाद फायर इवेंट
- 5. फायर
- 6. एएसपीनेट में क्लाइंट साइड स्क्रिप्ट सत्यापन के बाद फायर इवेंट?
- 7. विकल्प का चयन करें ड्रॉपडाउनलिस्ट सी # एएसपी.नेट फायर इवेंट
- 8. फायर टेक्स्टबॉक्स। टेक्स्ट
- 9. फायर पास्कल क्या है?
- 10. उसी तत्व पर माउसडाउन ईवेंट के बाद इवेंट फायर को डबलक्लिक क्यों नहीं करता है?
- 11. एक आफ्टरबिल्ड इवेंट
- 12. एक डब्ल्यूपीएफ एक्जिट इवेंट
- 13. किंडल फायर और फ़ाइल अपलोड
- 14. विंडोज इवेंट लॉग - एक इवेंट स्रोत कैसे पंजीकृत करें?
- 15. इवेंट
- 16. ईमेल प्राप्त होने पर एक ईवेंट को फायर करें
- 17. इवेंट कैप्चरिंग बनाम इवेंट बबलिंग
- 18. विशिष्ट विंडोज इवेंट लॉग इवेंट
- 19. एक इवेंट लिस्टनर एक अवलोकन योग्य है?
- 20. सॉकेट.ओ "डिस्कनेक्ट" ईवेंट को फायर करने में देरी?
- 21. नेक्सस 7 और किंडल फायर एचडी, विभिन्न
- 22. जावास्क्रिप्ट में फायर टैब कीप्रेस घटना?
- 23. फायर मैनिपुलेशन मैन्युअल रूप से WP7
- 24. जीडब्ल्यूटी। प्रोग्राम्स पर प्रोग्रामबॉक्स पर फायर चेंजवेन्ट।
- 25. पता लगाएं कि ऐप्पल किंडल फायर
- 26. मैन्युअल रीसेट इवेंट, ऑटो रीसेट इवेंट
- 27. फेसबुक इवेंट एपीआई: इवेंट सदस्य (एफक्यूएल)
- 28. टीवीर्टुअलस्टिंगट्री: ऑनमेइटर इटैम इवेंट और ऑनजीटटेक्स्ट इवेंट
- 29. गेम प्रोग्रामिंग और इवेंट हैंडलर
- 30. इवेंट हैंडलर?
यह एक इंटरऑपरेबिलिटी प्रश्न है, मेरा सुझाव है कि आप इसे इस तरह टैग करें, [यहां] देखें (http://msdn.microsoft.com/en-us/library/2x8kf7zx (v = vs.80) .aspx) के लिए पिनवोक – ldgorman