Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Simple Linear Regression
Introduction to Processes

664 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