Skip to content

Commit 3eabfbc

Browse files
committed
v0.21 release
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
1 parent bedd0fa commit 3eabfbc

File tree

7 files changed

+712
-320
lines changed

7 files changed

+712
-320
lines changed

COMmemLeak.ahk

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
Msgbox % "Beginning test: Look at current memory use"
3+
4+
Loop, 9000
5+
{
6+
ScriptControlTest()
7+
Sleep, 100
8+
}
9+
10+
Msgbox % "End of test: Memory use shouldn't be much larger than at the start"
11+
Return
12+
13+
ScriptControlTest()
14+
{
15+
; Initialize COM
16+
iErr := DllCall("ole32\CoInitialize", "UInt", 0, "Int")
17+
18+
If (iErr <> 0)
19+
ExitWithError("Failed to initialize COM")
20+
21+
; Convert ProgramID and InterfaceID to unicode
22+
ProgId_ScriptControl := "MSScriptControl.ScriptControl"
23+
IID_ScriptControl := "{0E59F1D3-1FBE-11D0-8FF2-00A0D10038BC}"
24+
25+
VarSetCapacity(ProgId_ScriptControl_utf16, 255, 0) ; arbitrary length of 255
26+
VarSetCapacity(IID_ScriptControl_utf16, 255, 0) ; arbitrary length of 255
27+
28+
iErr := DllCall("MultiByteToWideChar"
29+
, "UInt", 0 ; from CP_ACP (ANSI)
30+
, "UInt", 0 ; no flags
31+
, "UInt" , &ProgId_ScriptControl
32+
, "Int" , -1 ; until NULL
33+
, "UInt" , &ProgId_ScriptControl_utf16
34+
, "Int" , 128)
35+
36+
If (iErr = 0)
37+
ExitWithError("Failed to convert ProgId_ScriptControl to unicode")
38+
39+
iErr := DllCall("MultiByteToWideChar"
40+
, "UInt", 0 ; from CP_ACP (ANSI)
41+
, "UInt", 0 ; no flags
42+
, "UInt" , &IID_ScriptControl
43+
, "Int" , -1 ; until NULL
44+
, "UInt" , &IID_ScriptControl_utf16
45+
, "Int" , 128)
46+
47+
If (iErr = 0)
48+
ExitWithError("Failed to convert IID_ScriptControl to unicode")
49+
50+
; Convert ProgramID (to ClassID) and InterfaceID to binary
51+
VarSetCapacity(Clsid_ScriptControl_bin, 16) ; 16 = sizeof(CLSID)
52+
VarSetCapacity(IID_ScriptControl_bin, 16) ; 16 = sizeof(IID)
53+
54+
iErr := DllCall("ole32\CLSIDFromString"
55+
, "Str", ProgId_ScriptControl_utf16
56+
, "Str", Clsid_ScriptControl_bin
57+
, "Int")
58+
59+
If (iErr <> 0)
60+
ExitWithError("Failed to convert ProgId_ScriptControl to binary CLSID")
61+
62+
iErr := DllCall("ole32\IIDFromString"
63+
, "Str", IID_ScriptControl_utf16
64+
, "Str", IID_ScriptControl_bin
65+
, "Int")
66+
67+
If (iErr <> 0)
68+
ExitWithError("Failed to convert IID_ScriptControl to binary IID")
69+
70+
; Create the Script Control object
71+
CLSCTX_INPROC_SERVER := 1
72+
CLSCTX_INPROC_HANDLER := 2
73+
CLSCTX_LOCAL_SERVER := 4
74+
CLSCTX_INPROC_SERVER16 := 8
75+
CLSCTX_REMOTE_SERVER := 16
76+
77+
iErr := DllCall("ole32\CoCreateInstance"
78+
, "Str" , Clsid_ScriptControl_bin
79+
, "UInt" , 0
80+
, "Int" , CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER
81+
, "Str" , IID_ScriptControl_bin
82+
, "UInt*", ppvScriptControl
83+
, "Int")
84+
85+
If (iErr <> 0)
86+
ExitWithError("Failed to create Script Control object")
87+
88+
Language := "VBScript"
89+
90+
; Convert 'VBScript' to unicode
91+
VarSetCapacity(Language_utf16, 255, 0) ; arbitrary length of 255
92+
93+
iErr := DllCall("MultiByteToWideChar"
94+
, "UInt", 0 ; from CP_ACP (ANSI)
95+
, "UInt", 0 ; no flags
96+
, "UInt" , &Language
97+
, "Int" , -1 ; until NULL
98+
, "UInt" , &Language_utf16
99+
, "Int" , 128)
100+
101+
If (iErr = 0)
102+
ExitWithError("Failed to convert 'VBScript' to unicode")
103+
104+
; Convert 'VBScript' to BSTR
105+
Language_BSTR := DllCall("oleaut32\SysAllocString", "Str", Language_utf16, "UInt")
106+
107+
If (Language_BSTR = 0)
108+
ExitWithError("Failed to create 'VBScript' BSTR")
109+
110+
; Set the language to VBScript
111+
iErr := DllCall(NumGet(NumGet(ppvScriptControl+0) + 4*8), "UInt", ppvScriptControl
112+
, "UInt", Language_BSTR
113+
, "Int")
114+
115+
If (iErr <> 0)
116+
ExitWithError("Failed to call MSScriptControl::Language put")
117+
118+
; Free the BSTR
119+
DllCall("oleaut32\SysFreeString", "UInt", Language_BSTR)
120+
121+
; Release Script Control object
122+
DllCall(NumGet(NumGet(ppvScriptControl+0) + 4*2), "UInt", ppvScriptControl, "Int")
123+
124+
; Uninitialize COM
125+
DllCall("ole32\CoUninitialize")
126+
}
127+
128+
ExitWithError(sErr)
129+
{
130+
Msgbox % sErr
131+
ExitApp
132+
}

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# ws4ahk
22

