-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added null check to WS_ReleaseObject() Fixed error handling in WS_Init() Added some additional remarks. Added remark warning about % in string blocks. Added MIT License and copyright Removed codef(), WS_ErrMsg(), and ScriptStr() functions, and as such the __iScriptControlObj__ variable. Renamed the 2 internal vars to be a little more obscure. Updated documentation index.html: Removed older example links. Updated change log. Added stuff about the memory leak ws4ahk.ahk: Fixed some docs Lots more doc updates. Improved WS_ReleaseObject() Fixed error message for ansi->unicode conversion. test_suite.ahk: Updated function names. COMmemLeak.ahk: Reproduce memory leak test_suite.ahk: Updated function names. lots of messy tests
- Loading branch information
Showing
7 changed files
with
712 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
Msgbox % "Beginning test: Look at current memory use" | ||
|
||
Loop, 9000 | ||
{ | ||
ScriptControlTest() | ||
Sleep, 100 | ||
} | ||
|
||
Msgbox % "End of test: Memory use shouldn't be much larger than at the start" | ||
Return | ||
|
||
ScriptControlTest() | ||
{ | ||
; Initialize COM | ||
iErr := DllCall("ole32\CoInitialize", "UInt", 0, "Int") | ||
|
||
If (iErr <> 0) | ||
ExitWithError("Failed to initialize COM") | ||
|
||
; Convert ProgramID and InterfaceID to unicode | ||
ProgId_ScriptControl := "MSScriptControl.ScriptControl" | ||
IID_ScriptControl := "{0E59F1D3-1FBE-11D0-8FF2-00A0D10038BC}" | ||
|
||
VarSetCapacity(ProgId_ScriptControl_utf16, 255, 0) ; arbitrary length of 255 | ||
VarSetCapacity(IID_ScriptControl_utf16, 255, 0) ; arbitrary length of 255 | ||
|
||
iErr := DllCall("MultiByteToWideChar" | ||
, "UInt", 0 ; from CP_ACP (ANSI) | ||
, "UInt", 0 ; no flags | ||
, "UInt" , &ProgId_ScriptControl | ||
, "Int" , -1 ; until NULL | ||
, "UInt" , &ProgId_ScriptControl_utf16 | ||
, "Int" , 128) | ||
|
||
If (iErr = 0) | ||
ExitWithError("Failed to convert ProgId_ScriptControl to unicode") | ||
|
||
iErr := DllCall("MultiByteToWideChar" | ||
, "UInt", 0 ; from CP_ACP (ANSI) | ||
, "UInt", 0 ; no flags | ||
, "UInt" , &IID_ScriptControl | ||
, "Int" , -1 ; until NULL | ||
, "UInt" , &IID_ScriptControl_utf16 | ||
, "Int" , 128) | ||
|
||
If (iErr = 0) | ||
ExitWithError("Failed to convert IID_ScriptControl to unicode") | ||
|
||
; Convert ProgramID (to ClassID) and InterfaceID to binary | ||
VarSetCapacity(Clsid_ScriptControl_bin, 16) ; 16 = sizeof(CLSID) | ||
VarSetCapacity(IID_ScriptControl_bin, 16) ; 16 = sizeof(IID) | ||
|
||
iErr := DllCall("ole32\CLSIDFromString" | ||
, "Str", ProgId_ScriptControl_utf16 | ||
, "Str", Clsid_ScriptControl_bin | ||
, "Int") | ||
|
||
If (iErr <> 0) | ||
ExitWithError("Failed to convert ProgId_ScriptControl to binary CLSID") | ||
|
||
iErr := DllCall("ole32\IIDFromString" | ||
, "Str", IID_ScriptControl_utf16 | ||
, "Str", IID_ScriptControl_bin | ||
, "Int") | ||
|
||
If (iErr <> 0) | ||
ExitWithError("Failed to convert IID_ScriptControl to binary IID") | ||
|
||
; Create the Script Control object | ||
CLSCTX_INPROC_SERVER := 1 | ||
CLSCTX_INPROC_HANDLER := 2 | ||
CLSCTX_LOCAL_SERVER := 4 | ||
CLSCTX_INPROC_SERVER16 := 8 | ||
CLSCTX_REMOTE_SERVER := 16 | ||
|
||
iErr := DllCall("ole32\CoCreateInstance" | ||
, "Str" , Clsid_ScriptControl_bin | ||
, "UInt" , 0 | ||
, "Int" , CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | ||
, "Str" , IID_ScriptControl_bin | ||
, "UInt*", ppvScriptControl | ||
, "Int") | ||
|
||
If (iErr <> 0) | ||
ExitWithError("Failed to create Script Control object") | ||
|
||
Language := "VBScript" | ||
|
||
; Convert 'VBScript' to unicode | ||
VarSetCapacity(Language_utf16, 255, 0) ; arbitrary length of 255 | ||
|
||
iErr := DllCall("MultiByteToWideChar" | ||
, "UInt", 0 ; from CP_ACP (ANSI) | ||
, "UInt", 0 ; no flags | ||
, "UInt" , &Language | ||
, "Int" , -1 ; until NULL | ||
, "UInt" , &Language_utf16 | ||
, "Int" , 128) | ||
|
||
If (iErr = 0) | ||
ExitWithError("Failed to convert 'VBScript' to unicode") | ||
|
||
; Convert 'VBScript' to BSTR | ||
Language_BSTR := DllCall("oleaut32\SysAllocString", "Str", Language_utf16, "UInt") | ||
|
||
If (Language_BSTR = 0) | ||
ExitWithError("Failed to create 'VBScript' BSTR") | ||
|
||
; Set the language to VBScript | ||
iErr := DllCall(NumGet(NumGet(ppvScriptControl+0) + 4*8), "UInt", ppvScriptControl | ||
, "UInt", Language_BSTR | ||
, "Int") | ||
|
||
If (iErr <> 0) | ||
ExitWithError("Failed to call MSScriptControl::Language put") | ||
|
||
; Free the BSTR | ||
DllCall("oleaut32\SysFreeString", "UInt", Language_BSTR) | ||
|
||
; Release Script Control object | ||
DllCall(NumGet(NumGet(ppvScriptControl+0) + 4*2), "UInt", ppvScriptControl, "Int") | ||
|
||
; Uninitialize COM | ||
DllCall("ole32\CoUninitialize") | ||
} | ||
|
||
ExitWithError(sErr) | ||
{ | ||
Msgbox % sErr | ||
ExitApp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
# ws4ahk | ||
|
||
ws4ahk v0.20 | ||
Include VBScript or JScript code directly in your Autohotkey program! | ||
No temporary files. Full and easy access to COM. | ||
|
||
- `WS_Exec()`/`WS_Eval()`: Better handling of errors. | ||
Removed printf() style functionality, moved to codef() function. | ||
Removed leftover Clipboard debug. | ||
- `codef()`: New function to handle `printf()` style formatting of code. | ||
Also fixes the bug if in hex mode. | ||
***Note: ws4ahk is made for Autohotkey Basic. It may work with Autohotkey_L, but it is not supported.*** | ||
|
||
Note that if you are using the printf style in `WS_Exec` or `WS_Eval` (I doubt anyone is), you will need to slightly modify your code to use the new `codef()` function. | ||
I've been thinking long and hard about using Microsoft Scripting Control to provide easy COM usage to AHK, and the more I have, the more it became the ultimate choice--far better than anything I could develop myself. | ||
|
||
Pros | ||
* Automatic objct management (objects are automatically deallocated--no memory leaks!) | ||
* Able to use either VBScript or JScript to write COM related code | ||
* ByRef argument handling is all taken care of | ||
* Almost all `VARIANT` handling is taken care of | ||
* Can very easily write compound COM statements (e.g. `objExcel.Workbooks.Add().Sheets(1).Cells(1,1).Value = 50`) | ||
* There's no need for an extra dll. It can be done entirely in AHK. | ||
* Much more easily implemented! | ||
|
||
The ONLY disadvantage with using Microsoft Scripting Control is that there may be some computers that do not have it installed (but probably not very many). HOWEVER, not only is it available to download from Microsoft and very easily installed, but thanks to the `WS_CreateObjectFromDll()` function, it doesn't even have to be installed! If a computer doesn't have Microsoft Scripting Control installed, you just need to supply the msscript.ocx file in the same folder as your script, and it will still work perfectly. No need to register the OCX. Your script remains completely portable! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.