-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdispatch.cpp
More file actions
99 lines (85 loc) · 2.54 KB
/
Copy pathdispatch.cpp
File metadata and controls
99 lines (85 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "xlpython.h"
CDispatchWrapper::CDispatchWrapper(IDispatch* pDispatch)
{
this->cRef = 1;
this->pDispatch = pDispatch;
}
CDispatchWrapper::~CDispatchWrapper()
{
this->pDispatch->Release();
}
HRESULT __stdcall CDispatchWrapper::QueryInterface(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
*ppv = (IUnknown*) this;
else if(riid == IID_IDispatch)
*ppv = (IDispatch*) this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
ULONG __stdcall CDispatchWrapper::AddRef()
{
InterlockedIncrement(&cRef);
return cRef;
}
ULONG __stdcall CDispatchWrapper::Release()
{
ULONG ulRefCount = InterlockedDecrement(&cRef);
if (0 == ulRefCount)
delete this;
return ulRefCount;
}
HRESULT __stdcall CDispatchWrapper::GetTypeInfoCount(UINT* pCountTypeInfo)
{
return pDispatch->GetTypeInfoCount(pCountTypeInfo);
}
HRESULT __stdcall CDispatchWrapper::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
{
return pDispatch->GetTypeInfo(iTypeInfo, lcid, ppITypeInfo);
}
HRESULT __stdcall CDispatchWrapper::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
return pDispatch->GetIDsOfNames(riid, rgszNames, cNames, lcid, rgDispId);
}
HRESULT __stdcall CDispatchWrapper::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
// we test if pVarResult is NULL because this is what VBA sets it to if the function is called as a statement (i.e. without
// parentheses), but this then causes the arguments to be overwritten for some reason, so we pass it a dummy result
// variable which we then dispose of
VARIANT result;
VariantInit(&result);
HRESULT hRet = pDispatch->Invoke(dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult == NULL ? &result : pVarResult, pExcepInfo, puArgErr);
VariantClear(&result);
if(FAILED(hRet) && pExcepInfo->bstrDescription != NULL)
{
BSTR bstrOld = pExcepInfo->bstrDescription;
std::string old;
ToStdString(bstrOld, old);
if(old.substr(0, 24) == "Unexpected Python Error:")
{
std::vector<std::string> parts;
strsplit(old, "\n", parts, false);
if(parts[0] == "Unexpected Python Error: Traceback (most recent call last):")
{
std::string neu;
for(int k = (int) parts.size() - 1; k > 0; k--)
{
if(!parts[k].empty())
{
if(!neu.empty())
neu += "\n";
neu += parts[k];
}
}
ToBStr(neu, pExcepInfo->bstrDescription);
SysFreeString(bstrOld);
}
}
}
return hRet;
}