|
| 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 | +} |
0 commit comments