|
-
March 22nd, 2005, 08:44 AM
#1
string copy truncation
I have the following:
Code:
char cmdSQL[256];
int stLen = 0;
stLen = sprintf(cmdSQL,"updatemachineData_h '%s','%s',%d,%d,'%s',
'%s',%f,%f,%f,%d,%d,%f,%f,%f,%f,%f,'%s'",
nbr1, nmid1, ntme, njdt, nlitm1, naitm1, ncyc, na10, njobavg ,
nshftprd, njobprd, nhrsup, nruneff, ncyceff, nneteff, nshot, nstat1);
_bstr_t sndSQL = (_bstr_t)cmdSQL;
After the sprintf command, the string length is 186 and the CORRECT output is:
Code:
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 '
When I try to pass this to the _bstr_t variable sndSQL, it gets truncated to:
Code:
updatemachineData_h 'RKR ','020',83300,105081,'01-00300-0350','C400 CU BL 4928TW (VL) '
,7.987000,7.987000,7.900000,7968,7492
I have been trying to see what the limit is for the _bstr_t data type and also how to handle this, but need a push in the right direction.
Any help??
There are 10 types of people in the world, those that understand binary and those that don't. 
Using VS 2010
-
March 22nd, 2005, 09:06 AM
#2
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.
-
March 22nd, 2005, 09:12 AM
#3
Re: string copy truncation
... or try to replace this string:
_bstr_t sndSQL = (_bstr_t)cmdSQL;
with:
Code:
_bstr_t sndSQL (cmdSQL);
to use a copy constructor rather than casting...
-
March 22nd, 2005, 09:13 AM
#4
Re: string copy truncation
Agh.. Thanks. I will give it a shot and see if that helps.
There are 10 types of people in the world, those that understand binary and those that don't. 
Using VS 2010
-
March 22nd, 2005, 09:19 AM
#5
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
-
March 22nd, 2005, 09:28 AM
#6
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 ?
There are 10 types of people in the world, those that understand binary and those that don't. 
Using VS 2010
-
March 22nd, 2005, 09:47 AM
#7
Re: string copy truncation
 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:
_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.
-
March 22nd, 2005, 10:23 AM
#8
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:
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
-
March 22nd, 2005, 10:39 AM
#9
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.
There are 10 types of people in the world, those that understand binary and those that don't. 
Using VS 2010
-
March 22nd, 2005, 10:46 AM
#10
Re: string copy truncation
 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":
_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
-
March 22nd, 2005, 11:00 AM
#11
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...
-
March 22nd, 2005, 11:06 AM
#12
Re: string copy truncation
 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 )
-
March 22nd, 2005, 11:06 AM
#13
Re: string copy truncation
nice article about the topic found in the MSDN here.
-
March 22nd, 2005, 11:07 AM
#14
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.
There are 10 types of people in the world, those that understand binary and those that don't. 
Using VS 2010
-
March 22nd, 2005, 11:12 AM
#15
Re: string copy truncation
::SysAllocString
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|