Re: Passing an array of Strings from VB to C
This is a multi-part message in MIME format.
------=_NextPart_000_018C_01C77040.AAB1B7E0
Content-Type: text/plain;
charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable
SAFEARRAY **psaArray . double * indicates that caller expects you do =
create the array. You start you function with SafeArrayLock trying to =
lock nonexisting memory,. Your function should strat with =
SafeArrayCreate
"Bernard Delm=E9e" <bdelmee@advalvas._REMOVEME_.be> wrote in message =
news:4608e741$0$13863$ba620e4c@news.skynet.be...
Thanks gents for the useful links. I've now got a procedure
within my DLL which accesses correctly the string array elements
when called from VB6. The same test project migrated to
VB2005 Express crashes, though. The proc does not seem
to receive the expected SAFEARRAY** and trying to access
the structure returns random data (e.g absurdly huge number
of elements). Any suggestion welcome ;-(
--------> VB decl <--------
Private Declare Sub tst_str2 Lib "test_sl.dll" (str() As String)
--------> C impl <--------
extern "C" void __stdcall tst_str2( SAFEARRAY **psaArray )
{
char cbuf[80];
// lock before using
long l = SafeArrayLock(*psaArray);
// how many elements
long nelt = (*psaArray)->rgsabound[0].cElements;
wsprintf(cbuf, "There are %ld elements in this array", nelt);
MessageBox(0,cbuf,"# elements",0);
// access 3rd string in array for test purpose
// BSTR *pArrayElements = (BSTR *) (*psaArray)->pvData;
char **pArrayElements = (char **) (*psaArray)->pvData;
MessageBox(0, pArrayElements[2],"elt[2]",0);
// releasing the array
l = SafeArrayUnlock(*psaArray);
}
------=_NextPart_000_018C_01C77040.AAB1B7E0
Content-Type: text/html;
charset="ISO-8859-15"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; =
charset=iso-8859-15">
<META content="MSHTML 6.00.2900.3059" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2></FONT>SAFEARRAY =
<STRONG>**</STRONG>psaArray .
double * indicates that caller expects you do create the array. You =
start you
function with SafeArrayLock trying to lock nonexisting memory,. =
Your
function should strat with SafeArrayCreate</DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>"Bernard Delm=E9e" <</FONT><A
href="mailto:bdelmee@advalvas._REMOVEME_.be"><FONT face=Arial
size=2>bdelmee@advalvas._REMOVEME_.be</FONT></A><FONT face=Arial =
size=2>>
wrote in message </FONT><A
href="news:4608e741$0$13863$ba620e4c@news.skynet.be"><FONT =
face=Arial
size=2>news:4608e741$0$13863$ba620e4c@news.skynet.be</FONT></A><FONT =
face=Arial
size=2>...</FONT></DIV><FONT face=Arial size=2>> Thanks gents =
for the useful
links. I've now got a procedure<BR>> within my DLL which accesses =
correctly
the string array elements<BR>> when called from VB6. The same test =
project
migrated to<BR>> VB2005 Express crashes, though. The proc does not
seem<BR>> to receive the expected SAFEARRAY** and trying to =
access<BR>>
the structure returns random data (e.g absurdly huge number<BR>> of
elements). Any suggestion welcome ;-(<BR>> <BR>> --------> VB =
decl
<--------<BR>> Private Declare Sub tst_str2 Lib "test_sl.dll" =
(str() As
String)<BR>> <BR>> --------> C impl <--------<BR>> extern =
"C"
void __stdcall tst_str2( SAFEARRAY **psaArray )<BR>>
{<BR>> char cbuf[80];<BR>>
<BR>> // lock before
using<BR>> long l = =
SafeArrayLock(*psaArray);<BR>>
<BR>> // how many
elements<BR>> long nelt =
(*psaArray)->rgsabound[0].cElements;<BR>>
wsprintf(cbuf, "There are %ld elements in this array",
nelt);<BR>> MessageBox(0,cbuf,"#
elements",0);<BR>> <BR>> // access 3rd =
string in
array for test purpose<BR>> // BSTR =
*pArrayElements =
(BSTR *) (*psaArray)->pvData;<BR>> char
**pArrayElements = (char **)
(*psaArray)->pvData;<BR>> MessageBox(0,
pArrayElements[2],"elt[2]",0);<BR>> <BR>> =
//
releasing the array<BR>> l =
SafeArrayUnlock(*psaArray);<BR>> }</FONT></BODY></HTML>
------=_NextPart_000_018C_01C77040.AAB1B7E0--