Jump to content

Using .NET libary with AutoIt, possible?


Recommended Posts

Hello. I have not time to look into for now. But When I get some free time sure I'll do something.

 

Saludos

Link to comment
Share on other sites

Hello. I have not time to look into for now. But When I get some free time sure I'll do something.

EDIT: Please some mod delete one of my message :-S

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Link to comment
Share on other sites

I have been reading and experimenting a bit too ...

DSH

I found back an other old interesting implementation of DSH (DotNet Scripting Host).

I downloaded more then 10 years ago, and like the concept a lot (see documentation)

http://www.windows-scripting.de/default2.aspx?start=http://www.windows-scripting.de/scripting/dotnetscripting/dsh.en.asp

But unfortunately it is not developed anymore and supports only .Net 1.0 and 1.1.  

Best forget this one, only good to read for conceptional implementation interests.

CLR Host in Powershell

This example shows how do I instantiate CorRuntimeHost from mscoree.tlb in PowerShell?

http://stackoverflow.com/questions/16513608/how-do-i-instantiate-corruntimehost-from-mscoree-tlb-in-powershell

C# => PS1 https://gist.github.com/zippy1981/a838ff74196acce66d7e#file-program-cs  This is the working Example =>  EnumAppDomains.ps1

This example shows how to run inline C# or VB code. it does in memory compilation.

wwDotNetBridge

One of the interesting things to compare too is this : wwDotNetBridge Assembly for FoxPro (Thanks to junkew

https://github.com/RickStrahl/wwDotnetBridge

Which has a nice description of how they implemented this.

https://weblog.west-wind.com/posts/2007/Jul/01/Hosting-the-NET-Runtime-in-Visual-FoxPro

There is a difference in accessing a custom build Assembly and a Native .Net Runtime method :

https://west-wind.com/presentations/wwDotnetBridge/wwDotnetBridge.pdf 

"Access to Static Methods and Properties A lot of useful native.NET Runtime functionality is contained in static methods ... Native COM Interop doesn’t have a way to access anything static. "

 

; https://github.com/RickStrahl/wwDotnetBridge

#AutoIt3Wrapper_UseX64=y

; Initialize error handler
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$oDotNetBridge = ObjCreate("Westwind.wwDotNetBridge")


If IsObj($oDotNetBridge) then
    ConsoleWrite("Is Object Found " & IsObj($oDotNetBridge)& @CRLF)
Else
    ConsoleWrite("Can't find the object : did you Regasm ? " & @CRLF)
EndIf

; Does not work ?
; $oDotNetBridge.LoadAssembly("C:\_\Apps\AutoIT3\_DotNET\C#Library.dll")

; *** Display Event Log Entries
$loEventLog = $oDotNetBridge.CreateInstance(("System.Diagnostics.EventLog"))

ConsoleWrite("Instance is Object Found " & IsObj($loEventLog) & @CRLF)

 $loEventLog.Source = "Outlook"
 $loEventLog.Log = "Application"

; *** Turn Eventlog Entries into a ComArray Class
; *** Indirect access automatically turns .NET array into ComArray
$loEvents = $oDotNetBridge.GetProperty($loEventLog,"Entries")

ConsoleWrite("Number of Events Found : " & $loEvents.Count & @CRLF)

For $i = 0 to $loEvents.Count -1

    If $loEvents.item($i).source = "Outlook" Then

        ConsoleWrite($loEvents.item($i).source & @CRLF & _
            "--------" & @CRLF & _
            $loEvents.item($i).message & @CRLF)

    EndIf
Next

; This is my custom error handler
Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
  SetError(1)  ; to check for after this function returns
Endfunc

.Net Reflection

What is reflection in .Net  : https://www.codeproject.com/articles/55710/reflection-in-net

I hope this can help us all, to make us understand a bit more how .NET Interop works.

Enjoy

Edited by ptrex
Link to comment
Share on other sites

Link to comment
Share on other sites

Global $tIID__AppDomain = _AutoItObject_CLSIDFromString("{05F696DC-2B29-3663-AD8B-C4389CF2A713}")
Global $tagIUnknown = _
    "QueryInterface long(" & $refGUID & ";ptr);" & _
    "AddRef uint();" & _
    "Release uint();"
Global $tag_AppDomain = $tagIUnknown & _
    "GetTypeInfoCount long(uint*);" & _
    "GetTypeInfo long(uint;uint;int64);" & _
    "GetIDsOfNames long(" & $refObject & ";int64;uint;uint;int64);" & _
    "Invoke long(uint;" & $refObject & ";uint;short;int64;int64;int64;int64);" & _
    "get_ToString long(bstr*);" & _
    "Equals long(" & $URESOLVED_TYPE_12 & ";short*);" & _
    "GetHashCode long(int*);" & _
    "GetType long(ptr);" & _
    "InitializeLifetimeService long(" & $URESOLVED_TYPE_12 & "*);" & _
    "GetLifetimeService long(" & $URESOLVED_TYPE_12 & "*);" & _
    "get_Evidence long(ptr);" & _
    "add_DomainUnload long(" & $refObject & ");" & _
    "remove_DomainUnload long(" & $refObject & ");" & _
    "add_AssemblyLoad long(" & $refObject & ");" & _
    "remove_AssemblyLoad long(" & $refObject & ");" & _
    "add_ProcessExit long(" & $refObject & ");" & _
    "remove_ProcessExit long(" & $refObject & ");" & _
    "add_TypeResolve long(" & $refObject & ");" & _
    "remove_TypeResolve long(" & $refObject & ");" & _
    "add_ResourceResolve long(" & $refObject & ");" & _
    "remove_ResourceResolve long(" & $refObject & ");" & _
    "add_AssemblyResolve long(" & $refObject & ");" & _
    "remove_AssemblyResolve long(" & $refObject & ");" & _
    "add_UnhandledException long(" & $refObject & ");" & _
    "remove_UnhandledException long(" & $refObject & ");" & _
    "DefineDynamicAssembly long(" & $refObject & ";int;ptr);" & _
    "DefineDynamicAssembly_2 long(" & $refObject & ";" & $refObject & ";bstr;ptr);" & _
    "DefineDynamicAssembly_3 long(" & $refObject & ";" & $refObject & ";" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_4 long(" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_5 long(" & $refObject & ";" & $refObject & ";bstr;" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_6 long(" & $refObject & ";" & $refObject & ";bstr;" & $refObject & ";" & $refObject & ";" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_7 long(" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_8 long(" & $refObject & ";" & $refObject & ";bstr;" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";ptr);" & _
    "DefineDynamicAssembly_9 long(" & $refObject & ";" & $refObject & ";bstr;" & $refObject & ";" & $refObject & ";" & $refObject & ";" & $refObject & ";short;ptr);" & _
    "CreateInstance long(bstr;bstr;ptr);" & _
    "CreateInstanceFrom long(bstr;bstr;ptr);" & _
    "CreateInstance_2 long(bstr;bstr;ptr;ptr);" & _
    "CreateInstanceFrom_2 long(bstr;bstr;ptr;ptr);" & _
    "CreateInstance_3 long(bstr;bstr;short;" & $refObject & ";" & $refObject & ";ptr;" & $refObject & ";ptr;" & $refObject & ";ptr);" & _
    "CreateInstanceFrom_3 long(bstr;bstr;short;" & $refObject & ";" & $refObject & ";ptr;" & $refObject & ";ptr;" & $refObject & ";ptr);" & _
    "Load long(" & $ref_AssemblyName & ";ptr);" & _
    "Load_2 long(bstr;ptr);" & _
    "Load_3 long(ptr;ptr);" & _
    "Load_4 long(ptr;ptr;ptr);" & _
    "Load_5 long(ptr;ptr;" & $refObject & ";ptr);" & _
    "Load_6 long(" & $refObject & ";" & $refObject & ";ptr);" & _
    "Load_7 long(bstr;" & $ref_Evidence & ";ptr);" & _
    "ExecuteAssembly long(bstr;" & $ref_Evidence & ";int*);" & _
    "ExecuteAssembly_2 long(bstr;int*);" & _
    "ExecuteAssembly_3 long(bstr;" & $refObject & ";ptr;int*);" & _
    "get_FriendlyName long(bstr*);" & _
    "get_BaseDirectory long(bstr*);" & _
    "get_RelativeSearchPath long(bstr*);" & _
    "get_ShadowCopyFiles long(short*);" & _
    "GetAssemblies long(ptr);" & _
    "AppendPrivatePath long(bstr);" & _
    "ClearPrivatePath long();" & _
    "SetShadowCopyPath long(bstr);" & _
    "ClearShadowCopyPath long();" & _
    "SetCachePath long(bstr);" & _
    "SetData long(bstr;" & $URESOLVED_TYPE_12 & ");" & _
    "GetData long(bstr;" & $URESOLVED_TYPE_12 & "*);" & _
    "SetAppDomainPolicy long(" & $refObject & ");" & _
    "SetThreadPrincipal long(" & $refObject & ");" & _
    "SetPrincipalPolicy long(" & $refObject & ");" & _
    "DoCallBack long(" & $refObject & ");" & _
    "get_DynamicDirectory long(bstr*);"

; -------------------------------
Local $pAppDomain
; Initialize interface pointer $pAppDomain here, for example:
; _AutoItObject_CoCreateInstance(DllStructGetPtr($tCLSID_CAppDomain), 0, 1, DllStructGetPtr($tIID__AppDomain), $pAppDomain)
Local $objAppDomain = _AutoItObject_WrapperCreate($pAppDomain, $tag_AppDomain)
12     Variant (only used with arrays of variants)
Edited by junkew
Link to comment
Share on other sites

@junkew I think you need call CorRuntimeHost->GetDefaultDomain to create the instance of  _appdomain

 

Saludos

Link to comment
Share on other sites

I am looking for .NET 4 

CLRRuntimeHost->......

CorRuntimeHost->GetDefaultDomain

with the CLRRuntimeHost it looks like this I hope 
 

#REGION domain
        Local $pdwAppDomainId = DllStructCreate("DWORD")
        $oRuntimeHost.GetCurrentAppDomainId(DllStructGetPtr($pdwAppDomainId))
        ConsoleWrite(">pointer: " & DllStructGetPtr($pdwAppDomainId , 1) & @CRLF)
        $pDomain=DllStructGetPtr($pdwAppDomainId , 1)
        Local $oDomain = ObjCreateInterface($pDomain, ..., ...)
        ConsoleWrite(">oRuntimeDomain" & IsObj($oDomain)  & @CRLF)
#EndRegion

 

Edited by junkew
https://github.com/dotnet/coreclr/issues/8525
Link to comment
Share on other sites

RuntimeHost.GetCurrentAppDomainId will not return an Interface pointer...

I still dont understand what you're trying to do. If you find a working C++/C example that reproduce what you're trying to do let me know it.

 

Saludos

Link to comment
Share on other sites

1. Trying to get post 39 to work (2 vba examples) which comes down to create .NET objects within AutoIt to work based on the CLRRuntimeHost

2. and later preferably from AutoIt as host do dynamic stuff. http://www.codemag.com/Article/0211081
 

but as soon as 2 is done i am not sure why we should do it in AutoIt as you just as wel then can switch to C# as standard programming language

 

Edited by junkew
Link to comment
Share on other sites

Agree, indeed the point 2 example you added shows the whole picture for compiling inline .NET Code ...

The article is a good find.

But I am not sure if you can access the native .Net Classes as well using this approach, like is demonstrated in the wwDotNetBridge example.

 

Edited by ptrex
Link to comment
Share on other sites

There is no reason to assume its not possible like DannyFirex working example already proves you can get .NET custom class working

Getting to the domain should be possible thru the ICorRunteimeHost and from there you can call all functions from 

$tag_AppDomain

as DannyFirex suggested CorRuntimeHost->GetDefaultDomain to create the instance of  _appdomain

and nicely ICorRuntimeHost is not deprecacted in 4.x so seems to stay a valid interface to use

//- Load the CLR into the current process and return a runtime
    //- interface pointer. ICorRuntimeHost and ICLRRuntimeHost are
    //- the two CLR hosting interfaces supported by CLR 4.0. Here
    //- we demo the ICorRuntimeHost interface that was provided in
    //- .NET v1.x, and is compatible with all .NET Frameworks.

 

ad.CreateInstance("mscorlib", "System.Collections.Stack").Unwrap

 

 

just not enough time to experiment with appdomain createinstance as that basically can create your system.collections.stack object as part of the standard.net classes

Link to comment
Share on other sites

Hi junkew,

Yes you are correct the interface is not yet depreciated and will give what is needed ...

But as I was reading there are 2 ways of accessing the . net classes is to use the "Reflection" methods as I posted earlier (see post 46)

This shows a nice example showing 2 ways of accessing .Net classes. 1 using the Appdomain and the other using the Reflection method (with slightly different outcome.)

http://stackoverflow.com/questions/18727692/what-is-the-difference-between-these-ways-of-getting-current-directory

Quote

Here are the results

AppDomain.CurrentDomain.BaseDirectory


D:\

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)


F:\

Environment.CurrentDirectory and Directory.GetCurrentDirectory()


C:\

I know that Powershell ISE is using the Reflection approach.

Just wanted to highlight that there multiple ways of getting the job done...

But forgive me I can't tell which approach will last longest and is easier to implement. I have to do more reading about this....

http://www.brad-smith.info/blog/archives/500

https://msdn.microsoft.com/en-us/library/dd153782(v=vs.110).aspx

After a bit more reading, I think you guys are on the right track... the Appdomain approach seems to be the better way. :sweating:

Edited by ptrex
Link to comment
Share on other sites

The good about C++ examples is that they are easy to translate to AutoIt in a fairly direct way. This is a continuation of the code by Danyfirex in post 25. The code in the C++ link in post 54 is added. It certainly looks like we've got an _AppDomain object:

;#AutoIt3Wrapper_UseX64=y

Global Const $S_OK = 0

Global Const $sTag_CLRMetaHost = _
  "GetRuntime hresult(wstr;struct*;ptr);" & _
  "GetVersionFromFile hresult(ptr;ptr;ptr);" & _
  "EnumerateInstalledRuntimes hresult(ptr);" & _
  "EnumerateLoadedRuntimes hresult(ptr;ptr);" & _
  "RequestRuntimeLoadedNotification hresult(ptr,ptr,ptr):" & _
  "QueryLegacyV2RuntimeBinding hresult(ptr;ptr);" & _
  "ExitProcess hresult(int);"

Global Const $sTag_CLRRuntimeInfo = _
  "GetVersionString hresult(ptr;ptr);" & _
  "GetRuntimeDirectory hresult(ptr;ptr);" & _
  "IsLoaded hresult(ptr;ptr);" & _
  "LoadErrorString hresult(ptr;ptr;ptr;ptr);" & _
  "LoadLibrary hresult(ptr;ptr);" & _
  "GetProcAddress hresult(ptr;ptr);" & _
  "GetInterface hresult(ptr;ptr;ptr);" & _
  "IsLoadable hresult(Bool*);" & _
  "SetDefaultStartupFlags hresult(ptr;ptr);" & _
  "GetDefaultStartupFlags hresult(ptr;ptr;ptr);" & _
  "BindAsLegacyV2Runtime hresult();" & _
  "IsStarted hresult(ptr;ptr);"

Global Const $sTag_CLRRuntimeHost = _
  "Start hresult();" & _
  "Stop hresult();" & _
  "SetHostControl hresult(ptr);" & _
  "GetCLRControl hresult(ptr*);" & _
  "UnloadAppDomain hresult(ptr;ptr);" & _
  "ExecuteInAppDomain hresult(ptr;ptr;ptr);" & _
  "GetCurrentAppDomainId hresult(ptr);" & _
  "ExecuteApplication hresult(ptr;ptr;ptr;ptr;ptr;ptr);" & _
  "ExecuteInDefaultAppDomain hresult(wstr;wstr;wstr;wstr;ptr*);"

Global Const $sCLSID_CLRMetaHost = "{9280188d-0e8e-4867-b30c-7fa83884e8de}"
Global Const $sIID_ICLRMetaHost = "{d332db9e-b9b3-4125-8207-a14884f53216}"
Global Const $sIID_ICLRRuntimeInfo = "{BD39D1D2-BA2F-486a-89B0-B4B0CB466891}"
Global Const $sCLSID_CLRRuntimeHost = "{90F1A06E-7712-4762-86B5-7A5EBA6BDB02}"
Global Const $sIID_ICLRRuntimeHost = "{90F1A06C-7712-4762-86B5-7A5EBA6BDB02}"

Global $tCLSID_CLRMetaHost = _WinAPI_CLSIDFromString($sCLSID_CLRMetaHost)
Global $tIID_ICLRMetaHost = _WinAPI_CLSIDFromString($sIID_ICLRMetaHost)
Global $tIID_ICLRRuntimeInfo = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeInfo)
Global $tCLSID_CLRRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CLRRuntimeHost)
Global $tIID_ICLRRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeHost)

