1 |
|
5 |
#ifndef SMARTVARS_H
|
2 |
|
|
#define SMARTVARS_H
|
3 |
|
|
#undef _INC_CRTDBG
|
4 |
|
|
#include <assert.h>
|
5 |
|
|
#include <Atl/atlbase.h>
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
*
|
9 |
|
|
USAGE :
|
10 |
|
|
SafeBSTRArray names(1);
|
11 |
|
|
SafeLongArray types(1);
|
12 |
|
|
SafeBSTRArray vals(1);
|
13 |
|
|
|
14 |
|
|
names[0] = T2BSTR(_T("ReplaceFile"));
|
15 |
|
|
types[0] = swMacroFeatureParamTypeString;
|
16 |
|
|
vals[0] = m_replaceFile.AllocSysString();
|
17 |
|
|
|
18 |
|
|
CComVariant paramNames = names;
|
19 |
|
|
CComVariant paramTypes = types;
|
20 |
|
|
CComVariant paramValues = vals;
|
21 |
|
|
|
22 |
|
|
LPDISPATCH outFeat = NULL;
|
23 |
|
|
HRESULT hres = m_Part->InsertComFeature(T2BSTR(_T("{04B74DF2-ACBA-4218-887A-F42140F312E0}")),
|
24 |
|
|
paramNames, paramTypes, paramValues, &outFeat);
|
25 |
|
|
if (outFeat)
|
26 |
|
|
outFeat->Release();
|
27 |
|
|
*/
|
28 |
|
|
template <class T,int type> class SafeArray
|
29 |
|
|
{
|
30 |
|
|
public:
|
31 |
|
|
SafeArray(VARIANT *input):
|
32 |
|
|
m_input(input),
|
33 |
|
|
m_access(false),
|
34 |
|
|
m_bCreate(false),
|
35 |
|
|
m_pSafeArray(NULL),
|
36 |
|
|
m_arrayData(NULL),
|
37 |
|
|
m_result(S_OK)
|
38 |
|
|
|
39 |
|
|
{
|
40 |
|
|
if(m_input != NULL)
|
41 |
|
|
{
|
42 |
|
|
if (V_VT(m_input) != VT_EMPTY)
|
43 |
|
|
{
|
44 |
|
|
m_pSafeArray = V_ARRAY (m_input);
|
45 |
|
|
m_result = SafeArrayAccessData ( m_pSafeArray, (void HUGEP**)&m_arrayData);
|
46 |
|
|
m_access = m_result == S_OK ;
|
47 |
|
|
}
|
48 |
|
|
else
|
49 |
|
|
m_access = true;
|
50 |
|
|
}
|
51 |
|
|
else
|
52 |
|
|
{
|
53 |
|
|
assert(FALSE);
|
54 |
|
|
m_input = &m_target;
|
55 |
|
|
}
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
SafeArray(const VARIANT &input) :
|
59 |
|
|
m_access(true),
|
60 |
|
|
m_bCreate(false),
|
61 |
|
|
m_pSafeArray(NULL),
|
62 |
|
|
m_arrayData(NULL),
|
63 |
|
|
m_result(S_OK)
|
64 |
|
|
{
|
65 |
|
|
m_input = (VARIANT *) &input;
|
66 |
|
|
if (V_VT(m_input) != VT_EMPTY)
|
67 |
|
|
{
|
68 |
|
|
m_pSafeArray = V_ARRAY (m_input);
|
69 |
|
|
m_result = SafeArrayAccessData ( m_pSafeArray, (void HUGEP**)&m_arrayData);
|
70 |
|
|
m_access = m_result == S_OK ;
|
71 |
|
|
}
|
72 |
|
|
else
|
73 |
|
|
m_access = true;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
SafeArray(unsigned int size,unsigned int dims = 1):
|
77 |
|
|
m_input(&m_target),
|
78 |
|
|
m_access(false),
|
79 |
|
|
m_bCreate(true),
|
80 |
|
|
m_pSafeArray(NULL),
|
81 |
|
|
m_arrayData(NULL),
|
82 |
|
|
m_result(S_OK)
|
83 |
|
|
{
|
84 |
|
|
assert(size >= 0);
|
85 |
|
|
if(size > 0)
|
86 |
|
|
{
|
87 |
|
|
m_rgsabound[0].lLbound = 0;
|
88 |
|
|
m_rgsabound[0].cElements = (int) size;
|
89 |
|
|
m_pSafeArray = SafeArrayCreate(type, (int)dims, m_rgsabound);
|
90 |
|
|
V_VT(m_input) = VT_ARRAY | type;
|
91 |
|
|
m_result = SafeArrayAccessData( m_pSafeArray, (void HUGEP**)&m_arrayData);
|
92 |
|
|
m_access = m_result == S_OK ;
|
93 |
|
|
if(!m_access)
|
94 |
|
|
V_VT(m_input) = VT_EMPTY;
|
95 |
|
|
}
|
96 |
|
|
else
|
97 |
|
|
{
|
98 |
|
|
V_VT(m_input) = VT_EMPTY;
|
99 |
|
|
m_access = true;
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
inline HRESULT status(){return m_result;}
|
104 |
|
|
inline int getSize(int index = 0) {return (m_access&&m_pSafeArray)?m_pSafeArray->rgsabound[index].cElements:0;}
|
105 |
|
|
|
106 |
|
|
~SafeArray(){UnaccessData();}
|
107 |
|
|
|
108 |
|
|
operator T* () {assert(m_access);return m_arrayData;}
|
109 |
|
|
operator VARIANT () {assert(m_access);UnaccessData(); return *m_input;}
|
110 |
|
|
T & operator[](int i) {assert(m_access&&m_arrayData);return m_arrayData[i];}
|
111 |
|
|
private:
|
112 |
|
|
void UnaccessData(){
|
113 |
|
|
if(m_access){
|
114 |
|
|
m_result = SafeArrayUnaccessData( m_pSafeArray );
|
115 |
|
|
if(m_bCreate && m_result == S_OK)
|
116 |
|
|
V_ARRAY(m_input) = m_pSafeArray;
|
117 |
|
|
m_access = false;
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
bool m_bCreate;
|
122 |
|
|
bool m_access;
|
123 |
|
|
VARIANT *m_input;
|
124 |
|
|
VARIANT m_target;
|
125 |
|
|
T *m_arrayData ;
|
126 |
|
|
SAFEARRAY *m_pSafeArray;
|
127 |
|
|
SAFEARRAYBOUND m_rgsabound[1];
|
128 |
|
|
HRESULT m_result;
|
129 |
|
|
};
|
130 |
|
|
|
131 |
|
|
typedef SafeArray<double, VT_R8> SafeDoubleArray ;
|
132 |
|
|
typedef SafeArray<float, VT_R4> SafeFloatArray ;
|
133 |
|
|
typedef SafeArray<long, VT_I4> SafeLongArray ;
|
134 |
|
|
typedef SafeArray<BSTR, VT_BSTR> SafeBSTRArray ;
|
135 |
|
|
typedef SafeArray<LPDISPATCH, VT_DISPATCH> SafeDISPATCHArray;
|
136 |
|
|
typedef SafeArray<LPVARIANT, VT_VARIANT> SafeVARIANTArray;
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
/*
|
141 |
|
|
|
142 |
|
|
assing SafeArrays in C++
|
143 |
|
|
Be careful when managing SafeArray memory. If you receive a SafeArray from the SolidWorks product, you are responsible for destroying it. Also, if you pass a SafeArray to the SolidWorks product, SolidWorks does not destroy the SafeArray; you are responsible for destroying it.
|
144 |
|
|
|
145 |
|
|
See:
|
146 |
|
|
|
147 |
|
|
Return Values for information and examples of handling SafeArray and VARIANT return values.
|
148 |
|
|
|
149 |
|
|
A template class for VARIANT SafeArrays on the SolidWorks API Support Web site, http://www.solidworks.com/pages/services/APITips.html.
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
*/
|
154 |
|
|
|
155 |
|
|
#endif//SMARTVARS_H
|