Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

.NET Framework Interop


  • Please log in to reply
155 replies to this topic
Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I wrapped it for symmetry. I don't suppose it has much use.

It is typically unnecessary to call the Stop method, because the code stops executing when the process exits.



fasto
  • Members
  • 12 posts
  • Last active: Dec 19 2009 06:18 PM
  • Joined: 22 May 2009
zip library for dotnet from http://dotnetzip.codeplex.com/

CLR_Start() 

Zip := CLR_LoadLibrary("Ionic.Zip.dll")
ZipFile  := CLR_CreateObject(Zip,"Ionic.Zip.ZipFile")

com_invoke(zipfile, "password=", "1234")
com_invoke(zipfile, "addfile", "Contents.txt")
com_invoke(zipfile, "adddirectory", A_scriptdir "\DotNetZip-v1.8")

com_invoke(Zipfile, "name=", A_scriptdir "\xxx.zip")
com_invoke(zipfile,"save")

com_release(zipFile)
msgbox, compressed done!

zipFile  := CLR_CreateObject(Zip,"Ionic.Zip.ZipFile")
com_invoke(zipfile, "password=", "1234")
com_invoke(zipFile, "initialize", A_scriptdir "\xxx.zip" )
filecreatedir, %A_scriptdir%\xxx
com_invoke(zipfile,"extractall", A_scriptdir "\xxx") 

msgbox, extracted in xxx folder

com_release(zipFile)
com_release(Zip)

exitapp



the script is working ok, but unfortunatley problemes are

1)
it's not compatible with COM_L.
When calling CLR_createobject, com_l throws an error message.

2)

And I notice you made an explicit statment that the "static" method can't be called, which I don't know at all.
but from what i have tested so far, I guess that it means like system.Net.Dns class's gethostname method
'cause dns class has no contruction method so i couldn't create object for it and therefore couldn't call Gethostname method.
or is it possible to call dns.gethostname method with CLR lib?


3)
and finally could it be possible to make dot syntax possible as in Autohotkey_L and Com_L

possible or not, thanks for the script and Autohotkey_L.
Com_L and autohotkey_L combination is really fantastic.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

it's not compatible with COM_L.

COM_L has been updated to fix this issue.

When calling CLR_createobject, com_l throws an error message.

I'm going to assume you mean that CLR.ahk was throwing an error mesage about number of parameters passed to COM_CreateObject. If that's not correct, please give more detail.

And I notice you made an explicit statment that the "static" method can't be called,

Static methods can't be called with COM_Invoke because it requires an object to invoke. Static methods can be called, and in fact are called internally by CLR.ahk. It is difficult enough that I would recommend using embedded C# or VB in your scripts (as in the MessageBox.Show example).

and finally could it be possible to make dot syntax possible as in Autohotkey_L and Com_L

You may use COM_Enwrap to wrap any pointer which works with COM_Invoke, including those returned by CLR_CreateObject.

I tested briefly with the following (and updated COM_L):
CLR_Start()
lib := CLR_LoadLibrary("mscorlib")
obj := COM_Enwrap(CLR_CreateObject(lib,"System.Object"))
COM_Release(lib)

MsgBox % obj.ToString()
A "select few" types have ProgID's (keys in HKEY_CLASSES_ROOT), so can be simply instantiated with COM_CreateObject:
obj := COM_CreateObject("System.Collections.ArrayList")
MsgBox % obj.ToString()


fasto
  • Members
  • 12 posts
  • Last active: Dec 19 2009 06:18 PM
  • Joined: 22 May 2009

COM_L has been updated to fix this issue.


At first I couldn't decide whether it is appropritate to ask here or COM Thread.
But since I had some other questions about CLR, I came here. Thanks for the request.

I would recommend using embedded C# or VB in your scripts (as in the MessageBox.Show example)


OK, I tried that. It's working flawlessly though it's a bit tricky for me.

You may use COM_Enwrap to wrap any pointer which works with COM_Invoke.


I saw that function name before but until now i didn't know what the heck it is for. :lol:
Now I get it!

Some silly requests and sweet, detailed guides from the master!

Thanks, Lexikos

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
How do I enumerate a System.Object[]? I have tried this (using COM_L):
Loop, % array.Length
	myArray%A_Index% := array.GetValue(A_Index-1)
But it doesn't work...

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Arrays must be in SafeArray format.
Source: post



fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Thanks, Lexikos.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
CLR_CompileAssembly wasn't working in COM_L/U, so I've uploaded a new version of CLR with the following changes:
[*:1y5uk178]AutoHotkey_L or AutoHotkeyU required. (However, the old version is still available.)
[*:1y5uk178]CLR_LoadLibrary, CLR_CreateObject, and CLR_CompileC#/VB return a COM wrapper object.
[*:1y5uk178]It should not be necessary to call CLR_Start explicitly.Note that in simple cases, Assembly.CreateInstance(TypeName) can be used in place of CLR_CreateObject:
CorLib := CLR_LoadLibrary("mscorlib")
stk := CorLib.CreateInstance("System.Collections.Stack")
que := CorLib.CreateInstance("System.Collections.Queue")
...
CLR_CompileC#("class c{public void F(){System.Windows.Forms.MessageBox.Show(""Hello, world!"");}}" ,"System.dll|System.Windows.Forms.dll").CreateInstance("c").F()


CyberGeek
  • Members
  • 159 posts
  • Last active: May 10 2013 06:01 PM
  • Joined: 18 Apr 2010
Hi Lexikos and all!

Im fairly new to C# and really want to learn it
(and someday write some dll to use with ahk)

