I want to get my PC session ID in VBA

Asked 2 years ago, Updated 2 years ago, 399 views

I want Excel VBA to be able to code for the session number that I can get from the command prompt in query session. How can I do that?

I looked at another site and tried to retrieve it using Shell, but the returned value was empty.

It's just a test code, so don't worry about why i is a string variable.

Thank you for your cooperation.

Below is the code.

Dimwsh As Object

Setwsh=CreateObject("WScript.Shell")

US>'Variables that store command results
Dim result As Object
Set result=CreateObject("WScript.Shell")
 
Dim cmd As String
Dim filedata() As String
Dimi As String
 
US>' Command you want to execute
cmd = "query session"
 
US>' Execute command
Set result=wsh.exec("%ComSpec%/c"&cmd)
US>'Wait for command execution to finish
Do While result.Status = 0
    DoEvents
Loop

US>'Store results in an array separated by new lines
filedata=Split(result.StdOut.ReadAll, vbCrLf)
 
'Write the results from A1 in order
"i="
Dimfilenm As Variant
For Each filename Infiledata
    i=i&filenm&vbNewLine
Next
 
Set result=Nothing
Set wsh=Nothing

MsgBoxi

Enter a description of the image here

windows vba session

2022-09-30 21:58

2 Answers

query.exe has a 64-bit version of C:\Windows\System32\query.exe but no 32-bit version of C:\Windows\SysWOW64\query.exe.
Then, when a 32-bit process attempts to browse to C:\Windows\System32, the File System Redirector redirects it to C:\Windows\SysWOW64.

Therefore, if you attempt to launch C:\Windows\System32\cmd.exe/CC:\Windows\System32\query.exe as an external process from 32-bit Excel (redirected to C:\Windows\SysWOW64\cmd.exe/CC:\Windows\System32\query.exe.The 32-bit version of cmd.exe tries to run C:\Windows\System32\query.exe, but is redirected.) When you try to run C:\Windows\SysWOW64\query.exe, you get C:\Windows\SysWOW64\query.exe.

The solution is to use C:\Windows\Sysnative as described on this page (for Windows Vista and later).C:\Windows\Sysnative allows you to view C:\Windows\System32 even for 32-bit processes.

Please specify C:\Windows\Sysnative\query.exe as the executable for this time.


2022-09-30 21:58

The same phenomenon occurs in this environment, but now you can get the results of the query session after changing the code as follows:

[Before the change]

'Command you want to execute
cmd = "query session"

[After the change]

'Command you want to execute
cmd = "C:\Windows\WinSxS\amd64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_10.0.19041.1_none_9aa166e99861c2bc\query.exe session"

The query.exe path may vary depending on the environment.If the problem persists, search query.exe and try the path you found.

[Results]
Run Results

[Environment used for confirmation]

 Edition Windows 10 Pro
Version 20H2
OS Build 19042.1237
Experience Windows Feature Experience Pack 120.2212.3530.0

I thought query.exe was not the full pathname, so I searched query.exe.
C:\Windows\System32\query.exe was found, so we ran it and it didn't change.セッション Unable to retrieve session information
C:\Windows\WinSxS\amd64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_10.0.19041.1_none_9aa166e99861c2bc\query.exe was also found, and the problem was resolved.

The size and timestamp of these two query.exe were the same.
I don't know why these two query.exe behave differently, but if it's just query, C:\Windows\System32\query.exe will be launched, and I don't think I can get session information.

The query.exe and runtime behavior found are as follows:

 C:\Windows\System32\query.exe session
  • Runable from cmd
  • No value returned from VBA
 C:\Windows\WinSxS\amd64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_10.0.19041.1_none_9aa166e99861c2bc\query.exe session
  • Runable from cmd
  • Returns value from VBA
 C:\Windows\WinSxS\wow64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_10.0.19041.1_none_a4f6113bccc284b7\query.exe session
  • Cannot run from cmd
  • The following dialogs and messages appear:

Dialog Image

This version of C:\Windows\WinSxS\wow64_microsoft-windows-t..es-commandlinetools_31bf3856ad364e35_10.0.19041.1_none_a4f6113bccc284b7\query.exe is not compatible with the version of Windows running.Check your computer's system information before contacting the software publisher.


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.