| 1 |
#include <Exdisp.h> |
|---|
| 2 |
#include <comutil.h> |
|---|
| 3 |
#include <exdispid.h> |
|---|
| 4 |
#include <objbase.h> |
|---|
| 5 |
#include <ole2.h> |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) { |
|---|
| 9 |
|
|---|
| 10 |
va_list marker; |
|---|
| 11 |
va_start(marker, cArgs); |
|---|
| 12 |
|
|---|
| 13 |
if(!pDisp) { |
|---|
| 14 |
MessageBox(NULL, L"NULL IDispatch passed to AutoWrap()", L"Error", 0x10010); |
|---|
| 15 |
_exit(0); |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
DISPPARAMS dp = { NULL, NULL, 0, 0 }; |
|---|
| 20 |
DISPID dispidNamed = DISPID_PROPERTYPUT; |
|---|
| 21 |
DISPID dispID; |
|---|
| 22 |
HRESULT hr; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID); |
|---|
| 32 |
if(FAILED(hr)) { |
|---|
| 33 |
|
|---|
| 34 |
MessageBox(NULL, L"1", L"AutoWrap1()", 0x10010); |
|---|
| 35 |
|
|---|
| 36 |
return hr; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
VARIANT *pArgs = new VARIANT[cArgs+1]; |
|---|
| 41 |
|
|---|
| 42 |
for(int i=0; i<cArgs; i++) { |
|---|
| 43 |
pArgs[i] = va_arg(marker, VARIANT); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
dp.cArgs = cArgs; |
|---|
| 48 |
dp.rgvarg = pArgs; |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
if(autoType & DISPATCH_PROPERTYPUT) { |
|---|
| 52 |
dp.cNamedArgs = 1; |
|---|
| 53 |
dp.rgdispidNamedArgs = &dispidNamed; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL); |
|---|
| 58 |
if(FAILED(hr)) { |
|---|
| 59 |
char buf[200]; |
|---|
| 60 |
sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx", L"blah", dispID, hr); |
|---|
| 61 |
|
|---|
| 62 |
MessageBoxA(NULL, buf, "AutoWrap2()", 0x10010); |
|---|
| 63 |
|
|---|
| 64 |
return hr; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
va_end(marker); |
|---|
| 68 |
|
|---|
| 69 |
delete [] pArgs; |
|---|
| 70 |
|
|---|
| 71 |
return hr; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
bool GetCOMServerExecutablePath(wstring COMApplicationName, wstring * path) |
|---|
| 75 |
{ |
|---|
| 76 |
CLSID clsid; |
|---|
| 77 |
LPOLESTR pwszClsid; |
|---|
| 78 |
HKEY hKey; |
|---|
| 79 |
wchar_t szRegPath[1024]; |
|---|
| 80 |
DWORD cSize = 1024; |
|---|
| 81 |
HRESULT hr = CLSIDFromProgID(COMApplicationName.c_str(), &clsid); |
|---|
| 82 |
if (FAILED(hr)) |
|---|
| 83 |
{ |
|---|
| 84 |
return false; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
hr = StringFromCLSID(clsid, &pwszClsid); |
|---|
| 89 |
if (FAILED(hr)) |
|---|
| 90 |
{ |
|---|
| 91 |
return false; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
wstring regPath = L"CLSID\\"; |
|---|
| 95 |
regPath += pwszClsid; |
|---|
| 96 |
regPath += L"\\LocalServer32"; |
|---|
| 97 |
|
|---|
| 98 |
LONG lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, regPath.c_str(), 0, KEY_ALL_ACCESS, &hKey); |
|---|
| 99 |
if (lRet != ERROR_SUCCESS) |
|---|
| 100 |
{ |
|---|
| 101 |
wstring regPath = L"CLSID\\"; |
|---|
| 102 |
regPath += pwszClsid; |
|---|
| 103 |
regPath += L"\\LocalServer"; |
|---|
| 104 |
lRet = RegOpenKeyEx(HKEY_CLASSES_ROOT, regPath.c_str(), 0, KEY_ALL_ACCESS, &hKey); |
|---|
| 105 |
if (lRet != ERROR_SUCCESS) |
|---|
| 106 |
{ |
|---|
| 107 |
return false; |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
lRet = RegQueryValueEx(hKey, NULL, NULL, NULL, (LPBYTE)szRegPath, &cSize); |
|---|
| 113 |
RegCloseKey(hKey); |
|---|
| 114 |
if (lRet != ERROR_SUCCESS) |
|---|
| 115 |
{ |
|---|
| 116 |
return false; |
|---|
| 117 |
} else { |
|---|
| 118 |
wchar_t *x = wcsrchr(szRegPath, '/'); |
|---|
| 119 |
if(0!= x) |
|---|
| 120 |
{ |
|---|
| 121 |
int result = x - szRegPath; |
|---|
| 122 |
szRegPath[result] = '\0'; |
|---|
| 123 |
} |
|---|
| 124 |
if(szRegPath[0] == '"') |
|---|
| 125 |
{ |
|---|
| 126 |
|
|---|
| 127 |
wchar_t *c = wcsrchr(szRegPath, '"'); |
|---|
| 128 |
if(0!= c) |
|---|
| 129 |
{ |
|---|
| 130 |
int result = c - szRegPath; |
|---|
| 131 |
szRegPath[result] = '\0'; |
|---|
| 132 |
} |
|---|
| 133 |
*path = wcsstr(szRegPath, L"\"")+1; |
|---|
| 134 |
} else { |
|---|
| 135 |
*path = szRegPath; |
|---|
| 136 |
} |
|---|
| 137 |
return true; |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|