From: Kahlua on
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

Line: DlgDirSelect((LPSTR) cSelect, IDC_LIST1);
error C2664: 'BOOL CWnd::DlgDirSelect(LPTSTR,int)' : cannot convert
parameter 1 from 'LPSTR' to 'LPTSTR'

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

void CMyDlg::OnLbnSelchangeList1()
{
CString JobFile;
TCHAR cSelect[50];
int Length;
int nSelect;

nSelect=SendDlgItemMessage(IDC_LIST1, LB_GETCURSEL, 0, 0L);
DlgDirSelect((LPSTR) cSelect, IDC_LIST1);
Length=strlen(cSelect); //get length of filename
string
if (cSelect[Length-1]==0x2e) //if last char is "."
remove it
cSelect[Length-1]=0;
JobFile = _T("C:\\Temp\\"); //re-apply main part of
path to files
JobFile += _T(cSelect); //add filename selected
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
}


From: Joseph M. Newcomer on
See below...
On Sun, 27 Apr 2008 13:13:11 GMT, "Kahlua" <kahlua(a)right.here> 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(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Kahlua on
Thanks for the really helpfull information from both Joe and Scott.
I am sure all the data I am dealing with is 8-bits in width, mostly text but
sometimes unsigned 8-bit hex.
Does all the info given still apply?
Thanks


"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:081914560vtneiru0fahi6prgj7n6fq1pk(a)4ax.com...
> See below...
> On Sun, 27 Apr 2008 13:13:11 GMT, "Kahlua" <kahlua(a)right.here> 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(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm


From: Joseph M. Newcomer on
Since you are dealing with strings that contain file names, it is text. But whether or
not it is "8-bit" text should be irrelevant; it is text. If it is a CString, it is text.
In other situations you might have unsigned 8-bit data which may or may not represent
characters, but that's a different question to be handled in a different way. For
example, CByteArray would be a better choice of representation than char, TCHAR, CString,
or CStringA. However, in the code you showed, there is no concept of
data-which-is-not-a-string, so you would have to post a different question to cover a
situation in which you were reading raw 8-bit binary data.
joe

On Sun, 27 Apr 2008 14:52:08 GMT, "Kahlua" <kahlua(a)right.here> wrote:

>Thanks for the really helpfull information from both Joe and Scott.
>I am sure all the data I am dealing with is 8-bits in width, mostly text but
>sometimes unsigned 8-bit hex.
>Does all the info given still apply?
>Thanks
>
>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>news:081914560vtneiru0fahi6prgj7n6fq1pk(a)4ax.com...
>> See below...
>> On Sun, 27 Apr 2008 13:13:11 GMT, "Kahlua" <kahlua(a)right.here> 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(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Kahlua on
OK, I created a control variable variable for my listbox called c_List1
I shrunk the code for test purposes to find/fix errors.
Now I get the following error:

warning C4996: 'CWnd::DlgDirSelect' was declared deprecated
See declaration of 'CWnd::DlgDirSelect'
Message: 'CWnd::DlgDirSelect(lpszOut, nControlId) is no longer supported.
Instead, use CWnd::DlgDirSelect(lpszOut, nSize, nControlId)'

void CMyDlg::OnLbnSelchangeList1()
{
TCHAR cSelect[MAX_PATH];
int nSelect;

nSelect = c_List1.GetCurSel();

DlgDirSelect(cSelect, IDC_LIST1);
}



"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:081914560vtneiru0fahi6prgj7n6fq1pk(a)4ax.com...
> See below...
> On Sun, 27 Apr 2008 13:13:11 GMT, "Kahlua" <kahlua(a)right.here> 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(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm