Can i startup excel.exe and assign it a pid

I want to be able to identify the process at a later stage
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian GeeCommented:
(Since I'd like to know this as well,) I had to Google up windows +assign process id and the first two page results end up pointing to "no"...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
To identify a process later in Windows you start them with the WIN32-function CreateProcess. That function returns a structure of type PROCESS_INFORMATION where you can find both the process ID and a handle to the process. Later you can use these to both identify and control the process. Assigning a predetermined process ID, as far as I know, is not possible.

That was all for low level C/C++ programming and as the zone-selection of this question indicates you want something for VB. I'm not too familiar with VB but I undestand it includes some wrapper functionality that allows you to call those WIN32-functions, so that's a starting point for you.

You can't *assign* a PID to a process, but you can *identify* it.


Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("c:\Program Files\Microsoft Office 2007\Office12\EXCEL.EXE")
strPID = objExec.ProcessID
MsgBox strPID

Open in new window

itbabeAuthor Commented:
Thanks Rob,  I want to be able to identify the latest started instance of excel, how can I identify the pid of that one ?
OK, sure.  This code will show you the process ID for the latest instance of a process that has a name as defined by strProcessName


Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strProcessName = "iexplore.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT CreationDate,ProcessID FROM Win32_Process WHERE Name='" & strProcessName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

intProcID = 0
dteStarted = CDate("01-01-1601")

For Each objItem In colItems
	If CDate(WMIDateStringToDate(objItem.CreationDate)) > dteStarted Then
		dteStarted = CDate(WMIDateStringToDate(objItem.CreationDate))
		intProcID = objItem.ProcessId
	End If

If intProcID > 0 Then WScript.Echo "Process ID " & intProcID & " was started at " & dteStarted

Function WMIDateStringToDate(dtmDate)
	WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
	Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
	& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.