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
360 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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 54
wordlen challenge 3 46
pairs challenge 5 44
recursion example 16 66
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…
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…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

771 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

11 Experts available now in Live!

Get 1:1 Help Now