Local $hMSCorEE = DllOpen("MSCorEE.DLL")
Local $aRet = DllCall($hMSCorEE, "long", "CLRCreateInstance", "struct*", $tCLSID_CLRMetaHost, "struct*", $tIID_ICLRMetaHost, "ptr*", 0)

If $aRet[0] = $S_OK Then
  Local $pClrHost = $aRet[3]
  Local $oClrHost = ObjCreateInterface($pClrHost, $sIID_ICLRMetaHost, $sTag_CLRMetaHost)
  ConsoleWrite(">oClrHost: " & IsObj($oClrHost) & @CRLF)

  Local $sNETFrameworkVersion = "v4.0.30319"
  Local $tCLRRuntimeInfo = DllStructCreate("ptr")
  $oClrHost.GetRuntime($sNETFrameworkVersion, $tIID_ICLRRuntimeInfo, DllStructGetPtr($tCLRRuntimeInfo))
  Local $pCLRRuntimeInfo = DllStructGetData($tCLRRuntimeInfo, 1)
  ConsoleWrite(">pCLRRuntimeInfo: " & $pCLRRuntimeInfo & @CRLF)

  Local $oCLRRuntimeInfo = ObjCreateInterface($pCLRRuntimeInfo, $sIID_ICLRRuntimeInfo, $sTag_CLRRuntimeInfo)
  ConsoleWrite(">oCLRRuntimeInfo: " & IsObj($oCLRRuntimeInfo)  & @CRLF)
  Local $isIsLoadable = 0
  $oCLRRuntimeInfo.IsLoadable($isIsLoadable)
  ConsoleWrite(">IsLoadable: " & $isIsLoadable & @CRLF)

  If $isIsLoadable Then
    Local $tCLRRuntimeHost = DllStructCreate("ptr")
    $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CLRRuntimeHost), DllStructGetPtr($tIID_ICLRRuntimeHost), DllStructGetPtr($tCLRRuntimeHost))
    Local $pCLRRuntimeHost = DllStructGetData($tCLRRuntimeHost, 1)
    ConsoleWrite(">pCLRRuntimeHost: " & $pCLRRuntimeHost & @CRLF)
    Local $oCLRRuntimeHost = ObjCreateInterface($pCLRRuntimeHost, $sIID_ICLRRuntimeHost, $sTag_CLRRuntimeHost)
    ConsoleWrite(">oCLRRuntimeHost" & IsObj($oCLRRuntimeHost)  & @CRLF)

    Local $iRet = 0
    $oCLRRuntimeHost.Start()
    ;$oCLRRuntimeHost.ExecuteInDefaultAppDomain(@ScriptDir & "\C#Library.dll", "ClassLibraryDemo.ClassDemo", "NetMsgBox", "AutoIt Rocks!!! " & @CRLF & "Danyfirex does too lol!!!", $iRet)
    ;ConsoleWrite(">Ret: " & $iRet & @CRLF)

    Local Const $sCLSID_CorRuntimeHost = "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}"
    Local Const $sIID_ICorRuntimeHost = "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"
    Local $tCLSID_CorRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CorRuntimeHost)
    Local $tIID_ICorRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICorRuntimeHost)
    Local Const $sTag_ICorRuntimeHost = _
      "CreateLogicalThreadState hresult();" & _
      "DeleteLogicalThreadState hresult();" & _
      "SwitchInLogicalThreadState hresult();" & _
      "SwitchOutLogicalThreadState hresult();" & _
      "LocksHeldByLogicalThread hresult();" & _
      "MapFile hresult();" & _
      "GetConfiguration hresult();" & _
      "Start hresult();" & _
      "Stop hresult();" & _
      "CreateDomain hresult();" & _
      "GetDefaultDomain hresult(ptr*);" & _
      "EnumDomains hresult();" & _
      "NextDomain hresult();" & _
      "CloseEnum hresult();" & _
      "CreateDomainEx hresult();" & _
      "CreateDomainSetup hresult();" & _
      "CreateEvidence hresult();" & _
      "UnloadDomain hresult();" & _
      "CurrentDomain hresult();"

    Local $tCorRuntimeHost = DllStructCreate("ptr")
    $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CorRuntimeHost), DllStructGetPtr($tIID_ICorRuntimeHost), DllStructGetPtr($tCorRuntimeHost))
    Local $pCorRuntimeHost = DllStructGetData($tCorRuntimeHost, 1)
    ConsoleWrite( "$pCorRuntimeHost = " & $pCorRuntimeHost & @CRLF )

    Local $oCorRuntimeHost = ObjCreateInterface($pCorRuntimeHost, $sIID_ICorRuntimeHost, $sTag_ICorRuntimeHost)
    ConsoleWrite( "IsObj( $oCorRuntimeHost ) = " & IsObj( $oCorRuntimeHost ) & @CRLF )
  
    $oCorRuntimeHost.Start()

    Local Const $sIID_AppDomain = "{05F696DC-2B29-3663-AD8B-C4389CF2A713}"
    Local Const $sTag_AppDomain = _
      "GetTypeInfoCount hresult();" & _
      "GetTypeInfo hresult();" & _
      "GetIDsOfNames hresult();" & _
      "Invoke hresult();" & _
      "get_ToString hresult();" & _
      "Equals hresult();" & _
      "GetHashCode hresult();" & _
      "GetType hresult();" & _
      "InitializeLifetimeService hresult();" & _
      "GetLifetimeService hresult();" & _
      "get_Evidence hresult();" & _
      "add_DomainUnload hresult();" & _
      "remove_DomainUnload hresult();" & _
      "add_AssemblyLoad hresult();" & _
      "remove_AssemblyLoad hresult();" & _
      "add_ProcessExit hresult();" & _
      "remove_ProcessExit hresult();" & _
      "add_TypeResolve hresult();" & _
      "remove_TypeResolve hresult();" & _
      "add_ResourceResolve hresult();" & _
      "remove_ResourceResolve hresult();" & _
      "add_AssemblyResolve hresult();" & _
      "remove_AssemblyResolve hresult();" & _
      "add_UnhandledException hresult();" & _
      "remove_UnhandledException hresult();" & _
      "DefineDynamicAssembly hresult();" & _
      "DefineDynamicAssembly_2 hresult();" & _
      "DefineDynamicAssembly_3 hresult();" & _
      "DefineDynamicAssembly_4 hresult();" & _
      "DefineDynamicAssembly_5 hresult();" & _
      "DefineDynamicAssembly_6 hresult();" & _
      "DefineDynamicAssembly_7 hresult();" & _
      "DefineDynamicAssembly_8 hresult();" & _
      "DefineDynamicAssembly_9 hresult();" & _
      "CreateInstance hresult();" & _
      "CreateInstanceFrom hresult();" & _
      "CreateInstance_2 hresult();" & _
      "CreateInstanceFrom_2 hresult();" & _
      "CreateInstance_3 hresult();" & _
      "CreateInstanceFrom_3 hresult();" & _
      "Load hresult();" & _
      "Load_2 hresult();" & _
      "Load_3 hresult();" & _
      "Load_4 hresult();" & _
      "Load_5 hresult();" & _
      "Load_6 hresult();" & _
      "Load_7 hresult();" & _
      "ExecuteAssembly hresult();" & _
      "ExecuteAssembly_2 hresult();" & _
      "ExecuteAssembly_3 hresult();" & _
      "get_FriendlyName hresult();" & _
      "get_BaseDirectory hresult();" & _
      "get_RelativeSearchPath hresult();" & _
      "get_ShadowCopyFiles hresult();" & _
      "GetAssemblies hresult();" & _
      "AppendPrivatePath hresult();" & _
      "ClearPrivatePath ) = 0; hresult();" & _
      "SetShadowCopyPath hresult();" & _
      "ClearShadowCopyPath ) = 0; hresult();" & _
      "SetCachePath hresult();" & _
      "SetData hresult();" & _
      "GetData hresult();" & _
      "SetAppDomainPolicy hresult();" & _
      "SetThreadPrincipal hresult();" & _
      "SetPrincipalPolicy hresult();" & _
      "DoCallBack hresult();" & _
      "get_DynamicDirectory hresult();"

    Local $pAppDomain
    $oCorRuntimeHost.GetDefaultDomain( $pAppDomain )
    ConsoleWrite( "$pAppDomain = " & Ptr( $pAppDomain ) & @CRLF )

    Local $oAppDomain = ObjCreateInterface( $pAppDomain, $sIID_AppDomain, $sTag_AppDomain )
    ConsoleWrite( "IsObj( $oAppDomain ) = " & IsObj( $oAppDomain ) & @CRLF )
  EndIf

