Jump to content

DLLCall Problem


Mikon
 Share

Recommended Posts

here is my code

Func ProcessGetFileName($PId)
    Local $PROCESS_ALL_ACCESS = 0x1F0FFF
    Local $P_HANDLE = DllCall("kernel32.dll", "int", "OpenProcess", "int", $PROCESS_ALL_ACCESS, "int", 0, "int", $PId)
    Local $Buffer
    Local $Check = DllCall("psapi.dll", "int", "GetProcessImageFileName", "int", $P_HANDLE[0], "str", $Buffer, "int", 4095);this is the line with the error
    DllCall("kernel32.dll", "int", "CloseHandle", "int", $P_HANDLE[0])
    If $Check[0] Then Return $Buffer
    Return ""
EndFunc

i commented the line, where the problem is

i hope you can help me with this

thanks, Mikon

Link to comment
Share on other sites

use DllStruct.

<{POST_SNAPBACK}>

Wrong answer! Here's why.

here is my code

Func ProcessGetFileName($PId)
    Local $PROCESS_ALL_ACCESS = 0x1F0FFF
    Local $P_HANDLE = DllCall("kernel32.dll", "int", "OpenProcess", "int", $PROCESS_ALL_ACCESS, "int", 0, "int", $PId)
    Local $Buffer
    Local $Check = DllCall("psapi.dll", "int", "GetProcessImageFileName", "int", $P_HANDLE[0], "str", $Buffer, "int", 4095);this is the line with the error
    DllCall("kernel32.dll", "int", "CloseHandle", "int", $P_HANDLE[0])
    If $Check[0] Then Return $Buffer
    Return ""
EndFunc

i commented the line, where the problem is

i hope you can help me with this

thanks, Mikon

<{POST_SNAPBACK}>

Quite simply, this is a RTFM opportunity. The variable $Buffer is not modified itself (Its not even needed). Instead (as the documentation points out), any variables which are changed by the function can be found in the array that is returned. So the information you seek is located in $Check[2], not in $Buffer (Which you don't need, as I mentioned).

Here is the correct code. I had to changed 3 things. First, the correct version of PSAPI.dll was not being found so I explicitly qualified it with a path. Second, because of the first, the script was crashing. I added in code to save @error and check it later on so you won't access an invalid array element if the DllCall() failed. I also removed $Buffer.

Func ProcessGetFileName($PId)
    Local $PROCESS_ALL_ACCESS = 0x1F0FFF
    Local $P_HANDLE = DllCall("kernel32.dll", "int", "OpenProcess", "int", $PROCESS_ALL_ACCESS, "int", 0, "int", $PId)
    Local $Check = DllCall(@SystemDir & "\psapi.dll", "int", "GetProcessImageFileName", "int", $P_HANDLE[0], "str", "", "int", 4095);this is the line with the error
    Local $error = @error; Save @error state
    DllCall("kernel32.dll", "int", "CloseHandle", "int", $P_HANDLE[0])
    If $error Or Not $Check[0] Then Return ""
    Return $Check[2]
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...