Re: Porting up to VC++ 2005



See below...
On Sun, 27 Apr 2008 13:13:11 GMT, "Kahlua" <kahlua@xxxxxxxxxx> wrote:

Hi,
Thanks to everyone for you past help with VC++6

I have been using the code below with VC++6 with no errors and seems to work
fine.
When I try to do the same in VC++ 2005 I get errors:

Line: nSelect=SendDlgItemMessage(IDC_LIST1, LB_GETCURSEL, 0, 0L);
warning C4244: '=' : conversion from 'LRESULT' to 'int', possible loss of
data
***
Yes, this is a correct warning. VS2005 assumes that the return type can be 64 bits wide,
in accordance with the specified behavior of SendDlgItemMessage.

However, it is odd to see such code today; the correct code would be

nSelect = c_List1.GetCurSel();

by creating a control variable for the control. SendDlgItemMessage is really clunky, and
should be avoided.

and you should use more mnemonic names than the silly "IDC_controlN" names created for you
by the dialog editor.
****

Line: DlgDirSelect((LPSTR) cSelect, IDC_LIST1);
error C2664: 'BOOL CWnd::DlgDirSelect(LPTSTR,int)' : cannot convert
parameter 1 from 'LPSTR' to 'LPTSTR'
****
You are now compiling in Unicode mode. Note that you put an EXPLICIT (LPSTR) cast of
cSelect, and that has always been poor style, because the variable (whose declaration you
do not show, even though this error clearly relates directly to it) should always have
been a form that was Unicode-aware, specifically, LPCTSTR. If there is any casting to be
done, which should be unnecessary, it should be an LPTSTR cast, which conforms to the
speficied requirements of CWnd::DlgDirSelect
****

What do I need to chane for it to work under VC++ 2005 ?
Or, how do I code this properly?
****
Yes, you have to change this
****

void CMyDlg::OnLbnSelchangeList1()
{
CString JobFile;
TCHAR cSelect[50];
****
50 is, and always has been, an erroneous value. The very least is you must write
TCHAR cSelect[MAX_PATH];
which will be safe. Otherwise, you have potential for a buffer overrun if the user gives
you a path of more than 50 characters.
****
int Length;
****
You could change this to INT_PTR
****
int nSelect;

nSelect=SendDlgItemMessage(IDC_LIST1, LB_GETCURSEL, 0, 0L);
****
nSelect = c_List1.GetCurSel();
or, if you want to keep this really clunky syntax, it will now compile correctly
****
DlgDirSelect((LPSTR) cSelect, IDC_LIST1);
****
Given cSelect is already an LPTSTR, no cast is required. Delete the cast. It was wrong
in the older code, and deleting it will still be correct with older compilers
****
Length=strlen(cSelect); //get length of filename
string
****
Length = _tcslen(cSelect);
****
if (cSelect[Length-1]==0x2e) //if last char is "."
remove it
if(cSelect[Length - 1] == _T('.'))
there is no reason to convert a character constant to hex to compare it.
****
cSelect[Length-1]=0;
JobFile = _T("C:\\Temp\\"); //re-apply main part of
path to files
****
There is no reason to ever assume that c:\temp exists. You should try
JobFile = ::GetEnvironmentVariable(_T("TEMP"));
if(JobFile.IsEmpty())
JobFile = ::GetEnvironmentVariable(_T("TMP"));
and if it is still empty, you might choose a default value, but even if you set that
default to be c:\temp, there is no guarantee this directory exists! (It might on your
machine, but not on mine, so running this program on any machine but yours is risky). Note
that the location of the temp directory is generally "owned" by the end user, and I have
often made it reference some large disk (when my C:\ partition was getting crowded)
****
JobFile += _T(cSelect); //add filename selected
****
_T is erroneous here and must be removed. It only applies to string and character
literals, not variables!
****
JobFile += _T(".dat"); //re-apply file extension


CString mess;
mess.Format(_T("Would you like to load \"%s\" as top ?"), cSelect);
int a = AfxMessageBox(mess, MB_ICONQUESTION | MB_YESNO);
if(a != IDYES)
return;

AfxMessageBox(cSelect); //display selected filename
as test
}

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Porting up to VC++ 2005
    ... parameter 1 from 'LPSTR' to 'LPTSTR' ... You are now compiling in Unicode mode. ... done, which should be unnecessary, it should be an LPTSTR cast, which ... there is no reason to convert a character constant to hex to compare it. ...
    (microsoft.public.vc.mfc)
  • Re: Development news catch-up
    ... to topline a new original movie for the cable channel, ... Does Ponce have the chops to be the lead/recurring character? ... 10-to-1 says this means a new cast. ... "Summerland") is the latest addition to the comedy pilot, ...
    (rec.arts.tv)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... He's set to be the driver of the first Brubaker written Uncanny X-Men ...
    (rec.arts.comics.marvel.xbooks)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... He's set to be the driver of the first Brubaker written Uncanny X-Men ...
    (rec.arts.comics.marvel.xbooks)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... he's going to be a member of the X-Men. ...
    (rec.arts.comics.marvel.xbooks)

Loading