EndIf

DllClose($hMSCorEE) ;free


Func _WinAPI_CLSIDFromString($sGUID)
  $tGUID = DllStructCreate('ulong Data1;ushort Data2;ushort Data3;byte Data4[8]')
  $iRet = DllCall('ole32.dll', 'uint', 'CLSIDFromString', 'wstr', $sGUID, 'ptr', DllStructGetPtr($tGUID))
  If (@error) Or ($iRet[0]) Then
    Return SetError(@error, @extended, 0)
  EndIf
  Return $tGUID
EndFunc   ;==>_WinAPI_CLSIDFromString

 

Link to comment
Share on other sites

nice, will combine it with post 48 tagDefinition of appdomain later this week.

For me C/C++ and C# is easier (compiled many examples last 2 weeks in visual studio) than transforming to AutoIt but based on above examples learning so already thx for that.

Link to comment
Share on other sites

I have to pass on any sound of C unfortunately :(. So thanks for all the good stuff here, learning a lot as we go along.

this is already the combinations post 25 + 28 + 29 + 56

 

;#AutoIt3Wrapper_UseX64=y

Global Const $S_OK = 0

Global Const $sTag_CLRMetaHost = _
  "GetRuntime hresult(wstr;struct*;ptr);" & _
  "GetVersionFromFile hresult(ptr;ptr;ptr);" & _
  "EnumerateInstalledRuntimes hresult(ptr);" & _
  "EnumerateLoadedRuntimes hresult(ptr;ptr);" & _
  "RequestRuntimeLoadedNotification hresult(ptr,ptr,ptr):" & _
  "QueryLegacyV2RuntimeBinding hresult(ptr;ptr);" & _
  "ExitProcess hresult(int);"

Global Const $sTagCLRRuntimeInfo = "GetVersionString hresult(ptr;ptr);" & _
        "GetRuntimeDirectory hresult(ptr;ptr);" & _
        "IsLoaded hresult(ptr;ptr);" & _
        "LoadErrorString hresult(ptr;ptr;ptr;ptr);" & _
        "LoadLibrary hresult(ptr;ptr);" & _
        "GetProcAddress hresult(ptr;ptr);" & _
        "GetInterface hresult(ptr;ptr;ptr);" & _
        "IsLoadable hresult(Bool*);" & _
        "SetDefaultStartupFlags hresult(ptr;ptr);" & _
        "GetDefaultStartupFlags hresult(ptr;ptr;ptr);" & _
        "BindAsLegacyV2Runtime hresult();" & _
        "IsStarted hresult(ptr;ptr);"

Global Const $sTag_CLRRuntimeInfo = _
  "GetVersionString hresult(ptr;ptr);" & _
  "GetRuntimeDirectory hresult(ptr;ptr);" & _
  "IsLoaded hresult(ptr;ptr);" & _
  "LoadErrorString hresult(ptr;ptr;ptr;ptr);" & _
  "LoadLibrary hresult(ptr;ptr);" & _
  "GetProcAddress hresult(ptr;ptr);" & _
  "GetInterface hresult(ptr;ptr;ptr);" & _
  "IsLoadable hresult(Bool*);" & _
  "SetDefaultStartupFlags hresult(ptr;ptr);" & _
  "GetDefaultStartupFlags hresult(ptr;ptr;ptr);" & _
  "BindAsLegacyV2Runtime hresult();" & _
  "IsStarted hresult(ptr;ptr);"

Global Const $sTag_CLRRuntimeHost = _
  "Start hresult();" & _
  "Stop hresult();" & _
  "SetHostControl hresult(ptr);" & _
  "GetCLRControl hresult(ptr*);" & _
  "UnloadAppDomain hresult(ptr;ptr);" & _
  "ExecuteInAppDomain hresult(ptr;ptr;ptr);" & _
  "GetCurrentAppDomainId hresult(ptr);" & _
  "ExecuteApplication hresult(ptr;ptr;ptr;ptr;ptr;ptr);" & _
  "ExecuteInDefaultAppDomain hresult(wstr;wstr;wstr;wstr;ptr*);"

Global Const $sTagEnumUnknown = "Next hresult(ulong;ptr*;ulong); Skip hresult(ptr); Reset hresult(); Clone hresult(ptr);"

#Region CLSID & IID
Global Const $sCLSID_CLRMetaHost = "{9280188d-0e8e-4867-b30c-7fa83884e8de}"
Global Const $sIID_ICLRMetaHost = "{d332db9e-b9b3-4125-8207-a14884f53216}"
Global Const $sIID_ICLRRuntimeInfo = "{BD39D1D2-BA2F-486a-89B0-B4B0CB466891}"
Global Const $sCLSID_CLRRuntimeHost = "{90F1A06E-7712-4762-86B5-7A5EBA6BDB02}"
Global Const $sIID_ICLRRuntimeHost = "{90F1A06C-7712-4762-86B5-7A5EBA6BDB02}"
Global Const $sIID_IEnumUnknown = "{00000100-0000-0000-C000-000000000046}"

Global $tCLSID_CLRMetaHost = _WinAPI_CLSIDFromString($sCLSID_CLRMetaHost)
Global $tIID_ICLRMetaHost = _WinAPI_CLSIDFromString($sIID_ICLRMetaHost)
Global $tIID_ICLRRuntimeInfo = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeInfo)
Global $tCLSID_CLRRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CLRRuntimeHost)
Global $tIID_ICLRRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeHost)
Global $tIID_IEnumUnknown = _WinAPI_CLSIDFromString($sIID_IEnumUnknown)
#EndRegion CLSID & IID

