-2

the below function loads 2 strings from a file:

int loadsettings()
{
    wstring a,b;
    int retrn = 1;
    wfstream myFile;
    myFile.open ("settings.txt");
    if ((!myFile.is_open())||(myFile.fail()))
    MessageBox(NULL, "Error openning settings file!", "Error", MB_ICONINFORMATION);
    else
    {
        if(myFile.fail())
        {
            myFile.close();
            myFile.clear();
            retrn = 0;
            savedefault();  //creates a settings.txt with default values
            myFile.open ("settings.txt", ios::in);
        }


            myFile >> b; //b becomes "user"
            myFile >> a; // a becomes "password"

        user = (LPARAM)b.c_str();
        password = (LPARAM)a.c_str();

        SendMessage(hEdit,
                WM_SETTEXT,
                NULL,
                user);  // sets the text box to "u"

            SendMessage(hEdit2,
                WM_SETTEXT,
                NULL,
                password); //sets the text box to "p"

        myFile.close();
    }
    return retrn;
}

I want to gave the two strings taken from the file go to the textboxs hEdit and hEdit2. Try to do that with the Sendmessage settext. But only the first character in the string gets there. what should I do?

user1397417
  • 708
  • 4
  • 11
  • 34
  • What do you wanna use the resulting LPARAM for? – user1233963 Sep 09 '13 at 07:48
  • 4
    You seem to be confused what `LPARAM` is. It is a pointer type and the values look good - they are addresses to the buffers stored by your `std::string` objects. However, those are local objects and will go out of scope when `loadsettings` returns. You need to understand some basic C++ concepts here, object lifetime among others. Consider reading a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – IInspectable Sep 09 '13 at 07:54
  • I guess you want to pass you're strings to some WinAPI function? If so, don't worry about the resulting interpretation (which is a pointer only anways). If you want to cast back to a LPCSTR or similar, just reverse the process, i.e. (LPCSTR) myLparamString – bcause Sep 09 '13 at 07:54
  • if the values of it are good, why do I get "溢・" and "zaguタ" in the textbox that the LPARAMs go to? see the garble there lead me to think the conversion went wrong. but perhaps something else? – user1397417 Sep 09 '13 at 07:59
  • @user1397417 The value of LPARAM should not be relevant to you, but to where it's being passed to, since it's just a pointer. To which function are you passing LPARAM user, and LPARAM password? – bcause Sep 09 '13 at 08:07
  • I added the code where user and password are used – user1397417 Sep 09 '13 at 08:16
  • "I figured the (LPARAM)b.c_str(); would convert it" - convert it *to what??* Thats not a conversion; its a *cast*. And you're not using it for anything before the function (and the aforementioned variables within) finish execution scope and evaporate for defined behavior., So start over: *What are you trying to do??*" – WhozCraig Sep 09 '13 at 08:27
  • I figured it was obvious. i want to send the result from the input file to the SendMessage code at the bottom, and have it print the same thing i find in the file when i open it. – user1397417 Sep 09 '13 at 08:47
  • Please post real code that shows how you're actually passing the strings to `SendMessage` (showing two separate pieces of code doesn't help because we don't know what's in scope where). – interjay Sep 09 '13 at 09:17
  • 4
    You are getting funny characters because you pass ANSI strings to a window that expects UNICODE strings. See [About Unicode and Character Sets](http://msdn.microsoft.com/en-us/library/windows/desktop/dd317711.aspx) and [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://www.joelonsoftware.com/articles/Unicode.html). – IInspectable Sep 09 '13 at 09:52
  • after i moved the SendMessage()s inside my function as in the edit, it shows real characters, at least one of them. looks like the first character of the string gets there. so i see a "u" in the username column(hEdit) and a "p" in the password column(hEdit2). – user1397417 Sep 09 '13 at 10:22
  • 2
    Now you are sending UTF-16 encoded character strings to a window that expects ANSI strings. You really have to get the basics straight about character encodings. Read the links I provided above. None of this will make sense if you're lacking the basics. – IInspectable Sep 09 '13 at 10:55
  • where are you getting this information to tell me im sending UTF-16 to ANSI or ANSI to UNICODE as your saying? – user1397417 Sep 09 '13 at 11:23
  • You are using `std::wstring` - there's your UTF-16 encoded string. And then you are calling `MessageBox` with an `"ANSI"` string literal (vs. a `L"UNICODE"` string literal). I'll call this a smoking gun and you may call me Sherlock. Again, read the links I provided above. They will explain all of this. – IInspectable Sep 09 '13 at 12:00
  • I read quite a bit from those links. i did not see anything telling me that a wstring is UTF-16 encoding. Or anything about what encoding sendmessage uses. where in my sendmessage declares an ANSI string literal? – user1397417 Sep 09 '13 at 12:19
  • UNICODE strings on Windows are UTF-16 encoded. Your call to `RegisterClass[Ex]` determines whether your windows of that class will expect messages in ANSI or UNICODE format. Use the UNICODE variant (with a trailing `W`) or set the respective `UNICODE` preprocessor symbol if you want UNICODE windows. You can always call [`IsWindowUnicode`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633529.aspx) to find out. In case I haven't said so: Books are a **really** good resource to learn about complex issues, like programming. – IInspectable Sep 09 '13 at 12:26
  • Basically you should be defining UNICODE and _UNICODE_ when compiling and using the UNICODE/Wide APIs exclusively. Nobody should be using the ansi api unless they are targeting Windows 98, and if they are, they probably should be using UNICOWS instead of using the ANSI API. – Ben Sep 09 '13 at 13:41

1 Answers1

0

When you have only one letter it's almost every time because you send a Unicode string to an ANSI method.
The other way show you some gibberish results

  • If you want to compile with ANSI support

    uses string and not wstring

  • If you want Unicode support

    Add the unicode support do the 2 defined needed at the right place (topmost headers or project options)

    #define UNICODE // for windows api unicode
    #define _UNICODE // for libc unicode tchar and _TEXT macro...
    

    After that you should add a L"string" or _TEXT("string") or even _T("string") to your static strings in your code.

ColdCat
  • 1,192
  • 17
  • 29