Character Exception
-
Hi, Im having a LPSTR variable.I want to copy a string to this variable. The application is getting crashed while copying this string.I want to catch this exception.I have used exception class,but its not capturing that one.Please suggest me to do this.Im using the following code. LPSTR lpszstr; try { wsprintf(lpszstr,"%s","sdfsdfsd fsdfsdfsdf"); } catch (CException *why) { ::MessageBox(NULL,"test123","test",MB_OK); } Thanks,
-
Hi, Im having a LPSTR variable.I want to copy a string to this variable. The application is getting crashed while copying this string.I want to catch this exception.I have used exception class,but its not capturing that one.Please suggest me to do this.Im using the following code. LPSTR lpszstr; try { wsprintf(lpszstr,"%s","sdfsdfsd fsdfsdfsdf"); } catch (CException *why) { ::MessageBox(NULL,"test123","test",MB_OK); } Thanks,
People on these forums seem a bit obsessed by exceptions... - wsprintf is a windows API function. Windows API function have a C interface, not a C++ one. As C compilers don't understand C++ exception handling Windows API functions don't throw C++ exceptions. Therefore whatever wsprintf does it won't throw, ever. - CException is an MFC class. The only thing that throws exceptions based on CException is MFC and clients of MFC. Even if the previous point didn't apply then wsprintf couldn't throw a CException. - IF wsprintf threw a CException derived class you're still using it wrong. MFC (due to it's long history of working on brain damaged compilers) throws exceptions by pointer and not by value. When you catch an MFC exception AND handle it you have to delete the exception afterwards. Not generally using the delete operator either. Have a look at the docs for details. - finally even if wsprintf threw what you want it to catching it would be a bad idea. You've got code that's writing to a random area of memory. Do you really expect to be able to scribble over your process address space and get away with it? Sometimes you might be able to recover and keep going, hopefully most of the time your code will crash in a heap. So: - stop obsessing about exceptions but if you can't stop learn the difference between a processor exception, an operating system exception and a C++ exception - stop mucking about with low level stuff. Don't use array of characters use vectors or strings. Don't think they're something advanced - it's the stuff you're doing (pointers and arrays and not knowing the differences between them, using C functions like sprintf when stringstreams do in a far safer manner) that's advanced Ash
-
Hi, Im having a LPSTR variable.I want to copy a string to this variable. The application is getting crashed while copying this string.I want to catch this exception.I have used exception class,but its not capturing that one.Please suggest me to do this.Im using the following code. LPSTR lpszstr; try { wsprintf(lpszstr,"%s","sdfsdfsd fsdfsdfsdf"); } catch (CException *why) { ::MessageBox(NULL,"test123","test",MB_OK); } Thanks,
In addition to what Ash said, note that:
- LPSTR is a pointer to a multi-byte string (i.e.
char *
), but you are usingwsprintf
which is an unicode function (i.e. it works onwchar_t
) - when used with
wsprintf
, the format string"%s"
stand for an unicode string, but you are passing a multi-byte string instead: probably this is the reason of the crashes you are experiencing. Replacewsprintf
withsprintf
- to avoid problems with buffer overrun improved versions of
sprintf
andwsprintf
are privided: think usingprintf_s
andwsprintf_s
- LPSTR is a pointer to a multi-byte string (i.e.
-
Hi, Im having a LPSTR variable.I want to copy a string to this variable. The application is getting crashed while copying this string.I want to catch this exception.I have used exception class,but its not capturing that one.Please suggest me to do this.Im using the following code. LPSTR lpszstr; try { wsprintf(lpszstr,"%s","sdfsdfsd fsdfsdfsdf"); } catch (CException *why) { ::MessageBox(NULL,"test123","test",MB_OK); } Thanks,
LPSTR lpszstr;
this is a pointer, not a string. and it doesn't point anywhere (you haven't initialized it). you need to allocate some memory for a string, and then use that in your wsprintf. you need something like this:
TCHAR str[100];
wsprintf(str,"%s","sdfsdfsd fsdfsdfsdf");also, it might be good to read about the differences between pointers and arrays[^].
-
Hi, Im having a LPSTR variable.I want to copy a string to this variable. The application is getting crashed while copying this string.I want to catch this exception.I have used exception class,but its not capturing that one.Please suggest me to do this.Im using the following code. LPSTR lpszstr; try { wsprintf(lpszstr,"%s","sdfsdfsd fsdfsdfsdf"); } catch (CException *why) { ::MessageBox(NULL,"test123","test",MB_OK); } Thanks,
-
In addition to what Ash said, note that:
- LPSTR is a pointer to a multi-byte string (i.e.
char *
), but you are usingwsprintf
which is an unicode function (i.e. it works onwchar_t
) - when used with
wsprintf
, the format string"%s"
stand for an unicode string, but you are passing a multi-byte string instead: probably this is the reason of the crashes you are experiencing. Replacewsprintf
withsprintf
- to avoid problems with buffer overrun improved versions of
sprintf
andwsprintf
are privided: think usingprintf_s
andwsprintf_s
Hi Sauro, wsprintf (which resolves into wsprintfA and wsprintfW) is a version of either sprintf or swprintf implemented by USER (IIRC, might be KERNEL). swprintf is the unicode sprintf like function - nice of them to change a couple of letters like that! To add to the confusion wsprintf can't handle any floating point formats the way sprintf and swprintf can and can only handle 1k of characters. Cheers, Ash
- LPSTR is a pointer to a multi-byte string (i.e.
-
In addition to what Ash said, note that:
- LPSTR is a pointer to a multi-byte string (i.e.
char *
), but you are usingwsprintf
which is an unicode function (i.e. it works onwchar_t
) - when used with
wsprintf
, the format string"%s"
stand for an unicode string, but you are passing a multi-byte string instead: probably this is the reason of the crashes you are experiencing. Replacewsprintf
withsprintf
- to avoid problems with buffer overrun improved versions of
sprintf
andwsprintf
are privided: think usingprintf_s
andwsprintf_s
Sauro Viti wrote:
wsprintf which is an unicode function
1. Not true, it works on either and is actually defined to
wsprintfA
orwsprintfW
depending on the setting ofUNICODE
. 2. see 1, but also the reason for the crash is thatlpszstr
is not initialised to point to anything. 3.wsprintf_s
does not exist. As far as I recall the wxxx functions were created for windows programs that did not includestdio.h
.It's time for a new signature.
- LPSTR is a pointer to a multi-byte string (i.e.