How to get Visual C++ (2015 version) to work in "char = 8 bits" mode?
-
I am trying to revive a program that I wrote in years 2001 to 2005 for my old Borland C++ 4.51 compiler. The library functions that its compiled programs called, always ran in the mode of a character = 8 bits, not 16 bits. As this program largely handles text, not numerical values, it will have to compile to run with char = 8 bytes everywhere. For example, in this section: int rundialogbox(HWND wn, HINSTANCE I, char*name, BOOL CALLBACK /*_export*/ proc(HWND db, UINT M, WPARAM S, LPARAM L)) { DLGPROC dp = MakeProcInstance((DLGPROC)proc, I); int i = DialogBox(I, name, wn, dp); FreeProcInstance(dp); return i;} I had type mismatch error of the type char* :: WCHAR*, until in menu Project / Properties I set the character mode from Unicode to Not Set; then this section compiled OK. Whereupon, the next section void diprintf(HWND db,int c,char*fmt,...){ char C[256]; vsprintf(C,fmt,(&fmt)+1); SetDlgItemText(db,c,C); }; failed with this error: 1>faces.cpp(80): error C2664: 'int vsprintf(char *const ,const char *const ,va_list)': cannot convert argument 3 from 'char **' to 'va_list' 1> faces.cpp(80): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I replaced `char C[256];` by `WCHAR C[256];`, and compilation error still happened. Does my Visual C++ still have an 8-bit-char-mode set of library functions? If so, how to call them?
-
I am trying to revive a program that I wrote in years 2001 to 2005 for my old Borland C++ 4.51 compiler. The library functions that its compiled programs called, always ran in the mode of a character = 8 bits, not 16 bits. As this program largely handles text, not numerical values, it will have to compile to run with char = 8 bytes everywhere. For example, in this section: int rundialogbox(HWND wn, HINSTANCE I, char*name, BOOL CALLBACK /*_export*/ proc(HWND db, UINT M, WPARAM S, LPARAM L)) { DLGPROC dp = MakeProcInstance((DLGPROC)proc, I); int i = DialogBox(I, name, wn, dp); FreeProcInstance(dp); return i;} I had type mismatch error of the type char* :: WCHAR*, until in menu Project / Properties I set the character mode from Unicode to Not Set; then this section compiled OK. Whereupon, the next section void diprintf(HWND db,int c,char*fmt,...){ char C[256]; vsprintf(C,fmt,(&fmt)+1); SetDlgItemText(db,c,C); }; failed with this error: 1>faces.cpp(80): error C2664: 'int vsprintf(char *const ,const char *const ,va_list)': cannot convert argument 3 from 'char **' to 'va_list' 1> faces.cpp(80): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I replaced `char C[256];` by `WCHAR C[256];`, and compilation error still happened. Does my Visual C++ still have an 8-bit-char-mode set of library functions? If so, how to call them?
Anthony Appleyard wrote:
Does my Visual C++ still have an 8-bit-char-mode set of library functions? If so, how to call them?
The "Not Set" should allow the project to use the ANSI functions. You've already made the correct change.
Anthony Appleyard wrote:
void diprintf(HWND db,int c,char*fmt,...)
{
char C[256];
vsprintf(C,fmt,(&fmt)+1);
SetDlgItemText(db,c,C);
};failed with this error: 1>faces.cpp(80): error C2664: 'int vsprintf(char *const ,const char *const ,va_list)': cannot convert argument 3 from 'char **' to 'va_list' 1> faces.cpp(80): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
The debugger error tells you exactly what the problem is. The third argument needs to be a va_list. Those old unsafe typecasts are no longer legal. You can probably fix it with something like:
void diprintf(HWND db,int c,char*fmt,...)
{
char C[256];
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsprintf(C,fmt,arg_ptr);
SetDlgItemText(db,c,C);
};The function looks unsafe. The function will overflow if the variable-argument list results in a string longer than 256 char. You should consider using the safer [vsprintf_s](https://msdn.microsoft.com/en-us/library/xa1a1a6z.aspx).
vsprintf_s(C,256,fmt,arg_ptr);
Best Wishes, -David Delaune
-
Anthony Appleyard wrote:
Does my Visual C++ still have an 8-bit-char-mode set of library functions? If so, how to call them?
The "Not Set" should allow the project to use the ANSI functions. You've already made the correct change.
Anthony Appleyard wrote:
void diprintf(HWND db,int c,char*fmt,...)
{
char C[256];
vsprintf(C,fmt,(&fmt)+1);
SetDlgItemText(db,c,C);
};failed with this error: 1>faces.cpp(80): error C2664: 'int vsprintf(char *const ,const char *const ,va_list)': cannot convert argument 3 from 'char **' to 'va_list' 1> faces.cpp(80): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
The debugger error tells you exactly what the problem is. The third argument needs to be a va_list. Those old unsafe typecasts are no longer legal. You can probably fix it with something like:
void diprintf(HWND db,int c,char*fmt,...)
{
char C[256];
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsprintf(C,fmt,arg_ptr);
SetDlgItemText(db,c,C);
};The function looks unsafe. The function will overflow if the variable-argument list results in a string longer than 256 char. You should consider using the safer [vsprintf_s](https://msdn.microsoft.com/en-us/library/xa1a1a6z.aspx).
vsprintf_s(C,256,fmt,arg_ptr);
Best Wishes, -David Delaune
Thanks. It compiled OK. What should this become? int discanf(HWND db, int c, char*fmt, ...) { char C[256]; GetDlgItemText(db, c, C, 256); C[255] = 0; return vsscanf(C, fmt, (&fmt) + 1); }
-
Thanks. It compiled OK. What should this become? int discanf(HWND db, int c, char*fmt, ...) { char C[256]; GetDlgItemText(db, c, C, 256); C[255] = 0; return vsscanf(C, fmt, (&fmt) + 1); }
Anthony Appleyard wrote:
What should this become?
Come on man, this is something that you can easily figure out. You've been a member here for over 10 years. You know the old Chinese proverb? If you give a man a fish he is hungry again in an hour. If you teach him how to catch a fish you feed him for his lifetime. You've already had your daily fish. I highly encourage you to figure it out yourself. If you get stuck feel free to come back for more fish. Best Wishes, -大衛王
-
Thanks. It compiled OK. What should this become? int discanf(HWND db, int c, char*fmt, ...) { char C[256]; GetDlgItemText(db, c, C, 256); C[255] = 0; return vsscanf(C, fmt, (&fmt) + 1); }
-
For a start having an
int
variable calledc
, and achar
array calledC
is bad enough in the same module. But in the same function it is beyond ridiculous. Use proper meaningful unique names for all your variables.Sorry. I started computer programming in the late 1960's, when computer storage was much smaller (80,000 words memory was big), under early compilers such as Atlas Autocode and Basic, and I got accustomed to one-letter and two-letter variable names. Thanks for your help.
-
Sorry. I started computer programming in the late 1960's, when computer storage was much smaller (80,000 words memory was big), under early compilers such as Atlas Autocode and Basic, and I got accustomed to one-letter and two-letter variable names. Thanks for your help.
Anthony Appleyard wrote:
I got accustomed to one-letter and two-letter variable names.
I confirmed that it compiles in Visual Studio 2017. I am calling it "C Notation" naming convention.
\#define C 256
int Ƈ(HWND č, int c, char* Ç, ...)
{
char ₵[C];
va_list Ĉ;
va_start(Ĉ, Ç);
GetDlgItemText(č, c, ₵, C);
₵[C] = 0;
return vsscanf(₵, Ç, Ĉ);
}If you use this... your coworkers will pick you up and throw you out of the office. :-D Best Wishes, -David Delaune
-
Sorry. I started computer programming in the late 1960's, when computer storage was much smaller (80,000 words memory was big), under early compilers such as Atlas Autocode and Basic, and I got accustomed to one-letter and two-letter variable names. Thanks for your help.
-
Anthony Appleyard wrote:
I got accustomed to one-letter and two-letter variable names.
I confirmed that it compiles in Visual Studio 2017. I am calling it "C Notation" naming convention.
\#define C 256
int Ƈ(HWND č, int c, char* Ç, ...)
{
char ₵[C];
va_list Ĉ;
va_start(Ĉ, Ç);
GetDlgItemText(č, c, ₵, C);
₵[C] = 0;
return vsscanf(₵, Ç, Ĉ);
}If you use this... your coworkers will pick you up and throw you out of the office. :-D Best Wishes, -David Delaune