AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting the path of a file opened in a program

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Serenity



Joined: 08 Nov 2004
Posts: 1010

PostPosted: Thu Oct 04, 2007 7:14 pm    Post subject: Getting the path of a file opened in a program Reply with quote

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
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Thu Oct 04, 2007 8:18 pm    Post subject: Re: Getting the path of a file opened in a program Reply with quote

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()

Smile
Back to top
View user's profile Send private message
Serenity



Joined: 08 Nov 2004
Posts: 1010

PostPosted: Fri Oct 05, 2007 12:34 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Fri Oct 05, 2007 6:30 am    Post subject: Reply with quote

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.

Smile
Back to top
View user's profile Send private message
Serenity



Joined: 08 Nov 2004
Posts: 1010

PostPosted: Fri Oct 05, 2007 6:48 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Fri Oct 05, 2007 7:26 am    Post subject: Reply with quote

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.

Smile
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Fri Oct 05, 2007 10:14 am    Post subject: Re: Getting the path of a file opened in a program Reply with quote

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.

Smile
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Fri Oct 05, 2007 10:24 am    Post subject: Reply with quote

[ Re: Hook ] You can find some useful information here Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group