Windows Application Path

Using API to get a Windows Application Path

Page 1 of 1

8 Replies - 3250 Views - Last Post: 17 August 2009 - 12:20 PM Rate Topic: -----

#1 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Windows Application Path

Posted 14 August 2009 - 12:15 PM

Hi I need help with how to get the path of an application using API.

I tried using the the Kernel32.DLL entrypoint GetsystemDirectory and that give me the following:

C:Windows\system32

What I really want is the path to the application: for example C:Programfiles\E!PC\WGSSS.exe

Is this possible or should I not be trying to get the information using API calls?

Here is my current code...

Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Text

Module MyExtraOpenWindows

	Public Delegate Function CallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
	Public Declare Function EnumWindows Lib "user32" (ByVal Adress As CallBack, ByVal y As Integer) As Integer
	Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
	Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpWindowText As String, ByVal cch As Integer) As Integer
	Private Const SW_HIDE As Integer = 0
	Private Const SW_RESTORE As Integer = 9
	Private hWnd As Integer
	Public selectedIndex As Integer
	Private ActiveWindows As New System.Collections.ObjectModel.Collection(Of IntPtr)
	<DllImport("KERNEL32.DLL", EntryPoint:="GetSystemDirectoryW", _
		 SetLastError:=True, CharSet:=CharSet.Unicode, _
		 ExactSpelling:=True, _
		 CallingConvention:=CallingConvention.StdCall)> _
		 Public Function GetSystemDirectory(ByVal Buffer _
		   As StringBuilder, ByVal Size As Integer) As Long
		' Leave function empty - DLLImport attribute 
		' forces calls to GetSystemDirectory to
		' be forwarded to GetSystemDirectory in KERNEL32.DLL
	End Function
	Public Const MAX_PATH As Integer = 256
	<DllImport("User32")> Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
	End Function


	Public Function GetActiveWindows() As ObjectModel.Collection(Of IntPtr)
		UserInfoForm.cboSessions.Items.Clear()
		EnumWindows(AddressOf Enumerator, 0)
		Return ActiveWindows
	End Function
	Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean

		Dim Windwtext As String = Space(Int16.MaxValue)
		Dim WinName As System.Text.StringBuilder
		Dim winDir As New StringBuilder(MAX_PATH)
		Dim extradir As String


		If IsWindowVisible(hwnd) Then
			GetWindowText(hwnd, Windwtext, Int16.MaxValue)
			GetSystemDirectory(winDir, MAX_PATH)

			WinName = New System.Text.StringBuilder(UCase(Windwtext))

			If InStr(WinName.ToString, "MYEXTRA") > 0 Then
				WinName.Replace("~1 - MYEXTRA! ENTERPRISE", " ")
				extradir = winDir.ToString
				
				UserInfoForm.cboSessions.Items.Add(WinName.ToString)
			End If
		End If
		Return True

	End Function




End Module


Is This A Good Question/Topic? 0
  • +

Replies To: Windows Application Path

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Windows Application Path

Posted 14 August 2009 - 12:22 PM

Are you trying to get the path of your running application or that of a different application?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8372
  • View blog
  • Posts: 31,116
  • Joined: 12-June 08

Re: Windows Application Path

Posted 14 August 2009 - 01:30 PM

I second Psychocoder.. what directory are you trying to get?

The application's execution path is

My.Application.Info.DirectoryPath


Was This Post Helpful? 0
  • +
  • -

#4 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Windows Application Path

Posted 14 August 2009 - 02:10 PM

I'm trying to get the path of an application that is running. Not my application.

So what the code I placed above does, is go through the running applications and populates a list of the window titles of all the running extra sessions.

After the user selects the session, they want to use, I need to activate that session to interface with my application. I need the application path of the selected extra session to do that. I figured that as I grab the Window title using the API call that would be the best time to grab the application path but I can't seem to figure out how to do that.
Was This Post Helpful? 0
  • +
  • -

#5 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Windows Application Path

Posted 17 August 2009 - 07:14 AM

Good Morning Everyone,

Does anyone have any ideas to share on what I'm trying to do? In the string above I provided the code and then clarified that I'm trying to get the path of an application that is running not my application that I'm creating. Does anyone know how to get an application path using API or that is even possible? Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 773
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Windows Application Path

Posted 17 August 2009 - 07:39 AM

This will show you the path to all running applications (selected from the process list). Add Imports System.Diagnostics to the class header before using this code.

		Dim procs As Process()
		Dim proc As Process
		Dim a As String = ""

		procs = Process.GetProcesses()

		For Each proc In procs
			If Not proc.MainWindowTitle Is "" Then
				a += proc.MainModule.FileName + Environment.NewLine
			End If
		Next

		MessageBox.Show(a)



Note, that the process should be open to external queries, otherwise you will get an Access Denied exception.
Was This Post Helpful? 0
  • +
  • -

#7 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Windows Application Path

Posted 17 August 2009 - 11:39 AM

Thank you for your help. This code returns the process path but that is now what I need.

For example it recognized that two instances of the applications I'm trying to list were running and it pulled the path as C:\Program Files\E!PC\EXTRA.exe for both.

However one is titled CA4 which is saved to the Desktop and the other is titled CA5 which is saved to My Documents folder.

Do you know how can I pull the actual path to these?
Was This Post Helpful? 0
  • +
  • -

#8 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 773
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Windows Application Path

Posted 17 August 2009 - 11:49 AM

Quote

However one is titled CA4 which is saved to the Desktop and the other is titled CA5 which is saved to My Documents folder.


Are those actual applications or just shortcuts to the executable?
Was This Post Helpful? 0
  • +
  • -

#9 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Windows Application Path

Posted 17 August 2009 - 12:20 PM

These are not short cuts.

Here is the properties info for the one on my desktop:

Type of file: Extra Session
Location: C:\Documents and Settings\abundml\Desktop

so as you can see it does not point to an executible in any other folder.

Is this what you mean?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1