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