Local $hMSCorEE = DllOpen("MSCorEE.DLL")
Local $aRet = DllCall($hMSCorEE, "long", "CLRCreateInstance", "struct*", $tCLSID_CLRMetaHost, "struct*", $tIID_ICLRMetaHost, "ptr*", 0)

If $aRet[0] = $S_OK Then
  Local $pClrHost = $aRet[3]
  Local $oClrHost = ObjCreateInterface($pClrHost, $sIID_ICLRMetaHost, $sTag_CLRMetaHost)
  ConsoleWrite("> oClrHost: " & IsObj($oClrHost) & @CRLF)

#Region Get EnumerateRuntimes
    Local $tEnumerateRuntimes = DllStructCreate("ptr")
    $oClrHost.EnumerateInstalledRuntimes(DllStructGetPtr($tEnumerateRuntimes))
    Local $pEnumerateRuntimes = DllStructGetData($tEnumerateRuntimes, 1)
    ConsoleWrite("> pEnumerateRuntimes: " & $pEnumerateRuntimes & @CRLF)

    Local $oEnumerateRuntimes = ObjCreateInterface($pEnumerateRuntimes, $sIID_IEnumUnknown, $sTagEnumUnknown)
    ConsoleWrite("> oEnumerateRuntimes: " & IsObj($oEnumerateRuntimes) & @CRLF & @CRLF)
    Local $pRuntime = 0
    Local $oRunInfo = 0
    Local $tSize = 0
    Local $tString = 0
    ;List Runtimes
    While $oEnumerateRuntimes.Next(1, $pRuntime, Null) = $S_OK
        $oRunInfo = ObjCreateInterface($pRuntime, $sIID_ICLRRuntimeInfo, $sTagCLRRuntimeInfo)
;~      ConsoleWrite(">oRunInfo: " & IsObj($oRunInfo) & @CRLF)

    #Region Get GetVersionString
        $tSize = DllStructCreate("dword Data")
        $tSize.Data = 0
        $oRunInfo.GetVersionString(Null, DllStructGetPtr($tSize, 1)) ;get requiered Size
;~      ConsoleWrite(">Required Size: " & $tSize.Data & @CRLF)
        $tString = DllStructCreate("wchar Data[" & $tSize.Data & "]")
        $oRunInfo.GetVersionString(DllStructGetPtr($tString, 1), DllStructGetPtr($tSize, 1)) ;get Version String
        ConsoleWrite("+Version String: " & $tString.Data & @CRLF)
    #EndRegion Get GetVersionString
    WEnd
        ConsoleWrite( @CRLF)
#EndRegion Get EnumerateRuntimes

  Local $sNETFrameworkVersion = "v4.0.30319"
  Local $tCLRRuntimeInfo = DllStructCreate("ptr")
  $oClrHost.GetRuntime($sNETFrameworkVersion, $tIID_ICLRRuntimeInfo, DllStructGetPtr($tCLRRuntimeInfo))
  Local $pCLRRuntimeInfo = DllStructGetData($tCLRRuntimeInfo, 1)
  ConsoleWrite("> pCLRRuntimeInfo: " & $pCLRRuntimeInfo & @CRLF)

  Local $oCLRRuntimeInfo = ObjCreateInterface($pCLRRuntimeInfo, $sIID_ICLRRuntimeInfo, $sTag_CLRRuntimeInfo)
  ConsoleWrite("> oCLRRuntimeInfo: " & IsObj($oCLRRuntimeInfo)  & @CRLF)
  Local $isIsLoadable = 0
  $oCLRRuntimeInfo.IsLoadable($isIsLoadable)
  ConsoleWrite("> IsLoadable: " & $isIsLoadable & @CRLF)

#Region Get GetVersionString
    Local $tSize = DllStructCreate("dword Data")
    $tSize.Data = 0
    $oRunInfo.GetVersionString(Null, DllStructGetPtr($tSize, 1)) ;get requiered Size
    ConsoleWrite("> Required Size: " & $tSize.Data & @CRLF)
    Local $tString = DllStructCreate("wchar Data[" & $tSize.Data & "]")
    $oRunInfo.GetVersionString(DllStructGetPtr($tString, 1), DllStructGetPtr($tSize, 1)) ;get Version String
    ConsoleWrite("> Version String: " & $tString.Data & @CRLF)
