CreateObject as VBScript does

#1
I was looking for an easier way to use COM Objects in TCC, something that would work similar to the VBScript CreateObject, such as follows;
Code:
createobject oHebrew System.Globalization.HebrewCalendar
pshell /s "$TheDate = Get-Date"
oHebrew GetYear($TheDate)
...which returns...
Code:
5778
Another example;
Code:
createobject oShell Shell.Application

oShell FileRun()
...which displays the Run dialog to the user. This method has the same effect as clicking the Start menu and selecting Run.

I came up with the following two .BTMs
Code:
:: CreateObject.btm
:: Parameter 1 - User Object Reference
:: Parameter 2 - COM Object name
::
:: Use the pshell command to create a COM object
:: Create a .btm based on the User Object Reference
:: Create an alias for the just created .btm, based on the User Object Reference
::
:: NOTE: If you are using TCC x64, you can only use x64 COM Objects
:: You CAN use a x32 COM Object on TCC x64, but it requires a bit more work
:: Ref: https://jpsoft.com/forums/threads/pshell-use-32-bit-from-64-bit.8469/
::
:: To see what objects have been created;
:: dir %temp\o*.btm
::
:: TODO: More error checking
::
@echo off
iff %_4ver lt 21 then
  echo %0 requires TCC Version 21 or later
  quit
endiff

pshell /s "$%1 = new-object -com %2"
::
:: Create a .btm called %1
:: Example: if %1 = oMath, create oMath.btm
::
text > %temp\%1.btm
@echo off
pshell /s "$%@name[%0].%$"
endtext

::
:: Create an alias for the just created .btm
::
alias %1=%temp\%1.btm
Code:
:: ReleaseObject.btm
:: Parameter 1 - User Object Reference
::
:: This .btm deletes;
:: - the %1.btm that was created with CreateObject.btm
:: - the %1 alias that was created with CreateObject.btm
::
@echo off
iff exist %temp\%1.btm then
  del /q %temp\%1.btm
else
  echo %1 Object Reference .btm not found.
endiff
iff isalias %1 then
  unalias %1
else
  echo %1 Object Reference alias not found.
endiff
:: Uncomment the following if you want to Close the persistent PowerShell interpreter
::pshell /c
To remove the objects;
Code:
ReleaseObject oHebrew
ReleaseObject oShell
These .BTMs makes it easier to work with the 64-bit .NET dlls that I create with C#, but are limited to only simple COM calls.

If anyone finds these routines useful, and if they make any changes/additions, please post your improvements.

Joe
 
#4
I have updated CreateObject.btm and ReleaseObject.btm;
Code:
:: CreateObject.btm
:: Parameter 1 - User Object Reference
:: Parameter 2 - COM Object name
::
:: Use the pshell command to create a COM object
:: Create an alias for the COM object, based on the User Object Reference
::
:: NOTE: If you are using TCC x64, you can only use x64 COM Objects
:: You CAN use a x32 COM Object on TCC x64, but it requires a bit more work
:: Ref: https://jpsoft.com/forums/threads/pshell-use-32-bit-from-64-bit.8469/
::
:: To see what objects have been created;
:: ListObjects
::
:: 2018/09/17
:: Added %1Null alias to supress output
::
:: TODO: More error checking
::
@echo off
iff %_4ver lt 21 then
  echo %0 requires TCC Version 21 or later
  quit
endiff

pshell /s "$%1 = new-object -com %2"

::
:: Create an alias for the object
::
:: Use this alias to supress output
alias %1Null=pshell /s "$%1.%%$ | out-null"
::
:: Use this alias to allow output
alias %1=pshell /s "$%1.%%$"
::
:: Create an alias to List Objects
::
alias ListObjects=`alias o* | ffind /kmvt"$o"`
::
:: Create an alias to List Object Members
::
alias Members=`pshell /s "$%1 | get-member"`
Code:
:: ReleaseObject.btm
:: Parameter 1 - User Object Reference
::
:: This .btm deletes;
:: - the %1 alias that was created with CreateObject.btm
::
@echo off
iff isalias %1 then
  unalias %1
else
  echo %1 Object Reference alias not found.
endiff

iff isalias Members then
  unalias Members
else
  echo Members alias not found.
endiff

iff isalias ListObjects then
  unalias ListObjects
else
  echo ListObjects alias not found.
endiff

:: Uncomment the following if you want to Close the persistent PowerShell interpreter
::pshell /c
I wanted the ability to supress/allow output. Now I have that ability with the new alias;
Code:
c:\users\jlc\utils>oString AppendLine("One")

Capacity MaxCapacity Length
-------- ----------- ------
      16  2147483647      5



c:\users\jlc\utils>oStringNull AppendLine("One")
Joe