COM.h:
1 #pragma once
2 #include <atlcomcli.h>
3
4 class COMMacro
5 {
6 public:
7 COMMacro(LPTSTR Object);
8 ~COMMacro();
9 BOOL Invoke(LPTSTR strFunction,VARIANT* pVarParams,int nParamterCount,VARIANT* pResult);
10 VARIANT ToVARIANT(BSTR str);
11 VARIANT ToVARIANT(int nVal);
12 VARIANT ToVARIANT(bool bVal);
13 VARIANT ToVARIANT(double dVal);
14 VARIANT ToVARIANT_ByRef(BSTR* pStr);
15 VARIANT ToVARIANT_ByRef(int* pnVal);
16 VARIANT ToVARIANT_ByRef(bool* pbVal);
17 VARIANT ToVARIANT_ByRef(double* pdVal);
18 BSTR ToBSTR(VARIANT Val);
19 int ToInt(VARIANT Val);
20 bool ToBool(VARIANT Val);
21 double ToDouble(VARIANT Val);
22
23 BOOL ToBSTR(LPTSTR src,BSTR& dest);
24 protected:
25 CLSID m_clsID;
26 CComPtr<IUnknown> m_Unknown;
27 CComDispatchDriver* m_pDispatchDriver;
28 };
COM.cpp:
1 #include "stdafx.h"
2 #include "COM.h"
3 #include <comdef.h>
4
5 COMMacro::COMMacro(LPTSTR Object)
6 {
7 HRESULT hr = CLSIDFromProgID(Object,&m_clsID);
8 if(FAILED(hr))
9 {
10 throw _com_error(hr);
11 return;
12 }
13 hr = CoCreateInstance(m_clsID,NULL,CLSCTX_ALL,IID_IUnknown,(LPVOID*)&m_Unknown);
14 if(FAILED(hr))
15 {
16 throw _com_error(hr);
17 return;
18 }
19 m_pDispatchDriver = new CComDispatchDriver(m_Unknown);
20 }
21
22 COMMacro::~COMMacro()
23 {
24 delete m_pDispatchDriver;
25 }
26
27 BOOL COMMacro::Invoke(LPTSTR strFunction,VARIANT* pVarParams,int nParamterCount,VARIANT* pResult)
28 {
29 DISPID DispID;
30 HRESULT hr = m_pDispatchDriver->GetIDOfName(strFunction,&DispID);
31 if(FAILED(hr))
32 {
33 throw _com_error(hr);
34 return FALSE;
35 }
36 hr = m_pDispatchDriver->InvokeN(DispID,pVarParams,nParamterCount,pResult);
37 if(FAILED(hr))
38 {
39 throw _com_error(hr);
40 return FALSE;
41 }
42 return TRUE;
43 }
44
45 VARIANT COMMacro::ToVARIANT(BSTR str)
46 {
47 VARIANT vResult;
48 vResult.vt = VT_BSTR;
49 vResult.bstrVal = SysAllocString(str);
50 return vResult;
51 }
52
53 VARIANT COMMacro::ToVARIANT(int nVal)
54 {
55 VARIANT vResult;
56 vResult.vt = VT_I4;
57 vResult.intVal = nVal;
58 return vResult;
59 }
60
61 VARIANT COMMacro::ToVARIANT(bool bVal)
62 {
63 VARIANT vResult;
64 vResult.vt = VT_BOOL;
65 vResult.boolVal = bVal;
66 return vResult;
67 }
68
69 VARIANT COMMacro::ToVARIANT(double dVal)
70 {
71 VARIANT vResult;
72 vResult.vt = VT_R8;
73 vResult.dblVal = dVal;
74 return vResult;
75 }
76
77 VARIANT COMMacro::ToVARIANT_ByRef(BSTR* pStr)
78 {
79 VARIANT vResult;
80 vResult.vt = VT_BSTR | VT_BYREF;
81 vResult.byref = pStr;
82 return vResult;
83 }
84
85 VARIANT COMMacro::ToVARIANT_ByRef(int* pnVal)
86 {
87 VARIANT vResult;
88 vResult.vt = VT_I4 | VT_BYREF;
89 vResult.byref = pnVal;
90 return vResult;
91 }
92
93 VARIANT COMMacro::ToVARIANT_ByRef(bool* pbVal)
94 {
95 VARIANT vResult;
96 vResult.vt = VT_BOOL | VT_BYREF;
97 vResult.byref = pbVal;
98 return vResult;
99 }
100
101 VARIANT COMMacro::ToVARIANT_ByRef(double* pdVal)
102 {
103 VARIANT vResult;
104 vResult.vt = VT_BOOL | VT_BYREF;
105 vResult.byref = pdVal;
106 return vResult;
107 }
108
109 BSTR COMMacro::ToBSTR(VARIANT Val)
110 {
111 return *(BSTR*)Val.byref;
112 }
113
114 int COMMacro::ToInt(VARIANT Val)
115 {
116 return *(int*)Val.byref;
117 }
118
119 bool COMMacro::ToBool(VARIANT Val)
120 {
121 return *(bool*)Val.byref;
122 }
123
124 double COMMacro::ToDouble(VARIANT Val)
125 {
126 return *(double*)Val.byref;
127 }
128
129 BOOL COMMacro::ToBSTR(LPTSTR src,BSTR& dest)
130 {
131 dest = SysAllocString(src);
132 return TRUE;
133 }
使用说明:
1.构造函数:
参数(字符串常量):工程名.类名
2.Invoke:
参数1(字符串常量):函数名
参数2(VARIANT数组):调用参数,从右至左
参数3(数值常量):调用参数个数
参数4(VARIANT指针):返回值
3.ToVARIANT:将给定参数转换为VARIANT类型
4.ToVARIANT_ByRef:将给定参数转换为ByRef的VARIANT类型
5.ToBSTR:将给定ByRef的VARIANT转换为BSTR
6.ToInt:将给定ByRef的VARIANT转换为int
7.ToBool:将给定ByRef的VARIANT转换为bool
8.ToDouble:将给定ByRef的VARIANT转换为double
9.ToBSTR:将给定字符串常量转换为BSTR类型
示例:
1 CoInitialize(0); // 初始化ATL
2 try
3 {
4 COMMacro m(L"aaa.bbb");
5 VARIANT vResult,vParams[3];
6 int i = 100;
7 BSTR str;
8 m.ToBSTR(L"abc",str);
9 vParams[2] = m.ToVARIANT(123.456);
10 vParams[1] = m.ToVARIANT(i);
11 vParams[0] = m.ToVARIANT_ByRef(&str);
12 m.Invoke(L"aaa",vParams,3,&vResult);
13 wprintf(L"%s\n",m.ToBSTR(vParams[0]));
14 }
15 catch(_com_error& e)
16 {
17 wprintf(L"%s\n",e.ErrorMessage());
18 }
posted on 2010-11-20 22:40
lwch 阅读(762)
评论(0) 编辑 收藏 引用 所属分类:
QLanguage