#EndRegion  Get GetVersionString

  If $isIsLoadable Then
    Local $tCLRRuntimeHost = DllStructCreate("ptr")
    $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CLRRuntimeHost), DllStructGetPtr($tIID_ICLRRuntimeHost), DllStructGetPtr($tCLRRuntimeHost))
    Local $pCLRRuntimeHost = DllStructGetData($tCLRRuntimeHost, 1)
    ConsoleWrite("> pCLRRuntimeHost: " & $pCLRRuntimeHost & @CRLF)
    Local $oCLRRuntimeHost = ObjCreateInterface($pCLRRuntimeHost, $sIID_ICLRRuntimeHost, $sTag_CLRRuntimeHost)
    ConsoleWrite("> oCLRRuntimeHost" & IsObj($oCLRRuntimeHost)  & @CRLF & @CRLF)

    Local $iRet = 0
    $oCLRRuntimeHost.Start()
    ;$oCLRRuntimeHost.ExecuteInDefaultAppDomain(@ScriptDir & "\C#Library.dll", "ClassLibraryDemo.ClassDemo", "NetMsgBox", "AutoIt Rocks!!! " & @CRLF & "Danyfirex does too lol!!!", $iRet)
    ;ConsoleWrite(">Ret: " & $iRet & @CRLF)

    Local Const $sCLSID_CorRuntimeHost = "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}"
    Local Const $sIID_ICorRuntimeHost = "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"
    Local $tCLSID_CorRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CorRuntimeHost)
    Local $tIID_ICorRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICorRuntimeHost)
    Local Const $sTag_ICorRuntimeHost = _
      "CreateLogicalThreadState hresult();" & _
      "DeleteLogicalThreadState hresult();" & _
      "SwitchInLogicalThreadState hresult();" & _
      "SwitchOutLogicalThreadState hresult();" & _
      "LocksHeldByLogicalThread hresult();" & _
      "MapFile hresult();" & _
      "GetConfiguration hresult();" & _
      "Start hresult();" & _
      "Stop hresult();" & _
      "CreateDomain hresult();" & _
      "GetDefaultDomain hresult(ptr*);" & _
      "EnumDomains hresult();" & _
      "NextDomain hresult();" & _
      "CloseEnum hresult();" & _
      "CreateDomainEx hresult();" & _
      "CreateDomainSetup hresult();" & _
      "CreateEvidence hresult();" & _
      "UnloadDomain hresult();" & _
      "CurrentDomain hresult();"

    Local $tCorRuntimeHost = DllStructCreate("ptr")
    $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CorRuntimeHost), DllStructGetPtr($tIID_ICorRuntimeHost), DllStructGetPtr($tCorRuntimeHost))
    Local $pCorRuntimeHost = DllStructGetData($tCorRuntimeHost, 1)
    ConsoleWrite( "$pCorRuntimeHost = " & $pCorRuntimeHost & @CRLF )

    Local $oCorRuntimeHost = ObjCreateInterface($pCorRuntimeHost, $sIID_ICorRuntimeHost, $sTag_ICorRuntimeHost)
    ConsoleWrite( "IsObj( $oCorRuntimeHost ) = " & IsObj( $oCorRuntimeHost ) & @CRLF )

    $oCorRuntimeHost.Start()

    Local Const $sIID_AppDomain = "{05F696DC-2B29-3663-AD8B-C4389CF2A713}"
    Local Const $sTag_AppDomain = _
      "GetTypeInfoCount hresult();" & _
      "GetTypeInfo hresult();" & _
      "GetIDsOfNames hresult();" & _
      "Invoke hresult();" & _
      "get_ToString hresult();" & _
      "Equals hresult();" & _
      "GetHashCode hresult();" & _
      "GetType hresult();" & _
      "InitializeLifetimeService hresult();" & _
      "GetLifetimeService hresult();" & _
      "get_Evidence hresult();" & _
      "add_DomainUnload hresult();" & _
      "remove_DomainUnload hresult();" & _
      "add_AssemblyLoad hresult();" & _
      "remove_AssemblyLoad hresult();" & _
      "add_ProcessExit hresult();" & _
      "remove_ProcessExit hresult();" & _
      "add_TypeResolve hresult();" & _
      "remove_TypeResolve hresult();" & _
      "add_ResourceResolve hresult();" & _
      "remove_ResourceResolve hresult();" & _
      "add_AssemblyResolve hresult();" & _
      "remove_AssemblyResolve hresult();" & _
      "add_UnhandledException hresult();" & _
      "remove_UnhandledException hresult();" & _
      "DefineDynamicAssembly hresult();" & _
      "DefineDynamicAssembly_2 hresult();" & _
      "DefineDynamicAssembly_3 hresult();" & _
      "DefineDynamicAssembly_4 hresult();" & _
      "DefineDynamicAssembly_5 hresult();" & _
      "DefineDynamicAssembly_6 hresult();" & _
      "DefineDynamicAssembly_7 hresult();" & _
      "DefineDynamicAssembly_8 hresult();" & _
      "DefineDynamicAssembly_9 hresult();" & _
      "CreateInstance hresult();" & _
      "CreateInstanceFrom hresult();" & _
      "CreateInstance_2 hresult();" & _
      "CreateInstanceFrom_2 hresult();" & _
      "CreateInstance_3 hresult();" & _
      "CreateInstanceFrom_3 hresult();" & _
      "Load hresult();" & _
      "Load_2 hresult();" & _
      "Load_3 hresult();" & _
      "Load_4 hresult();" & _
      "Load_5 hresult();" & _
      "Load_6 hresult();" & _
      "Load_7 hresult();" & _
      "ExecuteAssembly hresult();" & _
      "ExecuteAssembly_2 hresult();" & _
      "ExecuteAssembly_3 hresult();" & _
      "get_FriendlyName hresult();" & _
      "get_BaseDirectory hresult();" & _
      "get_RelativeSearchPath hresult();" & _
      "get_ShadowCopyFiles hresult();" & _
      "GetAssemblies hresult();" & _
      "AppendPrivatePath hresult();" & _
      "ClearPrivatePath ) = 0; hresult();" & _
      "SetShadowCopyPath hresult();" & _
      "ClearShadowCopyPath ) = 0; hresult();" & _
      "SetCachePath hresult();" & _
      "SetData hresult();" & _
      "GetData hresult();" & _
      "SetAppDomainPolicy hresult();" & _
      "SetThreadPrincipal hresult();" & _
      "SetPrincipalPolicy hresult();" & _
      "DoCallBack hresult();" & _
      "get_DynamicDirectory hresult();"

    Local $pAppDomain
    $oCorRuntimeHost.GetDefaultDomain( $pAppDomain )
    ConsoleWrite( "$pAppDomain = " & Ptr( $pAppDomain ) & @CRLF )

    Local $oAppDomain = ObjCreateInterface( $pAppDomain, $sIID_AppDomain, $sTag_AppDomain )
    ConsoleWrite( "IsObj( $oAppDomain ) = " & IsObj( $oAppDomain ) & @CRLF & @CRLF)
  EndIf

EndIf

DllClose($hMSCorEE) ;free


Func _WinAPI_CLSIDFromString($sGUID)
  $tGUID = DllStructCreate('ulong Data1;ushort Data2;ushort Data3;byte Data4[8]')
  $iRet = DllCall('ole32.dll', 'uint', 'CLSIDFromString', 'wstr', $sGUID, 'ptr', DllStructGetPtr($tGUID))
  If (@error) Or ($iRet[0]) Then
    Return SetError(@error, @extended, 0)
  EndIf
  Return $tGUID
EndFunc   ;==>_WinAPI_CLSIDFromString

 

 

Link to comment
Share on other sites

Personally I do not find all this .NET stuff particularly interesting. This means that I know virtually nothing about the .NET Framework, and even less about the CLR. But I think it's interesting to find out whether it's possible to implement in AutoIt.

Because I do not know anything about the subject, I prefer to translate some existing code to AutoIt. Our AutoHotkey friends have been able to create a CLR.ahk UDF and make some working examples.

I've tested two of the examples. An example with a .NET assembly: XPTable.dll. And an example with some C# code which is compiled and executed with the methods of the _AppDomain object. Now that we have an _AppDomain object in AutoIt it should be possible to translate these examples to AutoIt.

The _AppDomain object has a lot of methods. Some of them are nearly the same but with different parameters. It's not an entirely trivial matter to find out which method to use. Therefore, it's nice with an example.

If you want to test the AutoHotkey example with the XPTable.dll you do it this way:

Download and store the following ressources in the same folder:

  • CLR v1.2 for AutoHotkey v1.1. Click "CLR".
  • XPTable.dll. Click "Download demo project". Search XPTable.dll in the zip.

Copy and store this AHK-code in tst00.ahk (still same folder):

#Include CLR.ahk

#NoEnv
SetWorkingDir, %A_ScriptDir%

;CLR_Start()

xptables := CLR_LoadLibrary("XPTable.dll")

; Type names must be fully qualified.
table       := CLR_CreateObject(xptables,"XPTable.Models.Table")
columnModel := CLR_CreateObject(xptables,"XPTable.Models.ColumnModel")
tableModel  := CLR_CreateObject(xptables,"XPTable.Models.TableModel")

table.ColumnModel := columnModel
table.TableModel := tableModel

columnModel.Columns.Add(CLR_CreateObject(xptables, "XPTable.Models.TextColumn", "MyTextColumn"))
columnModel.Columns.Add(CLR_CreateObject(xptables, "XPTable.Models.CheckBoxColumn", "MyCheckBoxColumn"))
columnModel.Columns.Add(CLR_CreateObject(xptables, "XPTable.Models.ButtonColumn", "MyButtonColumn"))

; msgbox % "VisibleColumnCount: " columnModel.VisibleColumnCount
; msgbox % "TotalColumnWidth: " columnModel.TotalColumnWidth
; msgbox % table.FirstVisibleColumn()

tableModel.Rows.Add(CLR_CreateObject(xptables, "XPTable.Models.Row"))
tableModel.Rows.Item(0).Cells.Add(CLR_CreateObject(xptables, "XPTable.Models.Cell", "MyCell1"))
tableModel.Rows.Item(0).Cells.Add(CLR_CreateObject(xptables, "XPTable.Models.Cell", "MyCheckbox1", true))
tableModel.Rows.Item(0).Cells.Add(CLR_CreateObject(xptables, "XPTable.Models.Cell", "MyButton1"))

; or
; tableModel.Rows.Add(row1:=CLR_CreateObject(xptables, "XPTable.Models.Row"))
; row1.Cells.Add(CLR_CreateObject(xptables, "XPTable.Models.Cell", "MyCell1"))
; row1 := ""

msgbox % "Columns: " columnModel.Columns.count() "`nRows: " tableModel.Rows.count "`nCells: " tableModel.Rows.Item(0).Cells.count
columnModel := ""
tableModel := ""

table.Left := 10
table.Top := 10
table.Width := 300
table.Height := 200

