Originally Posted by
codenewbie
Hi I'm hoping someone can help me with this. I've been searching google for several hours and I can't find a satisfactory answer to make my code work.
I'm trying to create an edit box within a dialog window that will recieve a double input from a user and then display that double value in a different edit box when a button is pressed. I've seen the use of the GetDlgItemText() function to get the information as a string and then using the atof() function to convert it to double, but when I try this I get 0 every time. For some reason, visual c++ 2008 makes me typecast my char arrays to LPWSTR in order for it to compile properly. (Not sure why or if I'm just not setting them up correctly to begin with). Can someone suggest a different way to do this or tell me what is wrong with my code so I can fix it?
Here is my code:
//global variables
double AccountBalance;
char temp[MAX_PATH];
//this code is within my button case in my switch statement.char buffer[MAX_PATH];
GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,(L PWSTR)buffer,MAX_PATH);
AccountBalance = atof(buffer);
AccountBalance = AccountBalance*2;
char temp2[100];
wsprintf((LPWSTR)temp2,L"%d",AccountBalance);
SetDlgItemText(AccountDialog,IDC_EDIT1,(LPWSTR)tem p2);
The reason it's forcing you to typecast is that someplace you've got #define UNICODE in your source. Hence all windows api calls need wide strings to work...
Either
A) remove the UNICODE define
B) change all your strings over to WCHAR .... and use the unicode vesions of all string functions in wchar.h
c) Explicitly call the Ansi version of Windows functions... as in GetDlgItemTextA() etc.
Unicode programming is a whole different animal than you are likely to be used to with console programming.
Code:
//global variables
#include <wchar.h>
#define MAX_EDIT 64
double AccountBalance;
char temp[MAX_EDIT];
//this code is within my button case in my switch statement.
WCHAR buffer[MAX_EDIT];
GetDlgItemText(AccountDialog,IDC_ACCOUNTBALANCE,buffer,MAX_EDIT);
AccountBalance = wcstod(buffer);
AccountBalance = AccountBalance*2;
WCHAR temp2[100];
wsprintf(temp2,L"%d",AccountBalance);
SetDlgItemText(AccountDialog,IDC_EDIT1,temp2);
As matters of general practice to reduce errors it's not a good idea to use any more global variables than you absolutely have to. It's also a good idea to put as little code as possible in switch statements of message handlers; you should instead create functions for things like the above and call them from the message handler. Also... when working in Windows API, you should try to use windows types (variables, structs, etc) whenever possible.