ussing if or Switch with LPCWSTR
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

Thread: ussing if or Switch with LPCWSTR

  1. #1
    Join Date
    Oct 2013
    Posts
    10

    Question ussing if or Switch with LPCWSTR

    Hello,

    i am trying to read an ini file and usse an if or switch to take action.

    i have this in ini.h file

    LPCWSTR ini_read_string(LPCWSTR _ini_File_Name, LPCWSTR _ini_Section_Name, LPCWSTR _ini_Key_Name, LPCWSTR _ini_Default )
    {
    TCHAR _Rvalue[5];
    GetPrivateProfileStringW(_ini_Section_Name,_ini_Key_Name,_ini_Default,_Rvalue,5,_ini_File_Name);
    return (LPCWSTR) _Rvalue;
    }


    now when i do this:


    OverLay[0]=ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("NULL"));
    if(OverLay[0]==_T("NULL"))MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);


    but the key dos not exists, so it will return NULL, but yet i am not getting the message box (LPCWSTR)
    i also dit try


    if(OverLay[0]=="NULL"), if(OverLay[0]==L"NULL"),if(OverLay[0]== (LPCWSTR)"NULL"), if(OverLay[0]== (LPCWSTR)L"NULL"), if(OverLay[0]== (LPCWSTR)_T("NULL"))


    but it never dit work.
    to be sure of the returning value i also dit this


    MessageBox(NULL,ini_read_string(map_file_ini,_T("Section"),_T("kay"),_T("NULL")),(LPCWSTR)L"Error!",MB_ICONEXCLAMATION|MB_OK);


    and ther it is, an message box with the text "NULL"
    so why is the if(OverLay[0]==xxx) not working?
    and i also need a big list so an working switch(OverLay[0]) will be nice as well (else i need to do if/ else if many times)


    Thanks for reading

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

    Re: ussing if or Switch with LPCWSTR

    Please use code tags, indent properly, turn off the goofy colors and write each statement on a separate line and use proper indentation.

    It would help if you showed the definition of Overlay.

  3. #3
    Join Date
    Oct 2013
    Posts
    10

    Re: ussing if or Switch with LPCWSTR

    Hope this is more easy to read.

    i am trying to read an ini file and usse an if or switch to take action.
    i have this in ini.h file
    Code:
    LPCWSTR ini_read_string(LPCWSTR _ini_File_Name, LPCWSTR _ini_Section_Name, LPCWSTR _ini_Key_Name, LPCWSTR _ini_Default )
    {
    TCHAR _Rvalue[5];
    GetPrivateProfileStringW(_ini_Section_Name,_ini_Key_Name,_ini_Default,_Rvalue,5,_ini_File_Name);
    return (LPCWSTR) _Rvalue;
    }
    when i usse the folowing code

    Code:
    LPCWSTR OverLay[40]  /array
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("NULL"));
    if ( OverLay[0] == _T( "NULL" ))  MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);
    but the key dos not exists, so it will return NULL, but yet i am not getting the message box (LPCWSTR)
    i also dit try this:
    Code:
    if(OverLay[0]==             "NULL")
    if(OverLay[0]==            L"NULL")
    if(OverLay[0]== (LPCWSTR)   "NULL" )
    if(OverLay[0]== (LPCWSTR)  L"NULL" )
    if(OverLay[0]== (LPCWSTR)_T("NULL"))
    but not 1 seems to work
    to test the returning value of ini_read_string(); i have done this:
    Code:
    MessageBox(NULL,
                            ini_read_string(map_file_ini,_T("Section"),_T("kay"),_T("NULL"))
                            ,(LPCWSTR)L"Error!",MB_ICONEXCLAMATION|MB_OK);
    and ther it is an Error message that say NULL

    so i wonder, why isent the "if ( OverLay[0]== xxx )" working
    i also need a switch(OverLay[0])

    has anybodey an idea of wat i am missing, or wat i do so wrong?

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

    Re: ussing if or Switch with LPCWSTR

    I should have seen it before. You should be comparing to NULL, not "NULL". With the quotes, it's a string literal.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: ussing if or Switch with LPCWSTR

    In your ini_read_string, you are returning a pointer to a memory location that is invalid once the function returns as _Rvalue is only valid during the function.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Oct 2013
    Posts
    10

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by GCDEF View Post
    I should have seen it before. You should be comparing to NULL, not "NULL". With the quotes, it's a string literal.
    i know that part but when you read an key from an ini file it returns an default value if the key not exists, i ussed _T("NULL") to return the string "NULL" if the kay not exists

    ini_read_string(LPCWSTR _ini_File_Name, LPCWSTR _ini_Section_Name, LPCWSTR _ini_Key_Name, LPCWSTR _ini_Default )
    but i also ussed something else like "TEST" or reading an existing kay

    ussing this i always get the default value, or true value if file / key exists
    Code:
    MessageBox(NULL,
                  ini_read_string(map_file_ini,_T("Section"),_T("kay"),_T("TEST"))
                 ,(LPCWSTR)L"Error!",MB_ICONEXCLAMATION|MB_OK);


    but this code never works
    Code:
    LPCWSTR OverLay[40]  /array
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("TEST"));
    if ( OverLay[0] == _T( "TEST" ))  MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);

    In your ini_read_string, you are returning a pointer to a memory location that is invalid once the function returns as _Rvalue is only valid during the function.
    if not mistaking its now savet to the variable LPCWSTR OverLay[40];

    Code:
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("NULL"));
    
    
    if ( OverLay[0] == _T( "NULL" ))  MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);

  7. #7
    Join Date
    Apr 1999
    Posts
    27,446

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by MR.Hotdog View Post
    but this code never works
    Code:
    LPCWSTR OverLay[40]  /array
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("TEST"));
    That code "never works" because you were told already that ini_read_string returns garbage.
    Code:
    LPCWSTR ini_read_string(LPCWSTR _ini_File_Name, LPCWSTR _ini_Section_Name, LPCWSTR _ini_Key_Name, LPCWSTR _ini_Default )
    {
       TCHAR _Rvalue[5];
       //...
       return (LPCWSTR) _Rvalue;
    }
    It is undefined behaviour to return a pointer or reference to a local variable. That is basic C++ rules, but you violated them.

    Regards,

    Paul McKenzie

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

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by MR.Hotdog View Post
    i know that part but when you read an key from an ini file it returns an default value if the key not exists, i ussed _T("NULL") to return the string "NULL" if the kay not exists

    ini_read_string(LPCWSTR _ini_File_Name, LPCWSTR _ini_Section_Name, LPCWSTR _ini_Key_Name, LPCWSTR _ini_Default )
    but i also ussed something else like "TEST" or reading an existing kay

    ussing this i always get the default value, or true value if file / key exists
    Code:
    MessageBox(NULL,
                  ini_read_string(map_file_ini,_T("Section"),_T("kay"),_T("TEST"))
                 ,(LPCWSTR)L"Error!",MB_ICONEXCLAMATION|MB_OK);


    but this code never works
    Code:
    LPCWSTR OverLay[40]  /array
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("TEST"));
    if ( OverLay[0] == _T( "TEST" ))  MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);


    if not mistaking its now savet to the variable LPCWSTR OverLay[40];

    Code:
    OverLay[0] = ini_read_string(map_file_ini,_T("Section"),_T("Key"),_T("NULL"));
    
    
    if ( OverLay[0] == _T( "NULL" ))  MessageBox(NULL,_T("File or kay not exists"),_T("Error"),MB_ICONEXCLAMATION|MB_OK);
    Your code is a little odd, but if that's the case you're comparing Overlay[0], which is a pointer, to "NULL" which is also a pointer. They can never be equal. Use strcmp to compare strings.

  9. #9
    Join Date
    Oct 2013
    Posts
    10

    Re: ussing if or Switch with LPCWSTR

    Thanks for helping so far, i still havent find a sulution, but yet i have learnd a loth from you gay's

    i dit learn C a while ago for C Processor projects, but i never need oter variables then int in those C projects.
    so i am new in both windows as strings,

    pahaps somebody may be able te help me out if i drop the whole code and say:
    if to usse if function on the returning value of GetPrivateProfileStringW


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include<Windows.h>
    	wchar_t ini_index[5];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
    	GetPrivateProfileStringW(_T("Test"),_T("Key"),_T("NE"),ini_index,5,_T("c:\\test.ini"));
    
          if(return value == "NE") std::cout << "File\Key not Exists";
    else if(return value == "ON") std::cout << "I left my tv on";
    else if(return value == "OFF")std::cout<< "TV is boring"
    else if(return value == "qefqd")std::cout<<"My cat is walking over my keyboard";
    system("pause");	
    return 0;
    	
    }
    an switch can be ussefull to, if it can be ussd with strings

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

    Re: ussing if or Switch with LPCWSTR

    What's return value?

    Read and understand what I said in my previous post. You can't test null terminated strings for equality using ==. You need to use some kind of string class or use one of the sting comparison functions such as strcmp. Also, qefqd won't fit in a 5 byte char array. You need to leave room for the null terminator.

    And no, switch doesn't work with strings. MSDN says "The expression must be of an integral type or of a class type for which there is an unambiguous conversion to integral type"

  11. #11
    Join Date
    Oct 2013
    Posts
    10

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by GCDEF View Post
    What's return value?
    somehow a bit of text wend missing (removed by mistake)

    i dit usse the:
    if(std::strcmp((char*)ini_index,"NE")) but dident work so thats why i post my last post.
    but thanks for showing me the strcmp,

    i found an nice website witch generate vonvertion scripts so now it works
    http://www.convertdatatypes.com/Conv...CPlusPlus.html

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include<Windows.h>
    	wchar_t ini_index[6];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
    	GetPrivateProfileStringW(_T("Test"),_T("Key"),_T("NE"),ini_index,5,_T("c:\\test.ini"));
    
    	wchar_t* vIn = ini_index;
    char * vOut = new char[wcslen(vIn)+1];
    wcstombs_s(NULL,vOut,wcslen(vIn)+1,vIn,wcslen(vIn)+1);
    
         if(std::strcmp(vOut,"NE")==0) std::cout << "File\Key not Exists";
    else if(std::strcmp(vOut,"ON")==0) std::cout << "I left my tv on";
    else if(std::strcmp(vOut,"OFF")==0)std::cout<< "TV is boring"
    else if(std::strcmp(vOut,"qefqd")==0)std::cout<<"My cat is walking over my keyboard";
    system("pause");	
    return 0;
    somehow std::strcmp(vOut,"NE") is true when vOut is not "NE" but ==0 was an quikfix to get it working

    but thanks a loth, i dit learn a loth from all of you





    In your ini_read_string, you are returning a pointer to a memory location that is invalid once the function returns as _Rvalue is only valid during the function.
    That code "never works" because you were told already that ini_read_string returns garbage.
    Also, qefqd won't fit in a 5 byte char array. You need to leave room for the null terminator.
    ou're comparing Overlay[0], which is a pointer, to "NULL" which is also a pointer. They can never be equal. Use strcmp to compare strings.

  12. #12
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,483

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by MR.Hotdog View Post
    Thanks for helping so far, i still havent find a sulution, but yet i have learnd a loth from you gay's

    i dit learn C a while ago for C Processor projects, but i never need oter variables then int in those C projects.
    so i am new in both windows as strings,

    pahaps somebody may be able te help me out if i drop the whole code and say:
    if to usse if function on the returning value of GetPrivateProfileStringW


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include<Windows.h>
    	wchar_t ini_index[5];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
    	GetPrivateProfileStringW(_T("Test"),_T("Key"),_T("NE"),ini_index,5,_T("c:\\test.ini"));
    
          if(return value == "NE") std::cout << "File\Key not Exists";
    else if(return value == "ON") std::cout << "I left my tv on";
    else if(return value == "OFF")std::cout<< "TV is boring"
    else if(return value == "qefqd")std::cout<<"My cat is walking over my keyboard";
    system("pause");	
    return 0;
    	
    }
    an switch can be ussefull to, if it can be ussd with strings
    In C++, a switch can't be used with strings - it's just the way it is. In other languages like Java and C#, a switch can be used with strings, but it doesn't mean you would want to do it (as comparing strings can be more expensive than comparing integers).

    To be honest, your code is a mess (which isn't surprising because you are new to this). A big problem that I see with your code is you are mixing UNICODE (LPWSTR), ANSI ("NE") and _T("Key") all over the place. Some of this code might work, depending on your compiler settings, but you need to understand what is going on otherwise it will bite you later.

    As GCDEF mentioned you can't compare strings in C++ by comparing the pointers of the strings. The code below is comparing pointers;
    Code:
    if(return value == "NE")
    This won't ever compare, because you are trying to compare the memory location of the variable value, with the memory location of the "NE" string literal and these won't be the same.

    This might be confusing if you are coming from other languages like Java or C# because the strings in these languages are actually classes and the classes provide an overloaded == which compare the actual strings contained within the string classes.

    In C++, you need to use actual string functions to do a string comparison. For ANSI strings you can use something like strcmp, for Unicode strings, wcscmp. For tchar, use _tcscmp. When you use std::string or std::wstring it can be problematic on Windows depending on which you using and what your character set compiler settings are.

    A good place to start understanding this is http://www.codeproject.com/Articles/...TR-LPCTSTR-etc

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

    Re: ussing if or Switch with LPCWSTR

    Quote Originally Posted by MR.Hotdog View Post
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include<Windows.h>
    	wchar_t ini_index[6];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
    	GetPrivateProfileStringW(_T("Test"),_T("Key"),_T("NE"),ini_index,5,_T("c:\\test.ini"));
    
    	wchar_t* vIn = ini_index;
    char * vOut = new char[wcslen(vIn)+1];
    wcstombs_s(NULL,vOut,wcslen(vIn)+1,vIn,wcslen(vIn)+1);
    
         if(std::strcmp(vOut,"NE")==0) std::cout << "File\Key not Exists";
    else if(std::strcmp(vOut,"ON")==0) std::cout << "I left my tv on";
    else if(std::strcmp(vOut,"OFF")==0)std::cout<< "TV is boring"
    else if(std::strcmp(vOut,"qefqd")==0)std::cout<<"My cat is walking over my keyboard";
    system("pause");	
    return 0;
    Compare that with this

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include<Windows.h>
    
    	wchar_t ini_index[6];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
          std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
          GetPrivateProfileString("Test", "Key", "NE", ini_index, 5, "c:\\test.ini");
    
          wchar_t* vIn = ini_index;
          char * vOut = new char[wcslen(vIn)+1];
          wcstombs_s(NULL, vOut, wcslen(vIn) + 1, vIn, wcslen(vIn) +1 );
    
          if(std::strcmp(vOut, "NE")==0) 
                 std::cout << "File\Key not Exists";
          else
          if(std::strcmp(vOut, "ON") == 0) 
                 std::cout << "I left my tv on";
          else 
          if(std::strcmp(vOut, "OFF") == 0)
                 std::cout<< "TV is boring"
          else 
          if(std::strcmp(vOut,"qefqd") == 0)
                 std::cout<<"My cat is walking over my keyboard";
          system("pause");	
          return 0;
    Start using whit space and proper indentation so your code is readable.

    Next, you need to learn what your code is doing and that's what the debugger does. What is GetPrivateProfileString returning?

  14. #14
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,483

    Re: ussing if or Switch with LPCWSTR

    You can also simplify the code by using the CString class (just #include <atlstr.h>).

    Code:
    #include <stdio.h>
    #include <tchar.h>
    #include <atlbase.h>
    #include <atlstr.h>
    #include <iostream>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::cout << "INI FILE READING TEST PROGRAM" << std::endl << std::endl;
    
      CString sKeyValue;
      DWORD dwKeyValueMax = 5;        
      DWORD dwError = 0;
    
      GetPrivateProfileString(
          _T("Test"),
          _T("Key"),
          _T("NE"),
          sKeyValue.GetBuffer(dwKeyValueMax),
          dwKeyValueMax,
          _T("C:\\test.ini")
        );
      sKeyValue.ReleaseBuffer();
    
      // Check ::GetLastError here for != 0 to determine if GPPS failed (normally Win Api's return non-zero
      // values to let you know when to call GetLastError, but GetPrivateProfileString doesn't work this way)
    
      if(sKeyValue == _T("NE"))
      {
        std::cout << "File\Key not Exists" << std::endl;
      }
      else if (sKeyValue == _T("ON")) 
      {
        std::cout << "I left my tv on" << std::endl;
      }
      else if(sKeyValue == _T("OFF"))
      {
        std::cout << "TV is boring" << std::endl;
      }
      else if(sKeyValue == _T("qefqd"))
      {
        std::cout << "My cat is walking over my keyboard" << std::endl;
      }
      else
      {
        std::cout << "Unrecognized value" << std::endl;
      }
    
      std::cout << std::endl;
      system("pause");	
    
      return 0;
    }

  15. #15
    Join Date
    Oct 2013
    Posts
    10

    Re: ussing if or Switch with LPCWSTR

    thanks a loth Arjay even with my limited englich i understend efrything you say.
    tomorow i will go to the website to read that article



    GCDEF also thanks a loth for all your posts.

    i most say it looks clean how you changet my code.
    always eazy to find things back to edit/fix it
    rite now GetPrivateProfileString is returning "ON" becorse i have made the ini file

    Code:
    [test]
    key=ON
    it returnts it value in-to wchar_t ini_index[6]; then convert it to char* vOut;
    checking it with if(std::strcmp(vOut, "ON") == 0) printing I left my tv on to the screen
    also ussd the other's "OFF" and "qefqd" always priting the right text to the screen

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center