How to create a SAFEARRAY??

Posted on 2003-02-19
Medium Priority
Last Modified: 2008-03-06
Hey everyone,

Here is a quick question for all you C++ experts... I need to create a SAFEARRAY.. but not exactly sure how to do it.

I'm trying to call a VB DLL method that access a store procedure of a database.. I believe it should work.. but I don't know how to create a SAFEARRAY... can someone help please...

VB DLL method.. this is how it looks like
Set rsClaimErrors = objDBCalls.RunSPReturnRS("sp_get_claim_errors", _
                                                Array("@i_USER_ID", 200, 200, strClaimID))

in the C++ side this is what the method requires...

UB92Format = pDBCalls->RunSPReturnRS(_bstr_t spSTR, SAFEARRAY **params);

I know what to put in the _bstr_t part... but I need to create a SAFEARRAY in C++ that would store the simple value as in the VB DLL method.. how can I do this..

Question by:Blade007
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7983622
What is a SAFEARRAY? What does it store?

Author Comment

ID: 7983990
A SAFEARRAY is used in COM to pass array of variant datatype... I believe that's what I read up on.. I could be wrong.

Accepted Solution

YuriPutivsky earned 200 total points
ID: 7984148
In you case you need VARIANT vector as a SafeArray

unsigned int count = 4; // in your case
SAFEARRAY sa = SafeArrayCreateVector(VT_VARIANT, 0, 4);
if (!sa)
// handle error here

// How to assign the particular element in a Safe Array?
// OK, here is a code example
// lock array
VARIANT* pvar = 0;

// get the pointer to the first element
long lx = 1;
SafeArrayPtrOfIndex(sa, &lx, (void**)&pvar);
// assign here for example string
pvar->vt = VT_BSTR;
pvar->bstrVal = ::SysAllocString(L"@i_USER_ID");

// get the pointer to the second element
lx = 2;
SafeArrayPtrOfIndex(sa, &lx, (void**)&pvar);
// assign here for example long
pvar->vt = VT_I4;
pvar->lVal = 200;

// ... and so on

// unlock array

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question