Implementieren Sie die IDispatch-Schnittstelle einfach in C ++C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Implementieren Sie die IDispatch-Schnittstelle einfach in C ++

Post by Anonymous »

Ich habe die folgende IDispatch-Schnittstelle in meinem C# -Projekt definiert: < /p>

Code: Select all

    [ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("3532C4E8-D320-487C-8BD4-F448B94B9E83")]
public interface ICppRuntimeApi2
{
[DispId(1)]
bool TestCallback(string pasteType);
}
Nach dem Importieren der generierten Typbibliothek in meinem C ++-Projekt kann ich meine ICPPRUNTIMEAPI2 frei implementieren, wie zum Beispiel:

Code: Select all

class CppRuntimeApiImpl2 : public MyProject::ICppRuntimeApi2 {
public:
CppRuntimeApiImpl2() : refCount(1) {}

// IUnknown methods
HRESULT __stdcall QueryInterface(REFIID riid, void** ppv) override {
if (riid == IID_IUnknown || riid == IID_IDispatch || riid == __uuidof(MyProject::ICppRuntimeApi2)) {
*ppv = static_cast(this);
}
else {
*ppv = nullptr;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}

ULONG __stdcall AddRef() override {
return InterlockedIncrement(&refCount);
}

ULONG __stdcall Release() override {
ULONG count = InterlockedDecrement(&refCount);
if (count == 0) {
delete this;
}
return count;
}

// IDispatch methods
HRESULT __stdcall GetTypeInfoCount(UINT* pctinfo) override {
*pctinfo = 0;
return S_OK;
}

HRESULT __stdcall GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo) override {
*ppTInfo = nullptr;
return E_NOTIMPL;
}

HRESULT __stdcall GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) override {
if (cNames != 1) return DISP_E_UNKNOWNNAME;
if (_wcsicmp(rgszNames[0], L"TestCallback") == 0) {
rgDispId[0] = 1;
return S_OK;
}
return DISP_E_UNKNOWNNAME;
}

HRESULT __stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr) override {
if (dispIdMember == 1) {
if (pDispParams->cArgs != 1 || pDispParams->rgvarg[0].vt != VT_BSTR) {
return DISP_E_BADPARAMCOUNT;
}
std::wstring pasteType(pDispParams->rgvarg[0].bstrVal, SysStringLen(pDispParams->rgvarg[0].bstrVal));
bool result = TestCallback(pasteType);
if (pVarResult) {
pVarResult->vt = VT_BOOL;
pVarResult->boolVal = result ? VARIANT_TRUE : VARIANT_FALSE;
}
return S_OK;
}
return DISP_E_MEMBERNOTFOUND;
}

// ICppRuntimeApi2 method
bool TestCallback(const std::wstring& pasteType) {
MessageBoxW(nullptr, (L"TestCallback call via COM interop! \nTag: " + pasteType).c_str(), L"TestCallback", MB_OK);
return true;
}

private:
LONG refCount;
};

< /code>
Diese Lösung funktioniert, aber dies ist offensichtlich keine praktikable Lösung. Für neue Methoden müsste umfangreiche manuelle Anstrengungen zur Implementierung jeder Dispid -Prüfung, der Parameter -Überprüfung und des Aufrufs zur tatsächlichen Logik C ++ -. Wenn Sie die Methode testcallback 
von C# aufrufen, scheint es nichts zu rufen. Ich habe versucht, die Beratungsfunktionen anzurufen, funktioniert aber trotzdem nicht. Weg?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post