Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Character Exception

Character Exception

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pryabu
    wrote on last edited by
    #1

    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,

    A S C C 4 Replies Last reply
    0
    • P Pryabu

      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,

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P Pryabu

        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,

        S Offline
        S Offline
        Sauro Viti
        wrote on last edited by
        #3

        In addition to what Ash said, note that:

        1. LPSTR is a pointer to a multi-byte string (i.e. char *), but you are using wsprintf which is an unicode function (i.e. it works on wchar_t)
        2. 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. Replace wsprintf with sprintf
        3. to avoid problems with buffer overrun improved versions of sprintf and wsprintf are privided: think using printf_s and wsprintf_s
        A L 2 Replies Last reply
        0
        • P Pryabu

          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,

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          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[^].

          image processing toolkits | batch image processing

          1 Reply Last reply
          0
          • P Pryabu

            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,

            C Offline
            C Offline
            Cool_Dev
            wrote on last edited by
            #5

            if you didn't allocate enough memory for lpszstr to hold the string you copy to it, definitly access violation will happen, and as wsprintf() doesn't throw any exception, you can't expect to catch it

            1 Reply Last reply
            0
            • S Sauro Viti

              In addition to what Ash said, note that:

              1. LPSTR is a pointer to a multi-byte string (i.e. char *), but you are using wsprintf which is an unicode function (i.e. it works on wchar_t)
              2. 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. Replace wsprintf with sprintf
              3. to avoid problems with buffer overrun improved versions of sprintf and wsprintf are privided: think using printf_s and wsprintf_s
              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S Sauro Viti

                In addition to what Ash said, note that:

                1. LPSTR is a pointer to a multi-byte string (i.e. char *), but you are using wsprintf which is an unicode function (i.e. it works on wchar_t)
                2. 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. Replace wsprintf with sprintf
                3. to avoid problems with buffer overrun improved versions of sprintf and wsprintf are privided: think using printf_s and wsprintf_s
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Sauro Viti wrote:

                wsprintf which is an unicode function

                1. Not true, it works on either and is actually defined to wsprintfA or wsprintfW depending on the setting of UNICODE. 2. see 1, but also the reason for the crash is that lpszstr 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 include stdio.h.

                It's time for a new signature.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups