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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using __toascii() got me a runtime access violation

Using __toascii() got me a runtime access violation

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
8 Posts 4 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.
  • G Offline
    G Offline
    glyfyx
    wrote on last edited by
    #1

    I'm very rusty - haven't coded C/C++ since 1994 - and I'm sure there's a simple answer, but I can't find it. I'm not using 'string' support. Just the old char* methods, and the code is behaving well except when I tried to convert a keyboard virtual key code to a it's ascii character I got a runtime access violation. The 'break' option showed me the site of the infraction - inside some MS code located in the midst of some UNICODE related logic, judging by the Defines. I included , but it didn't help. The related trimmed code follows: char c; c = (char)__toascii(keyCode); keyCode = p->vkCode; The offending later statement is: nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c); The following statement works perfectly (as above, but no c at the end) nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick); Searched the articles and FAQ but didn't find anything. Hope you can give me a hand here. BTW, I want to stay in the char* arena rather than strings, for now. I'm using Visual C++ 2003 and running Win2K SP4 One more thing - I'm logged in under my old email glyfyx@storm.ca but I have a new address, glyfix@storm.ca. I guess I should change it at Code Project? Thanks glyfyx

    M R M 3 Replies Last reply
    0
    • G glyfyx

      I'm very rusty - haven't coded C/C++ since 1994 - and I'm sure there's a simple answer, but I can't find it. I'm not using 'string' support. Just the old char* methods, and the code is behaving well except when I tried to convert a keyboard virtual key code to a it's ascii character I got a runtime access violation. The 'break' option showed me the site of the infraction - inside some MS code located in the midst of some UNICODE related logic, judging by the Defines. I included , but it didn't help. The related trimmed code follows: char c; c = (char)__toascii(keyCode); keyCode = p->vkCode; The offending later statement is: nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c); The following statement works perfectly (as above, but no c at the end) nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick); Searched the articles and FAQ but didn't find anything. Hope you can give me a hand here. BTW, I want to stay in the char* arena rather than strings, for now. I'm using Visual C++ 2003 and running Win2K SP4 One more thing - I'm logged in under my old email glyfyx@storm.ca but I have a new address, glyfix@storm.ca. I guess I should change it at Code Project? Thanks glyfyx

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      If c is a char, you should use %c. That's all. :-D

      Maxwell Chen

      M G 2 Replies Last reply
      0
      • G glyfyx

        I'm very rusty - haven't coded C/C++ since 1994 - and I'm sure there's a simple answer, but I can't find it. I'm not using 'string' support. Just the old char* methods, and the code is behaving well except when I tried to convert a keyboard virtual key code to a it's ascii character I got a runtime access violation. The 'break' option showed me the site of the infraction - inside some MS code located in the midst of some UNICODE related logic, judging by the Defines. I included , but it didn't help. The related trimmed code follows: char c; c = (char)__toascii(keyCode); keyCode = p->vkCode; The offending later statement is: nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c); The following statement works perfectly (as above, but no c at the end) nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick); Searched the articles and FAQ but didn't find anything. Hope you can give me a hand here. BTW, I want to stay in the char* arena rather than strings, for now. I'm using Visual C++ 2003 and running Win2K SP4 One more thing - I'm logged in under my old email glyfyx@storm.ca but I have a new address, glyfix@storm.ca. I guess I should change it at Code Project? Thanks glyfyx

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #3

        glyfyx wrote:

        I tried to convert a keyboard virtual key code to a it's ascii character

        if you are converting from Keyboard Virtual KeyCode, use int ToAscii( UINT uVirtKey, UINT uScanCode, PBYTE lpKeyState, LPWORD lpChar, UINT uFlags );

        glyfyx wrote:

        The offending later statement is: nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c);

        format specifier is %s and passing char, c.

        1 Reply Last reply
        0
        • G glyfyx

          I'm very rusty - haven't coded C/C++ since 1994 - and I'm sure there's a simple answer, but I can't find it. I'm not using 'string' support. Just the old char* methods, and the code is behaving well except when I tried to convert a keyboard virtual key code to a it's ascii character I got a runtime access violation. The 'break' option showed me the site of the infraction - inside some MS code located in the midst of some UNICODE related logic, judging by the Defines. I included , but it didn't help. The related trimmed code follows: char c; c = (char)__toascii(keyCode); keyCode = p->vkCode; The offending later statement is: nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c); The following statement works perfectly (as above, but no c at the end) nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick); Searched the articles and FAQ but didn't find anything. Hope you can give me a hand here. BTW, I want to stay in the char* arena rather than strings, for now. I'm using Visual C++ 2003 and running Win2K SP4 One more thing - I'm logged in under my old email glyfyx@storm.ca but I have a new address, glyfix@storm.ca. I guess I should change it at Code Project? Thanks glyfyx

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          The arguments you pass to fprintf() need to match the format specifiers in the format control string, in both type and number of arguments. All the "%d"s in the format control string need a corresponding 32-bit integer argument passsed... All the "%s"s in the format control string need a corresponding pointer to a null-terminated string passed. The version where you don't pass the c argument  only appears to work, since you pass 5 arguments when the format control string specifies an expected 6 arguments. The version that crashes probably fails because c is a char but the format specifier is %s, which means the passed argument should be a pointer to a NULL-terminated string, not a single character. If you do something like this, it should work:

          char c[2];
          c[0] = (char)__toascii(keyCode);
          c[1] = '\0';
          keyCode = p->vkCode;
          ...
          nLen = fprintf(fOut,"kcK=%d ud=%s kc=%d sh=%d ti=%d %s\n",cb.kcK,updn,keyCode,Shift,tick, c);

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • M Maxwell Chen

            If c is a char, you should use %c. That's all. :-D

            Maxwell Chen

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            :doh: I forgot about %c....do you need to cast the c argument to an int when it's passed as an argument for %c? I have't used that stuff in a long time :) Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              :doh: I forgot about %c....do you need to cast the c argument to an int when it's passed as an argument for %c? I have't used that stuff in a long time :) Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #6

              Mark Salsbery wrote:

              I forgot about %c....do you need to cast the c argument to an int when it's passed as an argument for %c?

              No, not needed. Simply this way is fine. :-D

              char a = 65;
              printf("%c \n", a);
              // Result:
              // A
              // Press any key to exit.

              Maxwell Chen

              M 1 Reply Last reply
              0
              • M Maxwell Chen

                Mark Salsbery wrote:

                I forgot about %c....do you need to cast the c argument to an int when it's passed as an argument for %c?

                No, not needed. Simply this way is fine. :-D

                char a = 65;
                printf("%c \n", a);
                // Result:
                // A
                // Press any key to exit.

                Maxwell Chen

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Cool thanks! Cheers, Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • M Maxwell Chen

                  If c is a char, you should use %c. That's all. :-D

                  Maxwell Chen

                  G Offline
                  G Offline
                  glyfyx
                  wrote on last edited by
                  #8

                  Of course, %c for char! I was sure it had a simple answer but couldn't see it for looking - most embarrassing. Too many years of VB? So big thanks Maxwell and Mark! Also Mark, I was starting to think of assembling a 2 byte char array as you suggested - that'd work with %s. Also special thanks to Rajkumar R for the ToAscii suggestion, which is intriguing to say the least, since it requires a GetKeyboardState call to set its 256 byte array arg, lpKeyState, to the current status of *all* the virtual keys. I would have thought GetKeyState for just the virtual key in question (1st arg, uVirtKey),would be sufficient. I suppose ToAscii will be a case-sensitive conversion - will give it a shot and see what happens. int ToAscii( UINT uVirtKey, UINT uScanCode, PBYTE lpKeyState, LPWORD lpChar, UINT uFlags ); Here's the MSDN article... http://msdn2.microsoft.com/en-us/library/ms646299(VS.85).aspx Thanks again everyone! glyfyx

                  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