Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

GetCommandLine via PSHELL

Aug
1,929
71
I was having problems using the Kernel32.dll GetCommandLine function via @WINAPI, so I developed an alternate method using PowerShell.

Here is the .BTM;
Code:
@setlocal
@echo off
type <<- endtext > GetCommandLine.ps1
[Environment]::CurrentDirectory = $pwd

Add-Type -TypeDefinition @'
    using System;
    using System.Runtime.InteropServices;

    public static class Kernel32
    {
                [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
                public static extern System.IntPtr GetCommandLine();
    }
'@

endtext
pshell GetCommandLine.ps1
if exist GetCommandLine.ps1 del /q GetCommandLine.ps1
pshell /s "$theptr = [kernel32]::GetCommandLine()"
pshell /s "[Runtime.InteropServices.Marshal]::PtrToStringAuto($theptr)"
endlocal

As with my original solution, you can run this .BTM from the;
* TCC Command Line
* TCC Debugger
* CMDebug Debugger
...to see the different command lines returned.

For myself, I have added the following to my TCSTART.BTM;
Code:
type <<- endtext > GetCommandLine.ps1
[Environment]::CurrentDirectory = $pwd

Add-Type -TypeDefinition @'
    using System;
    using System.Runtime.InteropServices;

    public static class Kernel32
    {
                [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
                public static extern System.IntPtr GetCommandLine();
    }
'@
endtext
  pshell GetCommandLine.ps1
  if exist GetCommandLine.ps1 del /q GetCommandLine.ps1

Next, I have added the following to my function.lst that I call from TCSTART.BTM;
Code:
GetCommandLine=%@pshell[$theptr = [kernel32]::GetCommandLine(); [Runtime.InteropServices.Marshal]::PtrToStringAuto($theptr)]

Now, when I need the command line, I can (example);
Code:
e:\utils>echo %@getcommandline[]
"C:\Program Files\JPSoft\TCMD24\TCC.EXE"

...from TCC, BDEBUGGER, IDE, TCC-RT, etc.

Joe
 
Yes, I could simply have done;
Code:
e:\documents\powershell\getcommandline>pshell /s "[environment]::CommandLine"
"C:\Program Files\JPSoft\TCMD24\TCC.EXE"
...to return the same information, but a demonstration of calling a function from kernel32.dll via PSHELL was my goal.

Joe
 
Back
Top