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
367 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

862 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now