Just for fun, a few days ago i followed a tutorial on how to create a C# DLL. I compiled it with C#2008Express and tried to DllCall it. But it didnt work(returned Errorlevel=-4 Function not found). Even the disasm didnt show any exported functions. I considered that the dll was to use with only C# as a class lib(i tried it with c# and obviously it worked).

So With this CLR for Ahk, will I be able to call functions from C# dlls?

edit: I cant call mscorlib.dll
Posted Image

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

edit: I cant call mscorlib.dll

Post code if you're having difficulty. It sounds like you're still trying to use DllCall, in which case you wouldn't actually be using this script.

Typically you'll need to load an assembly (CLR_LoadLibrary), create an object from it (CLR_CreateObject), then invoke the object (COM_Invoke). Make sure you're using the appropriate version of CLR.ahk for your version of AutoHotkey. That is, CLR for standard AHK or CLR_L for AutoHotkey_L (allows "dot" syntax and automatic cleanup). For instance:
; CLR or CLR_L
CLR_Start()  ; one-time init
mscorlib := CLR_LoadLibrary("mscorlib")
cookie := CLR_CreateObject(mscorlib, "System.Object")
MsgBox % COM_Invoke(cookie, "ToString")
COM_Release(cookie)
; CLR_L only
mscorlib := CLR_LoadLibrary("mscorlib")
cookie := CLR_CreateObject(mscorlib, "System.Object")
MsgBox % cookie.ToString()
cookie := ""  ; optional in this case


CyberGeek
  • Members
  • 159 posts
  • Last active: May 10 2013 06:01 PM
  • Joined: 18 Apr 2010
Thx for the reply,
but how can i use Math.Tan method (http://msdn.microsof...m.math.tan.aspx)

CLR_Start()  ; one-time init 
mscorlib := CLR_LoadLibrary("mscorlib.dll") 
cookie := CLR_CreateObject(mscorlib, "Math.Tan") 
MsgBox % COM_Invoke(cookie, "5") 
COM_Release(cookie)

#Include clr.ahk

Posted Image

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
CLR_CreateObject is only for instantiating a class. Static methods like Math.Tan are difficult to call via COM as it requires...
[*:2252zeql]getting an instance of the Type object representing the type which contains the method,
[*:2252zeql]initializing a SAFEARRAY of VARIANTs containing parameters,
[*:2252zeql]calling the appropriate overload of Type.InvokeMember via a complicated DllCall, and
[*:2252zeql]interpreting the result VARIANT as necessary.For instance, this is a slightly modified (for readability) extract from CLR.ahk:
[color=darkgray]; pApp is a pointer to an AppDomain as returned by CLR_GetDefaultDomain()[/color]
p_App := COM_QueryInterface(pApp,"{05F696DC-2B29-3663-AD8B-C4389CF2A713}")
[color=darkgray]...[/color]
; Get typeof(Assembly) for calling static methods. (p_App->GetType()->Assembly->GetType())
DllCall(NumGet(NumGet(p_App+0)+40),"uint",p_App,"uint*",p_Type)
DllCall(NumGet(NumGet(p_Type+0)+80),"uint",p_Type,"uint*",p_Asm)
COM_Release(p_Type)
DllCall(NumGet(NumGet(p_Asm+0)+40),"uint",p_Asm,"uint*",p_Type)
COM_Release(p_Asm)

; Initialize VARIANTs & SAFEARRAY(VARIANT) for method args.
VarSetCapacity(vArg,16,0), NumPut(8,vArg), NumPut(psLib,vArg,8)
VarSetCapacity(vRet,16,0)
VarSetCapacity(rArgs,24,0), NumPut(1,NumPut(&vArg,NumPut(16,NumPut(1,rArgs))+4))

; Attempt to load from file.  (Call Assembly.LoadFrom().)
hr:=DllCall(NumGet(NumGet(p_Type+0)+228),"uint",p_Type
    ,"uint",COM_SysString(ws,"LoadFrom"),"int",0x118
    ,"uint",0,"int64",1,"int64",0,"uint",&rArgs,"uint",&vRet)
The equivalent C# would be:
Assembly.LoadFrom(sLibrary)
It's generally easier to use C#.
helper_code=
(
class helper {
    public double Tan(double a) {
        return System.Math.Tan(a);
    }
}
)
CLR_Start()
if !asm := CLR_CompileC#(helper_code, "")
    ExitApp
hlp := CLR_CreateObject(asm, "helper")
MsgBox % COM_Invoke(hlp, "Tan", 1.0)
MsgBox % COM_Invoke(hlp, "Tan", 0.5)
...or in this case, AutoHotkey. :p
SetFormat, FloatFast, 0.14  ; for results closer to the above
MsgBox % Tan(1.0)
MsgBox % Tan(0.5)


CyberGeek
  • Members
  • 159 posts
  • Last active: May 10 2013 06:01 PM
  • Joined: 18 Apr 2010
I dont really wanna create Assemblies to use with C#
instead the dll should have exported functions

in Borland Delphi, its much easier
i downloaded Delphi7 and built my first dll using an example and i can use it in ahk with DLLCALL
Posted Image

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

I dont really wanna create Assemblies to use with C#

I wasn't suggesting you do. Any assembly you create with CLR.ahk must be written in C# or VB.Net anyway. My example shows how to use C# in AHK to make static methods more accessible to AHK.

instead the dll should have exported functions

Exporting functions in a way that allows them to be called by DllCall requires a bit of work and does not involve CLR.ahk. If you want to use CLR-based objects (i.e. not just flat functions) in AHK, use CLR.ahk but avoid static methods.

tinku99
  • Members
  • 560 posts
  • Last active: Feb 08 2015 12:54 AM
  • Joined: 03 Aug 2007
Lexikos,
Can we expect to see an IronAhk_L ?