Is this a valid assignment?

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 04:22:49


VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.
 
 
 

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 04:22:49

VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.

 
 
 

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 07:14:56

VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.
 
 
 

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 07:14:56

VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.
 
 
 

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 07:14:56

VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.
 
 
 

Is this a valid assignment?

Post by Mike » Wed, 27 Oct 2004 07:14:56

VariantInit(&pvar);
pvar.vt = VT_BSTR;
pvar.bstrVal = OLESTR ( "VBScript" ); //<---Here?

Is this a valid assignment?

Mike H.
 
 
 

Is this a valid assignment?

Post by jacob navi » Fri, 29 Oct 2004 07:15:21


I suppose so, if the definition of OLESTR yields a type
compatible with bstrVal member...
 
 
 

Is this a valid assignment?

Post by anonytmous » Fri, 29 Oct 2004 23:08:27


No. To allocate a BSTR you must use SysAllocString or one of its
sister functions. For the complete story on BSTRs see "Eric's Complete
Guide To BSTR Semantics":
< http://www.yqcomputer.com/ ;
Short URL: < http://www.yqcomputer.com/ ;

pvar.vt = VT_BSTR;
pvar.bstrVal = SysAllocString( OLESTR("VBScript") );

If you are calling COM objects you may want to check out the
DispHelper COM Helper Library:
< http://www.yqcomputer.com/ ;

There is a sample demonstrating use of the MS Script Control (just
guessing that you are trying to use this control):
< http://www.yqcomputer.com/ ;
Short URL: < http://www.yqcomputer.com/ ;

----
Note: Reply address is invalid.
 
 
 

Is this a valid assignment?

Post by Mike » Sat, 30 Oct 2004 00:52:26


Thanks, the reason I'm asking is that assigning it in this manner works when
compiled with PellesC, but not with LCC. However LCC does work using
SysAllocString as expected, but LCC also works if I use malloc and
MultiByteToWideChar?

Just wondering why?

Thanks,
Mike H.
 
 
 

Is this a valid assignment?

Post by jacob navi » Sat, 30 Oct 2004 09:16:34


OLESTR is
#define OLESTR(str) L##str

then
pvar.bstrVal = OLESTR ( "VBScript" );
is
pvar.bstrVal = L"VBScript";

This should work, but maybe it requires a pointer to shared memory to
work with VB who knows.
 
 
 

Is this a valid assignment?

Post by anonytmous » Sun, 31 Oct 2004 05:05:37


That's the nature of incorrect code. Consider the following code:

char abc[10];
abc[12] = 'z';

On some compilers with some programs this code will crash, while on
others this code will "work". The fact that code may "work" on one
compiler or another does not make it correct. Your code is incorrect.

I can offer some conjecture on why your incorrect code seems to "work"
with Pelles-C but not with LCC-WIN32. A BSTR is a counted string. This
enables BSTRs to be used to pass binary data. The length is stored in
a DWORD that is placed immediately before the start of the string. A
wide string literal is not preceded by a DWORD length. I would guess,
that by chance, you are getting a "small" value for the length in
Pelles-C while, by chance, getting a "large" value in LCC-WIN32. Thus,
when this garbage value is used, say by the COM framework to marshal a
string across processes, the small value of Pelles-C does not cause a
trip into uncommited memory while the large value of LCC-WIN32 does.

Similarly, with malloc, you are likely getting a "reasonable" value
preceding your string.

This is certainly not the fault of LCC-WIN32. In fact, it is
preferable that incorrect code crashes immediately, rather than
receiving a call from your customer in six months time when they
install the latest OS update and your program starts crashing.

An OLESTR (which is a wide unicode string in Win32/64) is NOT a BSTR.
A BSTR MUST be allocated with one of the SysAlloc* functions. These
are the rules, break them at your own risk.

All this is covered in more detail in the BSTR link I posted above.


----
Note: Reply address is invalid.
 
 
 

Is this a valid assignment?

Post by jacob navi » Sun, 31 Oct 2004 06:30:56


[snip]

I didn't know this. Thanks for the info...
 
 
 

Is this a valid assignment?

Post by Mike » Sun, 31 Oct 2004 11:20:05

"anony*mouse" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
What you are saying makes sense, I personally always use the Sysalloc
functions myself for the very reason you stated. (It always works) I see a
lot of examples such as this one that uses OLESTR in place of BSTR's that I
thought maybe there were some kind of exceptions as to when they could be
used that I didn't know about :) Your explanation clears things up as to why
one might work and one might not.

Mike H.