CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2007
    Posts
    23

    Very Strange behaviour with LPCTSTR

    Hi all,

    I've got a very strange thing happening and cannot figure it out.

    In the following code the first messagebox displays "SUCCESS" and the second "not equal too". What could be the reason why it would enter the loop? I made some changes to the build properties - could it be that?

    Code:
    LPCSTR strConnect = "SUCCESS";//pEBUF->Connect();
    AfxMessageBox((strConnect));
    			
    if (strConnect != "SUCCESS")
    {
          	AfxMessageBox("not equal too");
    }
    Thanks
    SMSDude

  2. #2
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Very Strange behaviour with LPCTSTR

    You Cant Compare LPCTSTR Like That.

    Do It Like So...

    if(lstrcmp(strConnect, "SUCCESS") == 0)
    {
    ...
    }

  3. #3
    Join Date
    Jun 2007
    Posts
    23

    Re: Very Strange behaviour with LPCTSTR

    Thanks bitshifter420

    I'm just confused as to why it worked before...

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Very Strange behaviour with LPCTSTR

    Quote Originally Posted by SMSdude
    I'm just confused as to why it worked before...
    It was just your delusion that "it worked"...
    Victor Nijegorodov

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: Very Strange behaviour with LPCTSTR

    Quote Originally Posted by SMSdude
    Thanks bitshifter420

    I'm just confused as to why it worked before...
    There are actually times when something such as you describe will 'work' although it's not proper coding. It all depends on how the compiler deals with static strings. Since you have two static strings defined as "SUCCESS" then the complier can store two instances of "SUCCESS" or just one - as long as storing just one does not interfere with the operation of the program.

    For example:
    Code:
    	LPCSTR s1 = "SUCCESS";
    	LPCSTR s2 = "SUCCESS";
    
    	CString message;
    	
    	message.Format("SUCCESS=%p, s1=%p (%s), s2=%p (%s) SUCCESS=%p\n","SUCCESS",s1,s1,s2,s2,"SUCCESS","SUCCESS");
    	
    	::OutputDebugString(message);
    The output of this code is:
    SUCCESS=00420520, s1=00420520 (SUCCESS), s2=00420520 (SUCCESS) SUCCESS=00420520

    The example clearly shows that only one static instance of the string literal "SUCCESS" is stored and so comparisons such as you made are likely to work even though they are not proper.

    By the way, I compiled your code and it does work for me as you were expecting - it DOES NOT display "not equal too".

  6. #6
    Join Date
    Jun 2002
    Location
    Cambridge, UK
    Posts
    28

    Re: Very Strange behaviour with LPCTSTR

    Basically as xC0000005 stated, your code is actually comparing a pointer to the literal "SUCCESS" to your pointer. Nothing prevents the compiler from having placed the 2 in separate modules and then failing the pointer-comparison. Generally you are safe, but it's non-portable code you are writting, if someone move your constants to another DLL, or to another namespace, you will fail the compare.

    Been badly bitten in a similar way when defining a pointer in one module and then declaring it elsewhere slightly differently, another common error.
    extern char * mystring[];
    ... other module
    char *mystring="hello world";
    ...they are not the same 'mystring' in the end.

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Very Strange behaviour with LPCTSTR

    Quote Originally Posted by SMSdude
    I'm just confused as to why it worked before...
    If the strConnect was a CString, your code would've worked as intended.

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