Solved

?I would like a VBS or other program that I can call that will give me the complete PATH and FILE name of the CALLING program?

Posted on 2011-02-10
5
370 Views
Last Modified: 2012-05-11
I would like to know where a program is located.

Therefore,
I would like a program that I can call that will tell me the complete windows path and filename of the calling program.

For example.
Let's suppose that I have a program named MYprog.exe
and it is located in:
c:\Program Files\MyApplication\CurrentProduction

Let's suppose the new program that you supply would be named:
ReturnsCallersLocation.VBS

Then,
I would like to be able to have MYprog.exe call ReturnsCallersLocation.vbs,
  which would return a string containing:
"c:\Program Files\MyApplication\CurrentProduction\MYprog.exe"

I "think"
VBS script would be easiest for me to use.
But, other languages could be acceptable.

Thanks
Phil





0
Comment
Question by:philkryder
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34865257
Not sure if you can do that direct . . . but a work around would be to force your script to have a parameter that accepts the path & program name.  If it's not there error out of your script.
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 34865362
I have a script that will work using AutoIT, on one condition;

Within MYprog.exe, are you able to call this script with a switch that is MYprog.exe? For example, the script below (which we'll call ReturnsCallersLocation.exe) would need to be run like this:

ReturnsCallersLocation.exe MYprog.exe
_ProcessGetLocation($CmdLineRaw)

Func _ProcessGetLocation($iPIDa)
    $iPID=ProcessExists($iPIDa)
	Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID)
    If $aProc[0] = 0 Then Return SetError(1, 0, '')
    Local $vStruct = DllStructCreate('int[1024]')
    DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc[0], 'ptr', DllStructGetPtr($vStruct), 'int', DllStructGetSize($vStruct), 'int_ptr', 0)
    Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc[0], 'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048)
    If StringLen($aReturn[3]) = 0 Then Return SetError(2, 0, '')
    Return $aReturn[3]
EndFunc

Open in new window

0
 
LVL 7

Expert Comment

by:justadad
ID: 34865672
Is this to troubleshoot a program to make sure the correct one is the one running? Or that it is running the correct script?

As I read your question, you are asking an external application to determine something for you that is available in the application itself.
 Every dos/windows programming language I have used has a function in it to determine the path of the program itself. ie If you are creating a myprog.exe there are calls from whatever programing language you are using to determine what you ask.

If you are trying to determine in the script if the correct program has called it then TheGorby's solution would work, but if you are trying to determine for myprog.exe where it is running from there are far simpler ways available to run inside the myprog.exe itself.

0
 
LVL 1

Author Comment

by:philkryder
ID: 34867916

I am using an automation tool that does NOT have a user interface provitding the ability to compute it's own location.

thus, I would like to have a program that I can call that will determine the location of the calling program.



I am not sure what auto.IT is

I don't know how I would instantiate the switch equal to the calling program.
Phil
0
 
LVL 11

Accepted Solution

by:
TheGorby earned 500 total points
ID: 34870987
Ok, I found some code (http://www.autoitscript.com/forum/topic/78445-solved-parent-process-child-process) that doesn't require the program to be called with a switch.

AutoIT is a programming language mostly used for automating program installations. Scripts written with it must be compiled using the built-in script editor that comes with AutoIT when you download it, or the scripts won't run on other computers. You can get it here: http://www.autoitscript.com/site/autoit/
$parent=_ProcessGetParent(@AutoItPID)

_ProcessGetLocation($parent)

Func _ProcessGetParent($i_pid)
    Local Const $TH32CS_SNAPPROCESS = 0x00000002

    Local $a_tool_help = DllCall("Kernel32.dll", "long", "CreateToolhelp32Snapshot", "int", $TH32CS_SNAPPROCESS, "int", 0)
    If IsArray($a_tool_help) = 0 Or $a_tool_help[0] = -1 Then Return SetError(1, 0, $i_pid)

    Local $tagPROCESSENTRY32 = _
        DllStructCreate _
            ( _
                "dword dwsize;" & _
                "dword cntUsage;" & _
                "dword th32ProcessID;" & _
                "uint th32DefaultHeapID;" & _
                "dword th32ModuleID;" & _
                "dword cntThreads;" & _
                "dword th32ParentProcessID;" & _
                "long pcPriClassBase;" & _
                "dword dwFlags;" & _
                "char szExeFile[260]" _
            )
    DllStructSetData($tagPROCESSENTRY32, 1, DllStructGetSize($tagPROCESSENTRY32))

    Local $p_PROCESSENTRY32 = DllStructGetPtr($tagPROCESSENTRY32)

    Local $a_pfirst = DllCall("Kernel32.dll", "int", "Process32First", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
    If IsArray($a_pfirst) = 0 Then Return SetError(2, 0, $i_pid)

    Local $a_pnext, $i_return = 0
    If DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") = $i_pid Then
        $i_return = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID")
        DllCall("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0])
        If $i_return Then Return $i_return
        Return $i_pid
    EndIf

    While 1
        $a_pnext = DLLCall("Kernel32.dll", "int", "Process32Next", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
        If IsArray($a_pnext) And $a_pnext[0] = 0 Then ExitLoop
        If DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") = $i_pid Then
            $i_return = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID")
            If $i_return Then ExitLoop
            $i_return = $i_pid
            ExitLoop
        EndIf
    WEnd

    If $i_return = "" Then $i_return = $i_pid

    DllCall("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0])
    Return $i_return
EndFunc

Func _ProcessGetLocation($iPID)
    ;$iPID=ProcessExists($iPIDa)
	Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID)
    If $aProc[0] = 0 Then Return SetError(1, 0, '')
    Local $vStruct = DllStructCreate('int[1024]')
    DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc[0], 'ptr', DllStructGetPtr($vStruct), 'int', DllStructGetSize($vStruct), 'int_ptr', 0)
    Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc[0], 'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048)
    If StringLen($aReturn[3]) = 0 Then Return SetError(2, 0, '')
    Return $aReturn[3]
EndFunc

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question