My program uses an external ocx library and receives data through it. The code below shows how it works.
VARIANT varArrItem, varArrData;
ocx_instance.GetItemArr(real, &varArrItem); // the library provides GetItemArr
// 1) receives data
long lLBound, lUBound;
VARIANT varItem, varData;
long index[2];
index[0] = 0;
index[1] = 0;
COleSafeArray* pSafeItemArr = (COleSafeArray*)&varArrItem; // 2) casts varArrItem to COleSafeArray
CString strItem;
CStringArray arrItem;
pSafeItemArr->GetLBound(1, &lLBound);
pSafeItemArr->GetUBound(1, &lUBound);
int nItemCnt = (lUBound - lLBound + 1);
for (int i = 0; i < nItemCnt; i++)
{
index[0] = i;
VariantInit(&varItem);
pSafeItemArr->GetElement(index, (void *)&varItem); // 3) gets its values using GetElement
strItem = varItem.bstrVal;
arrItem.Add(strItem);
VariantClear(&varItem);
}
A big problem of the program is that this code is run whenever new data arrives, which is quite often, and it consumes a lot of resources. So, I'd like to simplify the code and get just contents of varArrItem, as strings or an array of structs of strings, for example.
varArrItem.vt gives me 8204 and it's said that it consists of 8192(VT_ARRAY) and 12(VT_VARIANT). I'm stuck here and don't know what to do after this. How can I simply get what's in them? Is it possible to access to them without using COleSafeArray?
You don't NEED to use COleSafeArray, it is just a wrapper for convenience. You could just extract the SAFEARRAY* pointer directly from varArrItem and then use the SafeArray APIs directly: SafeArrayGet(L|U)Bound(), SafeArrayGetElement(), etc, though if performance is important then consider using SafeArrayAccessData() to access the VARIANT[] array directly, and thus its BSTR pointers. The less copying of data you do, the faster the code will run. The only copy of data this code actually needs to make is the assignment of the initial VARIANT and each CString you add to the CStringArray:
VARIANT varArray;
ocx_instance.GetItemArr(real, &varArray);
LPSAFEARRAY psa = varArrar.parray;
LONG lLBound, lUBound;
SafeArrayGetLBound(psa, 1, &lLBound);
SafeArrayGetUBound(psa, 1, &lUBound);
CStringArray arrItem;
VARIANT *varArrayData;
if (SUCCEEDED(SafeArrayAccessData(psa, (void**) &varArrayData)))
{
int nItemCnt = (lUBound - lLBound + 1);
for (int i = 0; i < nItemCnt; i++)
{
CString strItem = varArrayData[i].bstrVal;
arrItem.Add(strItem);
}
SafeArrayUnaccessData(psa);
}
Related
I'm new to C++ (I usually work on Java), and I'm trying to convert a ::CORBA::LongSeq object to a long * in C++, in order to perform operations on it afterwards.
So basically, what I tried is to do that :
long * Sample (const ::CORBA::LongSeq& lKeys) {
long nbElts = lKeys.length();
long * lCles = NULL;
for(int iIndex = 0; iIndex < nbElts; iIndex++) {
lCles[iIndex] = (long) lFCKey[iIndex];
}
return lCles;
}
And what happens is that I can retrieve the length of lKeys (so it should be looking at the right location, as far as I can tell), but then I get an access violation exception when I enter inside the for loop.
0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF
I'm not sure of what I'm doing wrong though... Anyone has an idea ?
Here is one solution, and you don't get into the mess with pointers:
#include <vector>
std::vector<long> Sample (const ::CORBA::LongSeq& lKeys)
{
long nbElts = lKeys.length();
std::vector<long> lCles(nbElts);
for(int iIndex = 0; iIndex < nbElts; ++iIndex)
lCles[iIndex] = (long) lFCKey[iIndex];
return lCles;
}
This is guaranteed to work correctly, if the number of elements is correct.
Since you say you know Java, then a std::vector<long> would be the equivalent to a few of the Java containers that store sequences of values. For example, you can get the return value and call the vector's data() function to get you a long * that points to the vector's internal buffer.
But overall, get out of the pointer business (or try to limit their usage).
Edit: The comment stated to use CORBA::Long. So here it is:
std::vector<CORBA::Long> Sample (const ::CORBA::LongSeq& lKeys)
{
long nbElts = lKeys.length();
std::vector<CORBA::Long> lCles(nbElts);
for(int iIndex = 0; iIndex < nbElts; ++iIndex)
lCles[iIndex] = lFCKey[iIndex];
return lCles;
}
The difference between Java and C++ is that you have to manage your memory yourself (in most cases).
The error you get is that you try to assign things to an uninitialized variable (lCles), and returning a local variable. The local variable lCles which is stored on the stack will be "destroyed" once you leave the method.
One suggestion of how to do this could be something like this:
long* lCles = new long[lKeys.length()];
for(int iIndex = 0; iIndex < nbElts; iIndex++) {
lCles[iIndex] = (long) lFCKey[iIndex];
}
return lCles;
The important part in the method calling this code is to then release the memory held by this lCles by doing a
delete [] lCles; // or whatever the name of the variable is.
when done.
Like this:
long * l = Sample(lkeys);
// Do your stuff here
delete [] l;
(Using std::vector as suggested in another answer is actually preferred, since you don't have to do memory management by yourself.)
There are two things wrong here.
1) You attempt to use lCles before you have initialised it:
long * lCles = NULL;
..
lCles[iIndex]
This causes the access violation inside the for loop.
2) You return a pointer, lCles which is only declared locally:
return lCles;
This means that it goes out of scope when the function exits, and it then becomes invalid.
I am creating a dynamic array of LPCWSTR, and want to assign values at run time.
I have following code :
cin>>count
LPCWSTR * lpwcstrArray = new LPCWSTR[count]();
for (int i = 0; i < count; i++)
{
// some logic to create different wstring on each iteration
wstring tempWString = L"somerandomstuff";
lpwcstrArray[i] = reinterpret_cast<LPSWSTR>tempWString.c_str();
}
Now if i access lpwcstrArray - all the indexs point at data of last string that was assigned.
I know this is not correct way to assign values, but i do not know the correct way.
wstring tempWString is created and destroyed with each iteration of the loop.
You have dangling pointers in your lpwcstrArray and are experiencing undefined behaviour when you access one of them.
You need to allocate the space yourself or use a std::wstring as the array type instead of a LPCWSTR.
You are storing pointers that point at the internals of temporary std::wstring objects. When those objects are destroyed on each loop iteration, your array is left with danging pointers. You need to dynamically allocate the individual strings instead, eg:
std::cin >> count
LPWSTR *lpwstrArray = new LPWSTR[count];
for (int i = 0; i < count; i++)
{
// some logic to create different wstring on each iteration
std::wstring tempWString = L"somerandomstuff";
LPWSTR str = new WCHAR[tempWString.length()+1];
const wchar_t *p = tempWString.c_str();
std::copy(p, p+tempWString.length(), str);
lpwstrArray[i] = str;
}
// use lpwstrArray as needed...
// don't forget to free the memory when you are done using it...
for (int i = 0; i < count; i++)
delete[] lpwstrArray[i];
delete[] lpwstrArray;
Depending on what you are really trying to accomplish, something more like the following would be safer, at least if you just need read-only access to the strings (which you likely do, as the C in LPCWSTR stands for const, so the user of the array is not going to be modifying them):
std::cin >> count
std::vector<std::wstring> wstrArray(count);
for (int i = 0; i < count; i++)
{
// some logic to create different wstring on each iteration
wstrArray[i] = L"somerandomstuff";
}
std::vector<LPWSTR> lpwstrArray(count);
for (int i = 0; i < count; i++)
lpwstrArray[i] = const_cast<wchar_t*>(wstrArray[i].c_str());
// use lpwstrArray as needed. if you need to pass it where an
// LPWSTR* is expected, you can use &lpwstrArray[0] for that...
// lpwstrArray and wstrArray will be freed automatically
// when they go out of scope...
Try the approach
std::wstring ws(_T("Hello"));
LPCTSTR lps= (LPCTSTR)(ws.c_str());
TRACE(lps);
Notes:
You should not use the W types directly (ex: LPCWSTR). Use the T types instead (ex: LPCTSTR). Why? Because they will automatically translate to the version they should be (LPCSTR for non-Unicode / ASCII; LPCWSTR for Unicode), depending on your project.
For the same reason you should surround your strings with _T() or precede them with an L.
Try to dive deep using "Go To Definition" successively beginning on LPCTSTR
See also the _tcscpy_s function documentation
I'm writing a C++ DLL to be accessed from Excel VBA (it is only completing mathematical equations and doesn't need to be accessed from the worksheet). The DLL is intended to replace current VBA code and I want to write it in C/C++ for performance.
Since I'm am using existing code and simply want to replace the current VBA function with the new DLL I have to use VBA Variants containing single vector arrays. The following is an abbreviated example of the VBA calling code:
Sub VBACaller()
Dim InputR0(1 To 5) As Variant, vResult As Variant
InputR0(1) = 26.8
InputR0(2) = 27.8
InputR0(3) = 28.8
InputR0(4) = 29.8
vResult = ReadArrayVBA(InputR0)
End Sub
I can pass this Variant array to my C++ function either ByVal or ByRef as a VARIANT data type and it appears to be received correctly. My problems occur when I try to read the contents of the array after converting it into a SAFEARRAY using SafeArrayAccessData. The SAFEARRAY conversion seems to work but the contents of the array aren't correct. The following is my C++ code:
VARIANT __stdcall ReadArrayByRef(VARIANT *input0)
{
//Variable to assign value from Array
double d_m = 0;
//Check if this is a variant type or not
if (V_VT(input0) & VT_ARRAY)
{
SAFEARRAY* pSafeArrayInput0 = NULL;
pSafeArrayInput0 = V_ARRAY(input0);
double* pVals;
HRESULT hr = SafeArrayAccessData(pSafeArrayInput0, (void **)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lLBound = -1, lUBound = 1; // get array bounds
SafeArrayGetLBound(pSafeArrayInput0, 1, &lLBound);
SafeArrayGetUBound(pSafeArrayInput0, 1, &lUBound);
if (lLBound > -1 && lUBound > -1)
{
d_m = pVals[1];
}
SafeArrayUnaccessData(pSafeArrayInput0);
}
}
//Output
VARIANT v;
VariantInit(&v);
v.vt = VT_R8;
v.dblVal = d_m;
return v;
}
The examples I've found seem to indicate that the .parray should hold the data, however inspection of input0 indicates that it is in .pparray. If I try to assign pSafeArrayInput0 = input0.pparray it gives an error. The value d_m returns is along the lines of 1.05319234616515E-307.
If I change the input to ByVal then I can access the elements of the array correctly using the following C++ code (the only difference being the address of input0 is being accessed by the SAFEARRAY.
VARIANT __stdcall ReadArrayByVal(VARIANT input0)
{
//Variable to assign value from Array
double d_m = 0;
//Check if this is a variant type or not
if (V_VT(&input0) & VT_ARRAY)
{
SAFEARRAY* pSafeArrayInput0 = NULL;
pSafeArrayInput0 = V_ARRAY(&input0);
double* pVals;
HRESULT hr = SafeArrayAccessData(pSafeArrayInput0, (void **)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lLBound = -1, lUBound = 1; // get array bounds
SafeArrayGetLBound(pSafeArrayInput0, 1, &lLBound);
SafeArrayGetUBound(pSafeArrayInput0, 1, &lUBound);
if (lLBound > -1 && lUBound > -1)
{
d_m = pVals[1];
}
SafeArrayUnaccessData(pSafeArrayInput0);
}
}
VARIANT v;
VariantInit(&v);
v.vt = VT_R8;
v.dblVal = d_m;
return v;
}
All the examples I've found indicate that passing the VARIANT input by a pointer should works per my ReadArrayByRef function (Ie http://support.microsoft.com/kb/167668 which is slightly different but the same at the points I'm after).
What am I doing wrong in my ByRef function?
You should check the VT_BYREF flag, and if it is set, dereference the pointer to access the array like so:
pSafeArrayInput0 = *V_ARRAYREF(input0);
I created a function that returns an error code (ErrCode enum) and pass two output parameters. But when I print the result of the function, I don't get the correct values in the array.
// .. some codes here ..
ErrCode err;
short lstCnt;
short lstArr[] = {};
err = getTrimmedList(lstArr, &lstCnt);
// list returned array (for comparison)
for (int i=0; i<lstCnt; ++i)
printf("lstArr[%3d] = %d", i, lstArr[i]);
// .. some codes here ..
The getTrimmedList function is like this:
ErrCode getTrimmedList(short* vList, short* vCnt)
{
short cnt;
ErrCode err = foo.getListCount(FOO_TYPE_1, &cnt);
if (NoError!=err) return err;
short* list = new short [cnt];
short total = 0;
for (short i=0; i<cnt; ++i)
{
FooBar bar = foo.getEntryByIndex(FOO_TYPE_1, i);
if (bar.isDeleted) continue;
list[total] = i;
++total;
}
*vCnt = total;
//vList = (short*)realloc(index, sizeof(short)*total);
vList = (short*)malloc(sizeof(short)*total);
memcpy(vList, list, sizeof(short)*total)
// list returned array (for comparison)
for (int i=0; i<lstCnt; ++i)
printf("lstArr[%3d] = %d", i, lstArr[i]);
return NoError;
}
where:
foo is an object that holds arrays of FooBar objects
foo.getListCount() returns the number of objects with type FOO_TYPE_1
FOO_TYPE_1 is the type of object we want to take/list
foo.getEntryByIndex() returns the ith FooBar object with type FOO_TYPE_1
bar.isDeleted is a flag that tells if bar is considered as 'deleted' or not
What's my error?
Edit:
Sorry, I copied a wrong line. I commented it above and put the correct line.
Edit 2
I don't have control over the returns of foo and bar. All their function returns are ErrCode and the outputs are passed through parameter.
Couple of questions before I can answer your post...
Where is "index" defined in:
vList = (short*)realloc(index, sizeof(short)*total);
Are you leaking the memory associated with:
short* list = new short [cnt];
Is it possible you have accidentally confused your pointers in memory allocation? In any case, here is an example to go from. You have a whole host of problems, but you should be able to use this as a guide to answer this question as it was originally asked.
WORKING EXAMPLE:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int getTrimmedList(short** vList, short* vCnt);
int main ()
{
// .. some codes here ..
int err;
short lstCnt;
short *lstArr = NULL;
err = getTrimmedList(&lstArr, &lstCnt);
// list returned array (for comparison)
for (int i=0; i<lstCnt; ++i)
printf("lstArr[%3d] = %d\n", i, lstArr[i]);
// .. some codes here ..
return 0;
}
int getTrimmedList(short** vList, short* vCnt)
{
short cnt = 5;
short* list = new short [cnt];
short* newList = NULL;
short total = 0;
list[0] = 0;
list[1] = 3;
list[2] = 4;
list[3] = 6;
total = 4;
*vCnt = total;
newList = (short*)realloc(*vList, sizeof(short)*total);
if ( newList ) {
memcpy(newList, list, sizeof(short)*total);
*vList = newList;
} else {
memcpy(*vList, list, sizeof(short)*total);
}
delete list;
return 0;
}
You have serious problems.
For starters, your function has only one output param as you use it: vCnt.
vList you use as just a local variable.
realloc is called with some index that we kow nothing about, not likely good. It must be something got from malloc() or realloc().
The allocated memory in vList is leaked as soon as you exit getTrimmedList.
Where you call the function you pass the local lstArr array as first argument that is not used for anything. Then print the original, unchanged array, to bounds in cnt, while it has 0 size still -- behavior is undefined.
Even if you managed to pass that array by ref, you could not reassign it to a different value -- C-style arrays can't do that.
You better use std::vector that you can actually pass by reference and fill in the called function. eliminating the redundant size and importantly the mess with memory handling.
You should use std::vector instead of raw c-style arrays, and pass-by-reference using "&" instead of "*" here. Right now, you are not properly setting your out parameter (a pointer to an array would look like "short **arr_ptr" not "short *arr_ptr", if you want to be return a new array to your caller -- this API is highly error-prone, however, as you're finding out.)
Your getTrimmedList function, therefore, should have this signature:
ErrCode getTrimmedList(std::vector<short> &lst);
Now you no longer require your "count" parameters, as well -- C++'s standard containers all have ways of querying the size of their contents.
C++11 also lets you be more specific about space requirements for ints, so if you're looking for a 16-bit "short", you probably want int16_t.
ErrCode getTrimmedList(std::vector<int16_t> &lst);
It may also be reasonable to avoid requiring your caller to create the "out" array, since we're using smarter containers here:
std::vector<int16_t> getTrimmedList(); // not a reference in the return here
In this style, we would likely manage errors using exceptions rather than return-codes, however, so other things about your interface would evolve, as well, most likely.
how to iterate through C++ safearray pointer to pointer and access its elements.
I tried to replicate the solution posted by Lim Bio Liong
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/022dba14-9abf-4872-9f43-f4fc05bd2602
but the strangest thing is that the IDL method signature comes out to be
HRESULT __stdcall GetTestStructArray([out] SAFEARRAY ** test_struct_array);
instead of
HRESULT __stdcall GetTestStructArray([out] SAFEARRAY(TestStruct)* test_struct_array);
Any ideas?
thanks in advance
Safearrays are created with SafeArrayCreate or SafeArrayCreateVector, but as you ask about iterating over a SAFEARRAY, let's say you already have a SAFEARRAY returned by some other function. One way is to use SafeArrayGetElement API which is especially convenient if you have multidimensional SAFEARRAYs, as it allows, IMO, a bit easier specifying of the indices.
However, for vectors (unidimensional SAFEARRAY) it is faster to access data directly and iterate over the values. Here's an example:
Let's say it's a SAFEARRAY of longs, ie. VT_I4
// get them from somewhere. (I will assume that this is done
// in a way that you are now responsible to free the memory)
SAFEARRAY* saValues = ...
LONG* pVals;
HRESULT hr = SafeArrayAccessData(saValues, (void**)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lowerBound, upperBound; // get array bounds
SafeArrayGetLBound(saValues, 1 , &lowerBound);
SafeArrayGetUBound(saValues, 1, &upperBound);
long cnt_elements = upperBound - lowerBound + 1;
for (int i = 0; i < cnt_elements; ++i) // iterate through returned values
{
LONG lVal = pVals[i];
std::cout << "element " << i << ": value = " << lVal << std::endl;
}
SafeArrayUnaccessData(saValues);
}
SafeArrayDestroy(saValues);
MSDN SafeArrayGetElement function gives you a code snippet on using SafeArrayGetElement to obtain individual object to array.
SAFEARRAY structure and SafeArray* functions explain the available API.
In ATL/MFC project you would want to use wrapper classes e.g. CComSafeArray to make things simpler and easier. See Simplifying SAFEARRAY programming with CComSafeArray on this.