3-
ws4ahk v0.20
3+
Include VBScript or JScript code directly in your Autohotkey program!
4+
No temporary files. Full and easy access to COM.
45

5-
- `WS_Exec()`/`WS_Eval()`: Better handling of errors.
6-
Removed printf() style functionality, moved to codef() function.
7-
Removed leftover Clipboard debug.
8-
- `codef()`: New function to handle `printf()` style formatting of code.
9-
Also fixes the bug if in hex mode.
6+
***Note: ws4ahk is made for Autohotkey Basic. It may work with Autohotkey_L, but it is not supported.***
107

11-
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.
8+
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.
9+
10+
Pros
11+
* Automatic objct management (objects are automatically deallocated--no memory leaks!)
12+
* Able to use either VBScript or JScript to write COM related code
13+
* ByRef argument handling is all taken care of
14+
* Almost all `VARIANT` handling is taken care of
15+
* Can very easily write compound COM statements (e.g. `objExcel.Workbooks.Add().Sheets(1).Cells(1,1).Value = 50`)
16+
* There's no need for an extra dll. It can be done entirely in AHK.
17+
* Much more easily implemented!
18+
19+
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!

doc.bat

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SET VER=0.20
1+
SET VER=0.21
22

33
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"
44

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

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

13-
@Echo Now edit these html files with the following:
14-
@ECHO * Change styleguide link
15-
@echo * Get rid of path in the title
13+
@echo Now edit these html files with the following:
14+
@echo * Change styleguide link
15+
@echo * Get rid of path in the title

index.html

+26-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h1>Embedded Windows Scripting<br>(VBScript &amp; JScript)<br>and COM for Autoho
1212
see the <a href="http://www.autohotkey.com/forum/topic21674.html">official thread</a>
1313
in the Autohotkey forums.</p>
1414

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

1818
<p><a href="ws4ahk_public_api.html">Public API Documentation</a></p>
@@ -39,10 +39,20 @@ <h3>Useful links:</h3>
3939
<hr>
4040
<h3>FAQ</h3>
4141
How is this different from Sean's scripts?<br>
42-
Hopefully answered in <a href="http://www.autohotkey.com/forum/viewtopic.php?p=138989#138989">this thread</a>.
42+
- Hopefully answered in <a href="http://www.autohotkey.com/forum/viewtopic.php?p=138989#138989">this thread</a>.
4343
<hr>
4444
<h3>Known bugs:</h3>
45-
<p>[<a href="http://www.autohotkey.com/forum/viewtopic.php?p=141847#141847">Reported by YMP</a>]
45+
<h4>Memory leak</h4>
46+
<p>A memory leak is occuring during the initialization of COM, the creation of the MSScriptControl,
47+
the setting of the scripting language, then the deletion of the MSScriptControl and uninitialization of COM.
48+
When this process is done repeatedly over 9000 times, the AutoHotKey process will be left using an extra
49+
megabyte of memory than before. I have put this whole process into a <a href="COMmemLeak.ahk">single script</a>
50+
to test it. I am unable to determine if the problem lies in my script, Autohotkey, or
51+
the MSScriptControl itself.</p>
52+
<p>The workaround is, of course, to minimize the number of times you call WS_Initialize() and WS_Uninitialize()
53+
in your scripts.</p>
54+
<h4>Popup timeout</h4>
55+
<p>[<a href="http://www.autohotkey.com/forum/viewtopic.php?p=141847#141847">Reported by YMP</a>]<br>
4656
The pop-up message in the following code should timeout after 2 seconds, but it does not.</p>
4757
<pre>
4858
Code=
@@ -73,13 +83,22 @@ <h3>Known bugs:</h3>
7383
<hr>
7484
<h3>Change history:</h3>
7585
<pre>
76-
v0.20
86+
v0.21 (3 July 2008)
87+
- Numerous documentation improvements.
88+
- Fixed error handling in WS_Initialize().
89+
- WS_ReleaseObject(): added check to catch 0 or "" argument.
90+
- Removed extraneous codef(), WS_ErrMsg(), and ScriptStr() functions.
91+
- Renamed internal global variables to be more unique.
92+
- Added MIT License as a formality.
93+
94+
v0.20 (24 Jan 2008)
7795
- WS_Exec()/WS_Eval(): Better handling of errors.
7896
Removed printf() style functionality, moved to codef() function.
79-
Removed debug Clipboard change.
97+
Removed leftover Clipboard debug.
8098
- codef(): New function to handle printf() style formatting of code.
99+
Also fixes the bug if in hex mode.
81100

82-
v0.13
101+
v0.13 (32 Dec 2007)
83102
- Added missing error handling.
84103

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

130149
<h3>Older versions:</h3>
131150
<p>
151+
<a href="ws4ahk0-20.ahk">v0.20</a><br>
132152
<a href="ws4ahk0-13.ahk">v0.13</a><br>
133153
<a href="ws4ahk0-12.ahk">v0.12</a><br>
134154
<a href="ws4ahk0-11.ahk">v0.11</a><br>
@@ -139,13 +159,6 @@ <h3>Older versions:</h3>
139159
<a href="ws4ahk0-01.ahk">v0.01</a>
140160
</p>
141161

142-
<hr>
143-
144-
<h3>Old Demo scripts</h3>
145-
<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>
146-
147-
148-
<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>
149162
<hr>
150163
<p>This page origionally generated using ABCyourway's DHTML Edit control :)</p>
151164
</body>

0 commit comments

Comments
 (0)