Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert WinApi type to Qt type

Convert WinApi type to Qt type

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.6k 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.
  • L Offline
    L Offline
    lolopolosko
    wrote on last edited by
    #1

    Hi
    This code is working fine, but in ending I got error (debug) "Exception at 0x5313a407, code: 0xc0000005: read access violation at: 0x0, flags=0x0"
    @ HWND activeWindow;
    activeWindow = GetForegroundWindow();
    LPWSTR l_windowTitle;
    SetWindowText(activeWindow,L"Hi!");
    GetWindowText(activeWindow,l_windowTitle,256);
    QString wchr = QString::fromWCharArray(l_windowTitle);
    qDebug()<<wchr;@
    Why?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lolopolosko
      wrote on last edited by
      #2

      Edit code
      @ HWND activeWindow;
      activeWindow = GetForegroundWindow();
      LPWSTR l_windowTitle = L"";
      LPWSTR l_windowTitle2;
      GetWindowText(activeWindow,l_windowTitle,256);
      GetWindowText(activeWindow,l_windowTitle2,256);

      qDebug()<<QString::fromWCharArray(l_windowTitle);
      qDebug()<<QString::fromWCharArray(l_windowTitle2);@
      

      output without error
      ""
      ""


      @ HWND activeWindow;
      activeWindow = GetForegroundWindow();
      LPWSTR l_windowTitle;
      GetWindowText(activeWindow,l_windowTitle,256);

      qDebug()<<QString::fromWCharArray(l_windowTitle);
      

      @

      output with error
      "Title message"

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lolopolosko
        wrote on last edited by
        #3

        And now I am editing my code and this work perfectly. But why? ^_^

        @ HWND activeWindow;
        activeWindow = GetForegroundWindow();
        char testVar[256];
        GetWindowTextA(activeWindow,testVar,sizeof testVar);

        qDebug()<<testVar;@
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #4

          How should it work if you didn't initialized your wchar_t variables?
          LPWSTR is simple pointer in memory of wchar_t type:
          @
          // LPWSTR l_windowTitle is the same as
          wchar_t * windowTitle;
          @

          And after what you are trying to get 256 bytes to this uninitialized place in memory, of cause it will fail!

          @
          //
          HWND activeWindow = GetForegroundWindow();
          LPWSTR l_windowTitle = new wchar_t[256]; // or better use winapi memory initialization functions if you use wrappers for wchar_t type
          GetWindowText(activeWindow,l_windowTitle,255);// let 1 byte for \0
          qDebug()<<QString::fromWCharArray(l_windowTitle);
          // free memory after you don't need it anymore
          delete [] l_windowTitle;
          @

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lolopolosko
            wrote on last edited by
            #5

            Thanks ^_^

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AcerExtensa
              wrote on last edited by
              #6

              Please add "[SOLVED]" prefix left to the subject topic. And have fun with coding!

              God is Real unless explicitly declared as Integer.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved