CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2012
    Posts
    2

    Question vc6 to 2008 null character

    Hello,

    I am migrating a bit of code from vc6 to 2008 and have came across a bit of code that I have put an example of below. My question is, in the case of value1 becoming 0 from the comboBox, why would this have not returned NULL in vc6?

    Code:
    CString exampleString;
    unsigned long value1;
    char* value2 = (char*) &value1;
    char* buf;
    
    value1 = exampleCombo.GetSelData();
    buf = exampleString.GetBufferSetLength(1);
    buffer[0] = (value2[0]);
    exampleString.ReleaseBuffer(1);
    return exampleString;

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: vc6 to 2008 null character

    Quote Originally Posted by slihm View Post
    Hello,

    I am migrating a bit of code from vc6 to 2008 and have came across a bit of code that I have put an example of below. My question is, in the case of value1 becoming 0 from the comboBox, why would this have not returned NULL in vc6?
    Why would it?
    It returns a string with one character (zero) in it, regardless of he VC version.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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

    Re: vc6 to 2008 null character

    Quote Originally Posted by slihm View Post
    Code:
    CString exampleString;
    unsigned long value1;
    char* value2 = (char*) &value1;
    char* buf;
    
    value1 = exampleCombo.GetSelData();
    buf = exampleString.GetBufferSetLength(1);
    buffer[0] = (value2[0]);
    exampleString.ReleaseBuffer(1);
    return exampleString;
    What does this casting mean?
    Code:
    unsigned long value1;
    char* value2 = (char*) &value1;
    char* buf;
    And what for?

    What is buffer?

    What is GetSelData()?
    Victor Nijegorodov

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vc6 to 2008 null character

    Quote Originally Posted by slihm View Post
    Hello,

    I am migrating a bit of code from vc6 to 2008 and have came across a bit of code that I have put an example of below.
    Explain what that code is supposed to accomplish. Whatever it is, I can bet there are a lot more safer ways to do this than casting all over the place like that.

    If you removed all of those casts, does the compiler give you errors? If so, then what you're doing is dangerous, and possibly you were only lucky this "works" in Visual C++ 6.0. Usage of C-style casts to "shut the compiler up" usually means that whatever you're doing, don't do it.

    Also, a CString in later versions of the Visual C++ compiler (later than 6.0) compiler is based on TCHAR, not char. Therefore if the character set you chose when creating the project is Unicode, this code will either not compile, or it will not work properly.

    Get rid of the casts, observe the errors (if any) that the compiler gives you, and rethink what is being done. Again, please state on a high level what that code is supposed to do.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2012
    Posts
    2

    Re: vc6 to 2008 null character

    Thank you for your responses. I am not the original author so I am trying to understand the logic myself. There are no compile issues. From my debugging I have found the purpose of the casting and buffer to be to create a symbol character from the returned value of GetSelData (a combo box that returns 0 if the first item is selected, 1 if the second is selected etc.) and return the symbol character back as a CString to the calling function. For example, if 1 is returned from GetSelData the symbol returned is a space (" "), if 5 is returned from GetSelData the symbol becomes a U ("U") that is returned in the CString. However, the calling function does not behave properly when NULL is returned which is returned when GetSelData is 0 because it expects a symbol character of some kind. What could this bit of code have returned other than NULL when the combo box was 0?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: vc6 to 2008 null character

    The code you've shown doesn't make any sense at all. VictorN asked the obvious questions, so there really isn't much anybody can do with what you've given.

    Also, your function returns a CString. It can't return NULL.

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