tomercagan
New member
2-D Safearray
I am trying to write a DLL to be called from VB. Thus I had to use SAFEARRAY. I have a problem that when I am accessing all the elements in the array I can change their values but when I try to access a specific element it seems not to accept the new value I try to assign. For example, I have these functions: void Set2DSafeArrayPtr(SAFEARRAY * psaiInOut, long val) { long ai[2]; int x,y; LRESULT hres; int xMin = psaiInOut->rgsabound[0].lLbound; int xMax = xMin + (int)psaiInOut->rgsabound[0].cElements - 1; int yMin = psaiInOut->rgsabound[1].lLbound; int yMax = yMin + (int)psaiInOut->rgsabound[1].cElements - 1; // Set up dimension array and pointer to receive value. long * piInOut; if (hres = SafeArrayLock(psaiInOut)) throw hres; for (x = xMin; x <= xMax; x++) { ai[0] = x; for (y = yMin; y <= yMax; y++) { ai[1] = y; hres = SafeArrayPtrOfIndex(psaiInOut, ai, (void **)&piInOut); if (hres) throw hres; // Equivalent to: aiInOut(x, y) = aiInOut(x, y) + 1. (*piInOut) = val; } } if (hres = SafeArrayUnlock(psaiInOut)) throw hres; } void SetElementPtr(SAFEARRAY * psaiInOut, int x, int y, long val) { LRESULT hres; long * piInOut; long ai[2]; if (hres = SafeArrayLock(psaiInOut)) throw hres; ai[0] = x; ai[1] = y; hres = SafeArrayPtrOfIndex(psaiInOut, ai, (void **)&piInOut); if (hres) throw hres; (*piInOut) = val; if (hres = SafeArrayUnlock(psaiInOut)) throw hres; } my program void main{ ... SAFEARRAYBOUND bDim[2]; bDim[0].lLbound = 0; bDim[0].cElements = 6; bDim[1].lLbound = 0; bDim[1].cElements = 6; psaiNew = SafeArrayCreate(VT_I4, 2, bDim); if(psaiNew == NULL){ cout << "Unable to initialize array. Not enough memory" << endl; return; } hres = SafeArrayLock(psaiNew); Set2DSafeArrayPtr(psaiNew, 15); SetElementPtr(psaiNew, 1,2,30); hres = SafeArrayUnlock(psaiNew); ... } The Set2DSafeArrayPtr seems to work fine but the SetElementPtr doesn't seem to work. Any idea why? What am I doing wrong? I'd also like to know of a good place I can find references/examples (other then MSDN)...