Advertisement
djvj

CLR_L

Dec 19th, 2015
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. ; ==========================================================
  2. ; .NET Framework Interop
  3. ; http://www.autohotkey.com/forum/topic26191.html
  4. ; ==========================================================
  5. ;
  6. ; Author: Lexikos
  7. ; Version: 1.2
  8. ; Requires: AutoHotkey_L v1.0.96+
  9. ;
  10.  
  11. CLR_LoadLibrary(AssemblyName, AppDomain:=0)
  12. {
  13. if !AppDomain
  14. AppDomain := CLR_GetDefaultDomain()
  15. e := ComObjError(0)
  16. Loop 1 {
  17. if assembly := AppDomain.Load_2(AssemblyName)
  18. break
  19. static nulln := ComObject(13,0)
  20. args := ComObjArray(0xC, 1), args[0] := AssemblyName
  21. typeofAssembly := AppDomain.GetType().Assembly.GetType()
  22. if assembly := typeofAssembly.InvokeMember_3("LoadWithPartialName", 0x158, nulln, nulln, args)
  23. break
  24. if assembly := typeofAssembly.InvokeMember_3("LoadFrom", 0x158, nulln, nulln, args)
  25. break
  26. }
  27. ComObjError(e)
  28. return assembly
  29. }
  30.  
  31. CLR_CreateObject(Assembly, TypeName, Args*)
  32. {
  33. if !(argCount := Args.MaxIndex())
  34. return Assembly.CreateInstance_2(TypeName, true)
  35.  
  36. vargs := ComObjArray(0xC, argCount)
  37. Loop % argCount
  38. vargs[A_Index-1] := Args[A_Index]
  39.  
  40. static Array_Empty := ComObjArray(0xC,0), nulln := ComObject(13,0)
  41.  
  42. return Assembly.CreateInstance_3(TypeName, true, 0, nulln, vargs, nulln, Array_Empty)
  43. }
  44.  
  45. CLR_CompileCSharp(Code, References:="", AppDomain:=0, FileName:="", CompilerOptions:="")
  46. {
  47. return CLR_CompileAssembly(Code, References, "System", "Microsoft.CSharp.CSharpCodeProvider", AppDomain, FileName, CompilerOptions)
  48. }
  49.  
  50. CLR_CompileVB(Code, References:="", AppDomain:=0, FileName:="", CompilerOptions:="")
  51. {
  52. return CLR_CompileAssembly(Code, References, "System", "Microsoft.VisualBasic.VBCodeProvider", AppDomain, FileName, CompilerOptions)
  53. }
  54.  
  55. CLR_StartDomain(ByRef AppDomain, BaseDirectory:="")
  56. {
  57. static nulln := ComObject(13,0)
  58. args := ComObjArray(0xC, 5), args[0] := "", args[2] := BaseDirectory, args[4] := ComObject(0xB,false)
  59. AppDomain := CLR_GetDefaultDomain().GetType().InvokeMember_3("CreateDomain", 0x158, nulln, nulln, args)
  60. return A_LastError >= 0
  61. }
  62.  
  63. CLR_StopDomain(ByRef AppDomain)
  64. { ; ICorRuntimeHost::UnloadDomain
  65. DllCall("SetLastError", "uint", hr := DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+20*A_PtrSize), "ptr", RtHst, "ptr", ComObjValue(AppDomain))), AppDomain := ""
  66. return hr >= 0
  67. }
  68.  
  69. ; NOTE: IT IS NOT NECESSARY TO CALL THIS FUNCTION unless you need to load a specific version.
  70. CLR_Start(Version:="") ; returns ICorRuntimeHost*
  71. {
  72. static RtHst := 0
  73. ; The simple method gives no control over versioning, and seems to load .NET v2 even when v4 is present:
  74. ; return RtHst ? RtHst : (RtHst:=COM_CreateObject("CLRMetaData.CorRuntimeHost","{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"), DllCall(NumGet(NumGet(RtHst+0)+40),"uint",RtHst))
  75. if RtHst
  76. return RtHst
  77. EnvGet SystemRoot, SystemRoot
  78. if Version =
  79. Loop % SystemRoot "\Microsoft.NET\Framework" (A_PtrSize=8?"64":"") "\*", 2
  80. if (FileExist(A_LoopFileFullPath "\mscorlib.dll") && A_LoopFileName > Version)
  81. Version := A_LoopFileName
  82. if DllCall("mscoree\CorBindToRuntimeEx", "wstr", Version, "ptr", 0, "uint", 0
  83. , "ptr", CLR_GUID(CLSID_CorRuntimeHost, "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}")
  84. , "ptr", CLR_GUID(IID_ICorRuntimeHost, "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}")
  85. , "ptr*", RtHst) >= 0
  86. DllCall(NumGet(NumGet(RtHst+0)+10*A_PtrSize), "ptr", RtHst) ; Start
  87. return RtHst
  88. }
  89.  
  90. ;
  91. ; INTERNAL FUNCTIONS
  92. ;
  93.  
  94. CLR_GetDefaultDomain()
  95. {
  96. static defaultDomain := 0
  97. if !defaultDomain
  98. { ; ICorRuntimeHost::GetDefaultDomain
  99. if DllCall(NumGet(NumGet(0+RtHst:=CLR_Start())+13*A_PtrSize), "ptr", RtHst, "ptr*", p:=0) >= 0
  100. defaultDomain := ComObject(p), ObjRelease(p)
  101. }
  102. return defaultDomain
  103. }
  104.  
  105. CLR_CompileAssembly(Code, References, ProviderAssembly, ProviderType, AppDomain:=0, FileName:="", CompilerOptions:="")
  106. {
  107. if !AppDomain
  108. AppDomain := CLR_GetDefaultDomain()
  109.  
  110. if !(asmProvider := CLR_LoadLibrary(ProviderAssembly, AppDomain))
  111. || !(codeProvider := asmProvider.CreateInstance(ProviderType))
  112. || !(codeCompiler := codeProvider.CreateCompiler())
  113. return 0
  114.  
  115. if !(asmSystem := (ProviderAssembly="System") ? asmProvider : CLR_LoadLibrary("System", AppDomain))
  116. return 0
  117.  
  118. ; Convert | delimited list of references into an array.
  119. StringSplit, Refs, References, |, %A_Space%%A_Tab%
  120. aRefs := ComObjArray(8, Refs0)
  121. Loop % Refs0
  122. aRefs[A_Index-1] := Refs%A_Index%
  123.  
  124. ; Set parameters for compiler.
  125. prms := CLR_CreateObject(asmSystem, "System.CodeDom.Compiler.CompilerParameters", aRefs)
  126. , prms.OutputAssembly := FileName
  127. , prms.GenerateInMemory := FileName=""
  128. , prms.GenerateExecutable := SubStr(FileName,-3)=".exe"
  129. , prms.CompilerOptions := CompilerOptions
  130. , prms.IncludeDebugInformation := true
  131.  
  132. ; Compile!
  133. compilerRes := codeCompiler.CompileAssemblyFromSource(prms, Code)
  134.  
  135. if error_count := (errors := compilerRes.Errors).Count
  136. {
  137. error_text := ""
  138. Loop % error_count
  139. error_text .= ((e := errors.Item[A_Index-1]).IsWarning ? "Warning " : "Error ") . e.ErrorNumber " on line " e.Line ": " e.ErrorText "`n`n"
  140. MsgBox, 16, Compilation Failed, %error_text%
  141. return 0
  142. }
  143. ; Success. Return Assembly object or path.
  144. return compilerRes[FileName="" ? "CompiledAssembly" : "PathToAssembly"]
  145. }
  146.  
  147. CLR_GUID(ByRef GUID, sGUID)
  148. {
  149. VarSetCapacity(GUID, 16, 0)
  150. return DllCall("ole32\CLSIDFromString", "wstr", sGUID, "ptr", &GUID) >= 0 ? &GUID : ""
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement