CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    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

  2. #2
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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...

  4. #4
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    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

  5. #5
    Join Date
    Sep 2004
    Posts
    519

    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

  6. #6
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    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

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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:
    _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.

  8. #8
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    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

  9. #9
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    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

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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":
    _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

  11. #11
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    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...

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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 )

  13. #13
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    Re: string copy truncation

    nice article about the topic found in the MSDN here.

  14. #14
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    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

  15. #15
    Join Date
    Mar 2005
    Location
    Germany
    Posts
    29

    Re: string copy truncation

    ::SysAllocString

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured