root/capture-mod/trunk/ApplicationPlugins/ComHelper.h

Revision 823, 3.6 kB (checked in by xkovah, 1 year ago)

adding the files finally

Line 
1 #include <Exdisp.h>
2 #include <comutil.h>
3 #include <exdispid.h>
4 #include <objbase.h>
5 #include <ole2.h>
6
7 /*  Thank you Micrsoft ... http://support.microsoft.com/kb/216686 */
8 HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) {
9     // Begin variable-argument list...
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     // Variables used...
19     DISPPARAMS dp = { NULL, NULL, 0, 0 };
20     DISPID dispidNamed = DISPID_PROPERTYPUT;
21     DISPID dispID;
22     HRESULT hr;
23     //char buf[200];
24     //char szName[200];
25
26    
27     // Convert down to ANSI
28     //WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
29     
30     // Get DISPID for name passed...
31     hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
32     if(FAILED(hr)) {
33        // sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx", szName, hr);
34         MessageBox(NULL, L"1", L"AutoWrap1()", 0x10010);
35         //_exit(0);
36         return hr;
37     }
38    
39     // Allocate memory for arguments...
40     VARIANT *pArgs = new VARIANT[cArgs+1];
41     // Extract arguments...
42     for(int i=0; i<cArgs; i++) {
43         pArgs[i] = va_arg(marker, VARIANT);
44     }
45    
46     // Build DISPPARAMS
47     dp.cArgs = cArgs;
48     dp.rgvarg = pArgs;
49    
50     // Handle special-case for property-puts!
51     if(autoType & DISPATCH_PROPERTYPUT) {
52         dp.cNamedArgs = 1;
53         dp.rgdispidNamedArgs = &dispidNamed;
54     }
55    
56     // Make the call!
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       // printf(
62         MessageBoxA(NULL, buf, "AutoWrap2()", 0x10010);
63        // _exit(0);
64         return hr;
65     }
66     // End variable-argument section...
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         // Convert CLSID to String
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         // Query value of key to get Path and close the key
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) // If no /Automation switch on the path
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     }
Note: See TracBrowser for help on using the browser.