 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Serenity
Joined: 08 Nov 2004 Posts: 1010
|
Posted: Thu Oct 04, 2007 7:14 pm Post subject: Getting the path of a file opened in a program |
|
|
I have a program which I'd like the title to show the file name of the open file. The program is a simple viewer for small images, so maybe it just reads the file into memory and closes it rather than keep it open for editing.
I thought about monitoring for the open dialogs edit box with a timer, hoping to catch the last entry before hitting open, but I want a more secure way. Is there a way of intercepting the path of the file to be opened as it is passed from explorer to this program? Or perhaps finding out from the program what file it has open? _________________ "Anything worth doing is worth doing slowly." - Mae West
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Thu Oct 04, 2007 8:18 pm Post subject: Re: Getting the path of a file opened in a program |
|
|
| Serenity wrote: | | Is there a way of intercepting the path of the file to be opened as it is passed from explorer to this program? Or perhaps finding out from the program what file it has open? |
Only If I am getting you correctly..
You can fetch the command line parameter with shimanov's GetRemoteCommandLine()
 |
|
| Back to top |
|
 |
Serenity
Joined: 08 Nov 2004 Posts: 1010
|
Posted: Fri Oct 05, 2007 12:34 am Post subject: |
|
|
| Thanks, although I'm not sure how I would use shimanov's script in this case - it seems to list processes like the task manager. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Fri Oct 05, 2007 6:30 am Post subject: |
|
|
| Serenity wrote: | | it seems to list processes like the task manager. |
I meant this:
| Code: | ^F9::
WinGet, PID, PID, A
MsgBox, % GetRemoteCommandLine( PID )
Return
GetRemoteCommandLine( p_pid_target )
{
hp_target := DllCall( "OpenProcess"
, "uint", 0x10 ; PROCESS_VM_READ
, "int", false
, "uint", p_pid_target )
if ( ErrorLevel or hp_target = 0 )
{
result = < error: OpenProcess > EL = %ErrorLevel%, LE = %A_LastError%, hp_target = %hp_target%
Gosub, return
}
hm_kernel32 := DllCall( "GetModuleHandle", "str", "kernel32.dll" )
pGetCommandLineA := DllCall( "GetProcAddress", "uint", hm_kernel32, "str", "GetCommandLineA" )
buffer_size = 6
VarSetCapacity( buffer, buffer_size )
success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pGetCommandLineA, "uint", &buffer, "uint", buffer_size, "uint", 0 )
if ( ErrorLevel or !success )
{
result = < error: ReadProcessMemory 1 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%
Gosub, return
}
loop, 4
ppCommandLine += ( ( *( &buffer+A_Index ) ) << ( 8*( A_Index-1 ) ) )
buffer_size = 4
VarSetCapacity( buffer, buffer_size, 0 )
success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", ppCommandLine, "uint", &buffer, "uint", buffer_size, "uint", 0 )
if ( ErrorLevel or !success )
{
result = < error: ReadProcessMemory 2 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%
Gosub, return
}
loop, 4
pCommandLine += ( ( *( &buffer+A_Index-1 ) ) << ( 8*( A_Index-1 ) ) )
buffer_size = 32768
VarSetCapacity( result, buffer_size, 1 )
success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine, "uint", &result, "uint", buffer_size, "uint", 0 )
if ( !success )
{
loop, %buffer_size%
{
success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine+A_Index-1, "uint", &result, "uint", 1, "uint", 0 )
if ( !success or Asc( result ) = 0 )
{
buffer_size := A_Index
break
}
}
success := DllCall( "ReadProcessMemory", "uint", hp_target, "uint", pCommandLine, "uint", &result, "uint", buffer_size, "uint", 0 )
if ( ErrorLevel or !success )
{
result = < error: ReadProcessMemory 3 > EL = %ErrorLevel%, LE = %A_LastError%, success = %success%
Gosub, return
}
}
return:
DllCall( "CloseHandle", "uint", hp_target )
return, result
} |
.. shows command line parameter which contains the fullpath of the target filename. I wonder if we could modify it to search the process memory for the current open file.
 |
|
| Back to top |
|
 |
Serenity
Joined: 08 Nov 2004 Posts: 1010
|
Posted: Fri Oct 05, 2007 6:48 am Post subject: |
|
|
| This would be great, although I wouldn't know where to start. :( When I press Ctrl+F9 all I get is the process name, "gbcamdump". |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Fri Oct 05, 2007 7:26 am Post subject: |
|
|
For example, after opening an AHK script from right-click context menu ( Edit Script ), ^F9 fetches me the following:
"metapad.exe" E:\Documents and Settings\Skan\Desktop\RunAHK.ahk
That is what I used it for: To retrieve the file name from editor and run it.
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Fri Oct 05, 2007 10:14 am Post subject: Re: Getting the path of a file opened in a program |
|
|
| Serenity wrote: | | Is there a way of intercepting the path of the file to be opened as it is passed from explorer to this program? |
Should be possible if you can hook on to the target application and monitor the WM_Notify Message.
The target application will receive CDN_FILEOK Notification when Ok button is pressed on Open File dialog.
lParam of the msg will be a pointer to OFNOTIFY which will contain a pointer to OPENFILENAME from which the fullpath to opened file name can be retrieved.
I have no experience in hook and not sure if it can be done. Search the forum. There have been some posts on the topic.
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Fri Oct 05, 2007 10:24 am Post subject: |
|
|
[ Re: Hook ] You can find some useful information here  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|