Skip to content

Commit

Permalink
v0.21 release
Browse files Browse the repository at this point in the history
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
m35 committed Aug 4, 2008
1 parent bedd0fa commit 3eabfbc
Show file tree
Hide file tree
Showing 7 changed files with 712 additions and 320 deletions.
132 changes: 132 additions & 0 deletions COMmemLeak.ahk
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
}
22 changes: 15 additions & 7 deletions README.md
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!
8 changes: 4 additions & 4 deletions doc.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SET VER=0.20
SET VER=0.21

robodoc-4-99-36.exe --rc ws.rc --documenttitle "Windows Scripting for Autohotkey v%VER% Public API" --doc "Windows Scripting for Autohotkey v%VER% Public API"

Expand All @@ -10,6 +10,6 @@ copy "Windows Scripting for Autohotkey v%VER% Internal API.html" ws4ahk_internal

copy "Windows Scripting for Autohotkey v%VER% Public API.css" ws4ahk.css

@Echo Now edit these html files with the following:
@ECHO * Change styleguide link
@echo * Get rid of path in the title
@echo Now edit these html files with the following:
@echo * Change styleguide link
@echo * Get rid of path in the title
39 changes: 26 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Embedded Windows Scripting<br>(VBScript &amp; JScript)<br>and COM for Autoho
see the <a href="http://www.autohotkey.com/forum/topic21674.html">official thread</a>
in the Autohotkey forums.</p>

<h3>Latest version: v0.20 (beta)</h3>
<h3>Latest version: v0.21 (beta)</h3>
<p><a href="ws4ahk.ahk">ws4ahk.ahk</a></p>

<p><a href="ws4ahk_public_api.html">Public API Documentation</a></p>
Expand All @@ -39,10 +39,20 @@ <h3>Useful links:</h3>
<hr>
<h3>FAQ</h3>
How is this different from Sean's scripts?<br>
Hopefully answered in <a href="http://www.autohotkey.com/forum/viewtopic.php?p=138989#138989">this thread</a>.
- Hopefully answered in <a href="http://www.autohotkey.com/forum/viewtopic.php?p=138989#138989">this thread</a>.
<hr>
<h3>Known bugs:</h3>
<p>[<a href="http://www.autohotkey.com/forum/viewtopic.php?p=141847#141847">Reported by YMP</a>]
<h4>Memory leak</h4>
<p>A memory leak is occuring during the initialization of COM, the creation of the MSScriptControl,
the setting of the scripting language, then the deletion of the MSScriptControl and uninitialization of COM.
When this process is done repeatedly over 9000 times, the AutoHotKey process will be left using an extra
megabyte of memory than before. I have put this whole process into a <a href="COMmemLeak.ahk">single script</a>
to test it. I am unable to determine if the problem lies in my script, Autohotkey, or
the MSScriptControl itself.</p>
<p>The workaround is, of course, to minimize the number of times you call WS_Initialize() and WS_Uninitialize()
in your scripts.</p>
<h4>Popup timeout</h4>
<p>[<a href="http://www.autohotkey.com/forum/viewtopic.php?p=141847#141847">Reported by YMP</a>]<br>
The pop-up message in the following code should timeout after 2 seconds, but it does not.</p>
<pre>
Code=
Expand Down Expand Up @@ -73,13 +83,22 @@ <h3>Known bugs:</h3>
<hr>
<h3>Change history:</h3>
<pre>
v0.20
v0.21 (3 July 2008)
- Numerous documentation improvements.
- Fixed error handling in WS_Initialize().
- WS_ReleaseObject(): added check to catch 0 or "" argument.
- Removed extraneous codef(), WS_ErrMsg(), and ScriptStr() functions.
- Renamed internal global variables to be more unique.
- Added MIT License as a formality.

v0.20 (24 Jan 2008)
- WS_Exec()/WS_Eval(): Better handling of errors.
Removed printf() style functionality, moved to codef() function.
Removed debug Clipboard change.
Removed leftover Clipboard debug.
- codef(): New function to handle printf() style formatting of code.
Also fixes the bug if in hex mode.

v0.13
v0.13 (32 Dec 2007)
- Added missing error handling.

v0.12 (9 Dec 2007)
Expand Down Expand Up @@ -129,6 +148,7 @@ <h3>Change history:</h3>

<h3>Older versions:</h3>
<p>
<a href="ws4ahk0-20.ahk">v0.20</a><br>
<a href="ws4ahk0-13.ahk">v0.13</a><br>
<a href="ws4ahk0-12.ahk">v0.12</a><br>
<a href="ws4ahk0-11.ahk">v0.11</a><br>
Expand All @@ -139,13 +159,6 @@ <h3>Older versions:</h3>
<a href="ws4ahk0-01.ahk">v0.01</a>
</p>

<hr>

<h3>Old Demo scripts</h3>
<p>26 Jul 2007: New updated <a href="WS_DEControl2.ahk">WS_DEControl2.ahk</a> and <a href="WS_DEDemo2.ahk">WS_DEDemo2.ahk</a> to not require passing the scripting object name in every function. Again based on ABCyourway's code. (NOTE: These require v0.03 of the ws4ahk)</p>


<p><a href="WS_DEControl.ahk">WS_DEControl.ahk</a> and <a href="WS_DEdemo.ahk">WS_DEdemo.ahk</a>, originally written by ABCyourway for EasyCOM. (NOTE: These require v0.01 of the ws4ahk)</p>
<hr>
<p>This page origionally generated using ABCyourway's DHTML Edit control :)</p>
</body>
Expand Down
Loading

0 comments on commit 3eabfbc

Please sign in to comment.