Re: string copy truncation
too lazy to count the chars... but I asuume it must be 128 in the truncated string.
Why the guess? BSTR is UNICODE or WCHAR or whatever; and it is likely that the framework converts each char to a WCHAR (2 bytes).
Declare the string as BSTR or something of the kind and you're set.
Re: string copy truncation
... or try to replace this string:
Quote:
_bstr_t sndSQL = (_bstr_t)cmdSQL;
with:
Code:
_bstr_t sndSQL (cmdSQL);
to use a copy constructor rather than casting...
Re: string copy truncation
Agh.. Thanks. I will give it a shot and see if that helps.
Re: string copy truncation
Not sure what the limit of BSTR is but it's high. Much higher than 256.
Anyway:
(from above)
Code:
_bstr_t sndSQL = (_bstr_t)cmdSQL;
Should be:
Code:
_bstr_t sndSQL(cmdSQL);
Hope this helps
Re: string copy truncation
No go...
I used the
Code:
_bstr_t sndSQL(cmdSQL);
but the string is still truncated in the same location (original string length: 186, new string length 108, I counted the characters).
I am really confused now. I am well below the data type limits and the string is still not copying correctly...
Is there anything else I can supply to help with this? I shouldn't need to do any wide character conversion, do I ?
Re: string copy truncation
Quote:
Originally Posted by rick7423
No go...
I used the
Code:
_bstr_t sndSQL(cmdSQL);
but the string is still truncated in the same location (original string length: 186, new string length 108, I counted the characters).
I am really confused now. I am well below the data type limits and the string is still not copying correctly...
Is there anything else I can supply to help with this? I shouldn't need to do any wide character conversion, do I ?
It is strange...
This ]_bstr_t must work. From MSDN:
Quote:
_bstr_t( char* s2 ) Constructs a _bstr_t object by calling SysAllocString to create a new BSTR object and encapsulate it. This constructor first performs a multibyte to Unicode conversion.
Well..., try to create _bstr_t object using the "plain_ text_copy" of your string:
Code:
_bstr_t sndSQL("updatemachineData_h 'RKR ','020',83300,105081,'01-00300-0350','C400 CU BL 4928TW (VL) '
,7.987000,7.987000,7.900000,7968,749272,2.185000,100.000000,
100.964066,1.439381,2.660000,'03000 '");
or try to use CString instead of char array.
Re: string copy truncation
when passing a string to a BSTR, you have to specify it with a cpital L
Code:
bstrVal = ::SysAllocString (L"string%20content");
Microsoft says:
Quote:
Multibyte and Wide Characters
A multibyte character is a character composed of sequences of one or more bytes. Each byte sequence represents a single character in the extended character set. Multibyte characters are used in character sets such as Kanji.
Wide characters are multilingual character codes that are always 16 bits wide. The type for character constants is char; for wide characters, the type is wchar_t. Since wide characters are always a fixed size, using wide characters simplifies programming with international character sets.
The wide-character-string literal L"hello" becomes an array of six integers of type wchar_t.
{L'h', L'e', L'l', L'l', L'o', 0}
The Unicode specification is the specification for wide characters. The run-time library routines for translating between multibyte and wide characters include mbstowcs, mbtowc, wcstombs, and wctomb.
obviously a simple cast fails :(
Re: string copy truncation
Man I feel like a complete nub, I tried all of these suggestions and nothing so far makes any difference. The string is still truncated at the same place.
I tried:
Code:
_bstr_t sndSQL(cmdSQL);
_bstr_t sndSQL("<string>");
_bstr_t sndSQL(L"<string>");
_bstr_t sndSQL(L(cmdSQL));
all with the same results, string is truncated at same position.
FYI, I need the _bstr_t to pass into another function. The function prototype is:
bool (doStuff(_bstr_t sql, bool svrAvail);
Thanks for all the help so far.
Re: string copy truncation
Quote:
Originally Posted by Ernst Lustig
when passing a string to a BSTR, you have to specify it with a cpital L
Code:
bstrVal = ::SysAllocString (L"string%20content");
Microsoft says:
obviously a simple cast fails :(
I am sorry, Ernst, but it is NOT correct for a non-unicode build. You do NOT have to "specify it with a capital L". See the MSDN article "_bstr_t::_bstr_t":
Quote:
_bstr_t::_bstr_tMicrosoft Specific
_bstr_t( ) throw( );
_bstr_t( const _bstr_t& s1 ) throw( );
_bstr_t( const char* s2 ) throw( _com_error );
_bstr_t( const wchar_t* s3 ) throw( _com_error );
_bstr_t( const _variant_t& var ) throw ( _com_error );
_bstr_t( BSTR bstr, bool fCopy ) throw ( _com_error );
Parameters
s1
a _bstr_t object to be copied
s2
a multibyte string
s3
a Unicode string
var
a _variant_t object
bstr
an existing BSTR object
fCopy
if false, the bstr argument is attached to the new object without making a copy by calling SysAllocString.
Remarks
Constructs a _bstr_t object.
_bstr_t( ) Constructs a default _bstr_t object that encapsulates a NULL BSTR object.
_bstr_t( _bstr_t& s1 ) Constructs a _bstr_t object as a copy of another. This is a “shallow” copy, which increments the reference count of the encapsulated BSTR object instead of creating a new one.
_bstr_t( char* s2 ) Constructs a _bstr_t object by calling SysAllocString to create a new BSTR object and encapsulate it. This constructor first performs a multibyte to Unicode conversion.
_bstr_t( wchar_t* s3 ) Constructs a _bstr_t object by calling SysAllocString to create a new BSTR object and encapsulates it.
........
There is a link: http://msdn.microsoft.com/library/de...lusbstrust.asp
Re: string copy truncation
hmmm...
I do OLE automation once in a while and since I discovered the 'L' specifyer, some of my problems had gone. As long as my programs work, I do not care, what Mick Rosoft says...
Re: string copy truncation
Quote:
Originally Posted by Ernst Lustig
hmmm...
I do OLE automation once in a while and since I discovered the 'L' specifyer, some of my problems had gone. As long as my programs work, I do not care, what Mick Rosoft says...
I have also used ADO/OLE automation in one of my project and also without problems for both ANSI and UNICODE builds (and without 'L' specifyer' because I always use _T macro ;) )
Re: string copy truncation
nice article about the topic found in the MSDN here.
Re: string copy truncation
Since this _bstr_t( const char* s2 ) throw( _com_error ); throws a comm error, I tried this:
Code:
_bstr_t sndSQL;
try{
_bstr_t ssql(cmdSQL);
sndSQL = (L"updatemachineData_h 'RKR ','020',83300,105081,'01-00300-0350','C400 CU BL 4928TW (VL) ',7.987000,7.987000,7.900000,7968,749272,2.185000,100.000000,100.964066,1.439381,2.660000,'03000 '");
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bs = _bstr_t(" Error (ExecuteX):\r\n")
+ _bstr_t(e.Error()) + _bstr_t(" Msg: ")
+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
+ _bstr_t(e.Description());
AfxMessageBox(bs);
}
No com error is thrown, although the watch window entry for this element states (m_str): 0x00000000 "" Expression cannot be evaluated.
Re: string copy truncation