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. WriteProcessMemory error 5

WriteProcessMemory error 5

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
9 Posts 3 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.
  • S Offline
    S Offline
    Snoepie
    wrote on last edited by
    #1

    X| when excuting this (procID is a control panel applet) HANDLE lView=OpenProcess(PROCESS_ALL_ACCESS,false,procID); if (lView) { if ( !LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tokenLuid ) ) j=GetLastError(); tokenPriv.PrivilegeCount = 1; tokenPriv.Privileges[0].Luid = tokenLuid; tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tokenHandle=NULL; if (!OpenProcessToken(lView,TOKEN_ADJUST_PRIVILEGES,&tokenHandle)) j=GetLastError(); if ( !AdjustTokenPrivileges(tokenHandle,FALSE,&tokenPriv,sizeof(TOKEN_PRIVILEGES) , (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) ) j=GetLastError(); // Allocate virtual memory for ListView LVITEM *_lvi=(LVITEM*)VirtualAllocEx(lView, NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE); if (!VirtualQueryEx(lView,_lvi,&memBasicInfo,sizeof(MEMORY_BASIC_INFORMATION))) j=GetLastError(); no errors are triggered, memBasicInfo returns the correct parameters, ie i have full access to the allocated memory. But if i follow it up by: if (WriteProcessMemory(lView, _lvi, &lvi, sizeof(LVITEM),&byteCount)) j=GetLastError(); i still get error 5, but bytecount gives the correct count. I disabled the antivirus scanner, so it's not interfering I am flabbergasted.

    C L 2 Replies Last reply
    0
    • S Snoepie

      X| when excuting this (procID is a control panel applet) HANDLE lView=OpenProcess(PROCESS_ALL_ACCESS,false,procID); if (lView) { if ( !LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tokenLuid ) ) j=GetLastError(); tokenPriv.PrivilegeCount = 1; tokenPriv.Privileges[0].Luid = tokenLuid; tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tokenHandle=NULL; if (!OpenProcessToken(lView,TOKEN_ADJUST_PRIVILEGES,&tokenHandle)) j=GetLastError(); if ( !AdjustTokenPrivileges(tokenHandle,FALSE,&tokenPriv,sizeof(TOKEN_PRIVILEGES) , (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) ) j=GetLastError(); // Allocate virtual memory for ListView LVITEM *_lvi=(LVITEM*)VirtualAllocEx(lView, NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE); if (!VirtualQueryEx(lView,_lvi,&memBasicInfo,sizeof(MEMORY_BASIC_INFORMATION))) j=GetLastError(); no errors are triggered, memBasicInfo returns the correct parameters, ie i have full access to the allocated memory. But if i follow it up by: if (WriteProcessMemory(lView, _lvi, &lvi, sizeof(LVITEM),&byteCount)) j=GetLastError(); i still get error 5, but bytecount gives the correct count. I disabled the antivirus scanner, so it's not interfering I am flabbergasted.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Why are you calling GetLastError on WriteprocessMemory success? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S 1 Reply Last reply
      0
      • S Snoepie

        X| when excuting this (procID is a control panel applet) HANDLE lView=OpenProcess(PROCESS_ALL_ACCESS,false,procID); if (lView) { if ( !LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tokenLuid ) ) j=GetLastError(); tokenPriv.PrivilegeCount = 1; tokenPriv.Privileges[0].Luid = tokenLuid; tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; tokenHandle=NULL; if (!OpenProcessToken(lView,TOKEN_ADJUST_PRIVILEGES,&tokenHandle)) j=GetLastError(); if ( !AdjustTokenPrivileges(tokenHandle,FALSE,&tokenPriv,sizeof(TOKEN_PRIVILEGES) , (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL) ) j=GetLastError(); // Allocate virtual memory for ListView LVITEM *_lvi=(LVITEM*)VirtualAllocEx(lView, NULL, sizeof(LVITEM),MEM_COMMIT, PAGE_READWRITE); if (!VirtualQueryEx(lView,_lvi,&memBasicInfo,sizeof(MEMORY_BASIC_INFORMATION))) j=GetLastError(); no errors are triggered, memBasicInfo returns the correct parameters, ie i have full access to the allocated memory. But if i follow it up by: if (WriteProcessMemory(lView, _lvi, &lvi, sizeof(LVITEM),&byteCount)) j=GetLastError(); i still get error 5, but bytecount gives the correct count. I disabled the antivirus scanner, so it's not interfering I am flabbergasted.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        GetLastError() returns the last error that occurred, which didn't necessarily happen in the last function call. That is way it is called GetLastError() and not GetError(). You normally want to stop a sequence of operations at the first error, so you could and should structure your code like this:

        result=callFunction1();
        if (result==OK) result=callFunction2();
        if (result==OK) result=callFunction3();
        if (result!=OK) log("it went wrong; error = ", GetLastError());

        :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        S 1 Reply Last reply
        0
        • C CPallini

          Why are you calling GetLastError on WriteprocessMemory success? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          Snoepie
          wrote on last edited by
          #4

          the call doesn't complete..... The bytecount written is correct, but the memory addressed by the call stays empty. Also in MSDN is stated that a Write call my fail whilst still returning no fail error code. After doing a sendmessage LVM_GetItemState to listView, the call returns empty as well. So the memory location seems to be inacessible, which is bizarre because i also have this: lvi.pszText=_item; WriteProcessMemory(lView, _lvi, &lvi, sizeof(LVITEM), NULL); SendMessage(listView, LVM_GETITEMTEXT, NULL, (LPARAM)_lvi); ReadProcessMemory(lView, _item, item, 512, NULL); and that returns with the correct itemtext......

          1 Reply Last reply
          0
          • L Luc Pattyn

            GetLastError() returns the last error that occurred, which didn't necessarily happen in the last function call. That is way it is called GetLastError() and not GetError(). You normally want to stop a sequence of operations at the first error, so you could and should structure your code like this:

            result=callFunction1();
            if (result==OK) result=callFunction2();
            if (result==OK) result=callFunction3();
            if (result!=OK) log("it went wrong; error = ", GetLastError());

            :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            S Offline
            S Offline
            Snoepie
            wrote on last edited by
            #5

            tnx for the input, but the original code is spiked with GetLastErrors, took some out to shorten code for viewing.

            L 1 Reply Last reply
            0
            • S Snoepie

              tnx for the input, but the original code is spiked with GetLastErrors, took some out to shorten code for viewing.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Yeah, great. When the error handling code indicates some problem, show us fictitious code. Someone will make the right guess anyhow? :~

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              C 1 Reply Last reply
              0
              • L Luc Pattyn

                Yeah, great. When the error handling code indicates some problem, show us fictitious code. Someone will make the right guess anyhow? :~

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                They must protect their brilliant code against industrial espionage, my friend. :rolleyes:

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                S 1 Reply Last reply
                0
                • C CPallini

                  They must protect their brilliant code against industrial espionage, my friend. :rolleyes:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  S Offline
                  S Offline
                  Snoepie
                  wrote on last edited by
                  #8

                  :~ found it: Microsoft Windows Vista and later. When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied). took me a full week to discover this tidbit. Tnx MS

                  C 1 Reply Last reply
                  0
                  • S Snoepie

                    :~ found it: Microsoft Windows Vista and later. When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied). took me a full week to discover this tidbit. Tnx MS

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Good: tenacity wins, eventually. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    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