hwnd := table.Handle
Gui, +LastFound
DllCall("SetParent", UInt, hwnd, UInt, WinExist())
Gui, Show, W320 H220, XPTable Example

return

Open tst00.ahk in SciTE4AutoHotkey and press F5 to run the script. For some reason the script does not exit properly. Just kill the AutoHotkey process.

 

If you want to test the AutoHotkey example with C# code you do it this way:

Copy and store this AHK-code in tst01.ahk (still same folder):

c# =
(
  using System.Windows.Forms;
  using System.Management;
  using System;
  class Foo {
  public string Test(string geraet) {
  string Tabelle = " ";
  ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + geraet);
  ManagementObjectCollection irgendwas = searcher.Get();
  foreach (ManagementObject mo in irgendwas)
  {
    foreach (PropertyData prop in mo.Properties)
    {
     Tabelle = Tabelle + "/n" + prop.Name + ": " + prop.Value;
    }
  }
     return Tabelle;
    }
  }
)

;CLR_Start()

asm := CLR_CompileC#(c#, "System.dll | System.Management.dll | System.Windows.Forms.dll")
obj := CLR_CreateObject(asm, "Foo")
kram:=obj.Test("Win32_Processor")
StringReplace, kram, kram, /n, `n , All
MsgBox %kram%
obj := "", asm := "" ; Not strictly necessary
; Note: This doesn't unload the Assembly itself, just frees the Assembly object.

return

#include CLR.ahk

Open tst01.ahk in SciTE4AutoHotkey and press F5 to run the script.

 

If these examples can be translated into AutoIt, it'll clearly be a step in the right direction.

Link to comment
Share on other sites

Hello. Nice find Larsj I think it's no hard to traslate to AutoIt Maybe I'll do later. (I'm busy right now.)

Also Here is the 39#'s Stack Object Example.

 

;~ #AutoIt3Wrapper_UseX64=y
#include <Memory.au3>



Global Const $S_OK = 0

Global Const $sTag_CLRMetaHost = _
        "GetRuntime hresult(wstr;struct*;ptr);" & _
        "GetVersionFromFile hresult(ptr;ptr;ptr);" & _
        "EnumerateInstalledRuntimes hresult(ptr);" & _
        "EnumerateLoadedRuntimes hresult(ptr;ptr);" & _
        "RequestRuntimeLoadedNotification hresult(ptr,ptr,ptr):" & _
        "QueryLegacyV2RuntimeBinding hresult(ptr;ptr);" & _
        "ExitProcess hresult(int);"

Global Const $sTag_CLRRuntimeInfo = _
        "GetVersionString hresult(ptr;ptr);" & _
        "GetRuntimeDirectory hresult(ptr;ptr);" & _
        "IsLoaded hresult(ptr;ptr);" & _
        "LoadErrorString hresult(ptr;ptr;ptr;ptr);" & _
        "LoadLibrary hresult(ptr;ptr);" & _
        "GetProcAddress hresult(ptr;ptr);" & _
        "GetInterface hresult(ptr;ptr;ptr);" & _
        "IsLoadable hresult(Bool*);" & _
        "SetDefaultStartupFlags hresult(ptr;ptr);" & _
        "GetDefaultStartupFlags hresult(ptr;ptr;ptr);" & _
        "BindAsLegacyV2Runtime hresult();" & _
        "IsStarted hresult(ptr;ptr);"

Global Const $sTag_CLRRuntimeHost = _
        "Start hresult();" & _
        "Stop hresult();" & _
        "SetHostControl hresult(ptr);" & _
        "GetCLRControl hresult(ptr*);" & _
        "UnloadAppDomain hresult(ptr;ptr);" & _
        "ExecuteInAppDomain hresult(ptr;ptr;ptr);" & _
        "GetCurrentAppDomainId hresult(ptr);" & _
        "ExecuteApplication hresult(ptr;ptr;ptr;ptr;ptr;ptr);" & _
        "ExecuteInDefaultAppDomain hresult(wstr;wstr;wstr;wstr;ptr*);"


Global Const $sIID_AppDomain = "{05F696DC-2B29-3663-AD8B-C4389CF2A713}"
Global Const $sTag_AppDomain = _
        "GetTypeInfoCount hresult();" & _
        "GetTypeInfo hresult();" & _
        "GetIDsOfNames hresult();" & _
        "Invoke hresult();" & _
        "get_ToString hresult();" & _
        "Equals hresult();" & _
        "GetHashCode hresult();" & _
        "GetType hresult();" & _
        "InitializeLifetimeService hresult();" & _
        "GetLifetimeService hresult();" & _
        "get_Evidence hresult();" & _
        "add_DomainUnload hresult();" & _
        "remove_DomainUnload hresult();" & _
        "add_AssemblyLoad hresult();" & _
        "remove_AssemblyLoad hresult();" & _
        "add_ProcessExit hresult();" & _
        "remove_ProcessExit hresult();" & _
        "add_TypeResolve hresult();" & _
        "remove_TypeResolve hresult();" & _
        "add_ResourceResolve hresult();" & _
        "remove_ResourceResolve hresult();" & _
        "add_AssemblyResolve hresult();" & _
        "remove_AssemblyResolve hresult();" & _
        "add_UnhandledException hresult();" & _
        "remove_UnhandledException hresult();" & _
        "DefineDynamicAssembly hresult();" & _
        "DefineDynamicAssembly_2 hresult();" & _
        "DefineDynamicAssembly_3 hresult();" & _
        "DefineDynamicAssembly_4 hresult();" & _
        "DefineDynamicAssembly_5 hresult();" & _
        "DefineDynamicAssembly_6 hresult();" & _
        "DefineDynamicAssembly_7 hresult();" & _
        "DefineDynamicAssembly_8 hresult();" & _
        "DefineDynamicAssembly_9 hresult();" & _
        "CreateInstance hresult(bstr;bstr;object*);" & _
        "CreateInstanceFrom hresult();" & _
        "CreateInstance_2 hresult();" & _
        "CreateInstanceFrom_2 hresult();" & _
        "CreateInstance_3 hresult();" & _
        "CreateInstanceFrom_3 hresult();" & _
        "Load hresult();" & _
        "Load_2 hresult();" & _
        "Load_3 hresult();" & _
        "Load_4 hresult();" & _
        "Load_5 hresult();" & _
        "Load_6 hresult();" & _
        "Load_7 hresult();" & _
        "ExecuteAssembly hresult();" & _
        "ExecuteAssembly_2 hresult();" & _
        "ExecuteAssembly_3 hresult();" & _
        "get_FriendlyName hresult();" & _
        "get_BaseDirectory hresult();" & _
        "get_RelativeSearchPath hresult();" & _
        "get_ShadowCopyFiles hresult();" & _
        "GetAssemblies hresult();" & _
        "AppendPrivatePath hresult();" & _
        "ClearPrivatePath ) = 0; hresult();" & _
        "SetShadowCopyPath hresult();" & _
        "ClearShadowCopyPath ) = 0; hresult();" & _
        "SetCachePath hresult();" & _
        "SetData hresult();" & _
        "GetData hresult();" & _
        "SetAppDomainPolicy hresult();" & _
        "SetThreadPrincipal hresult();" & _
        "SetPrincipalPolicy hresult();" & _
        "DoCallBack hresult();" & _
        "get_DynamicDirectory hresult();"

Global Const $sTag_ICorRuntimeHost = _
        "CreateLogicalThreadState hresult();" & _
        "DeleteLogicalThreadState hresult();" & _
        "SwitchInLogicalThreadState hresult();" & _
        "SwitchOutLogicalThreadState hresult();" & _
        "LocksHeldByLogicalThread hresult();" & _
        "MapFile hresult();" & _
        "GetConfiguration hresult();" & _
        "Start hresult();" & _
        "Stop hresult();" & _
        "CreateDomain hresult();" & _
        "GetDefaultDomain hresult(ptr*);" & _
        "EnumDomains hresult();" & _
        "NextDomain hresult();" & _
        "CloseEnum hresult();" & _
        "CreateDomainEx hresult();" & _
        "CreateDomainSetup hresult();" & _
        "CreateEvidence hresult();" & _
        "UnloadDomain hresult();" & _
        "CurrentDomain hresult();"
Global Const $sCLSID_CLRMetaHost = "{9280188d-0e8e-4867-b30c-7fa83884e8de}"
Global Const $sIID_ICLRMetaHost = "{d332db9e-b9b3-4125-8207-a14884f53216}"
Global Const $sIID_ICLRRuntimeInfo = "{BD39D1D2-BA2F-486a-89B0-B4B0CB466891}"
Global Const $sCLSID_CLRRuntimeHost = "{90F1A06E-7712-4762-86B5-7A5EBA6BDB02}"
Global Const $sIID_ICLRRuntimeHost = "{90F1A06C-7712-4762-86B5-7A5EBA6BDB02}"

Global $tCLSID_CLRMetaHost = _WinAPI_CLSIDFromString($sCLSID_CLRMetaHost)
Global $tIID_ICLRMetaHost = _WinAPI_CLSIDFromString($sIID_ICLRMetaHost)
Global $tIID_ICLRRuntimeInfo = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeInfo)
Global $tCLSID_CLRRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CLRRuntimeHost)
Global $tIID_ICLRRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICLRRuntimeHost)


Global Const $sCLSID_CorRuntimeHost = "{CB2F6723-AB3A-11D2-9C40-00C04FA30A3E}"
Global Const $sIID_ICorRuntimeHost = "{CB2F6722-AB3A-11D2-9C40-00C04FA30A3E}"
Global $tCLSID_CorRuntimeHost = _WinAPI_CLSIDFromString($sCLSID_CorRuntimeHost)
Global $tIID_ICorRuntimeHost = _WinAPI_CLSIDFromString($sIID_ICorRuntimeHost)

Local $hMSCorEE = DllOpen("MSCorEE.DLL")
Local $aRet = DllCall($hMSCorEE, "long", "CLRCreateInstance", "struct*", $tCLSID_CLRMetaHost, "struct*", $tIID_ICLRMetaHost, "ptr*", 0)

If $aRet[0] = $S_OK Then
    Local $pClrHost = $aRet[3]
    Local $oClrHost = ObjCreateInterface($pClrHost, $sIID_ICLRMetaHost, $sTag_CLRMetaHost)
    ConsoleWrite(">oClrHost: " & IsObj($oClrHost) & @CRLF)

    Local $sNETFrameworkVersion = "v4.0.30319"
    Local $tCLRRuntimeInfo = DllStructCreate("ptr")
    $oClrHost.GetRuntime($sNETFrameworkVersion, $tIID_ICLRRuntimeInfo, DllStructGetPtr($tCLRRuntimeInfo))
    Local $pCLRRuntimeInfo = DllStructGetData($tCLRRuntimeInfo, 1)
    ConsoleWrite(">pCLRRuntimeInfo: " & $pCLRRuntimeInfo & @CRLF)

    Local $oCLRRuntimeInfo = ObjCreateInterface($pCLRRuntimeInfo, $sIID_ICLRRuntimeInfo, $sTag_CLRRuntimeInfo)
    ConsoleWrite(">oCLRRuntimeInfo: " & IsObj($oCLRRuntimeInfo) & @CRLF)
    Local $isIsLoadable = 0
    $oCLRRuntimeInfo.IsLoadable($isIsLoadable)
    ConsoleWrite(">IsLoadable: " & $isIsLoadable & @CRLF)

    If $isIsLoadable Then
        Local $tCLRRuntimeHost = DllStructCreate("ptr")
        $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CLRRuntimeHost), DllStructGetPtr($tIID_ICLRRuntimeHost), DllStructGetPtr($tCLRRuntimeHost))
        Local $pCLRRuntimeHost = DllStructGetData($tCLRRuntimeHost, 1)
        ConsoleWrite(">pCLRRuntimeHost: " & $pCLRRuntimeHost & @CRLF)
        Local $oCLRRuntimeHost = ObjCreateInterface($pCLRRuntimeHost, $sIID_ICLRRuntimeHost, $sTag_CLRRuntimeHost)
        ConsoleWrite(">oCLRRuntimeHost: " & IsObj($oCLRRuntimeHost) & @CRLF)

        Local $iRet = 0
        $oCLRRuntimeHost.Start()
        ;$oCLRRuntimeHost.ExecuteInDefaultAppDomain(@ScriptDir & "\C#Library.dll", "ClassLibraryDemo.ClassDemo", "NetMsgBox", "AutoIt Rocks!!! " & @CRLF & "Danyfirex does too lol!!!", $iRet)
        ;ConsoleWrite(">Ret: " & $iRet & @CRLF)

        Local $tCorRuntimeHost = DllStructCreate("ptr")
        $oCLRRuntimeInfo.GetInterface(DllStructGetPtr($tCLSID_CorRuntimeHost), DllStructGetPtr($tIID_ICorRuntimeHost), DllStructGetPtr($tCorRuntimeHost))
        Local $pCorRuntimeHost = DllStructGetData($tCorRuntimeHost, 1)
;~      ConsoleWrite("$pCorRuntimeHost = " & $pCorRuntimeHost & @CRLF)

        Local $oCorRuntimeHost = ObjCreateInterface($pCorRuntimeHost, $sIID_ICorRuntimeHost, $sTag_ICorRuntimeHost)
        ConsoleWrite("IsObj( $oCorRuntimeHost ) = " & IsObj($oCorRuntimeHost) & @CRLF)

        $oCorRuntimeHost.Start()

        Local $pAppDomain = 0
        $oCorRuntimeHost.GetDefaultDomain($pAppDomain)
        ConsoleWrite("$pAppDomain = " & Ptr($pAppDomain) & @CRLF)

        Local $oAppDomain = ObjCreateInterface($pAppDomain, $sIID_AppDomain, $sTag_AppDomain)
        ConsoleWrite("IsObj( $oAppDomain ) = " & IsObj($oAppDomain) & @CRLF)
        Local $oObject = 0
        $oAppDomain.CreateInstance("mscorlib", "System.Collections.Stack", $oObject)
        ConsoleWrite("IsObj($oObject): " & IsObj($oObject) & @CRLF)
        Local $oStack = $oObject.Unwrap
        If IsObj($oStack) Then
        ConsoleWrite("IsObj($oStack): " & IsObj($oStack) & @CRLF)
        $oStack.Push("+Bye Bye!!! ^.^")
        $oStack.Push("+I'm a Stack Object!!!")
        $oStack.Push("+AutoIt Rocks!!!")
        ConsoleWrite($oStack.Pop & @CRLF)
        ConsoleWrite($oStack.Pop & @CRLF)
        ConsoleWrite($oStack.Pop & @CRLF)
        $oStack = 0
        EndIf

    EndIf

EndIf

DllClose($hMSCorEE) ;free


Func _WinAPI_CLSIDFromString($sGUID)
    $tGUID = DllStructCreate('ulong Data1;ushort Data2;ushort Data3;byte Data4[8]')
    $iRet = DllCall('ole32.dll', 'uint', 'CLSIDFromString', 'wstr', $sGUID, 'ptr', DllStructGetPtr($tGUID))
    If (@error) Or ($iRet[0]) Then
        Return SetError(@error, @extended, 0)
    EndIf
    Return $tGUID
EndFunc   ;==>_WinAPI_CLSIDFromString

Updated. 

Saludos

Edited by Danyfirex
Code Updated changed some datatypes
Link to comment
Share on other sites

  • Melba23 pinned and unpinned this topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...