 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Wed Jul 15, 2009 9:45 pm Post subject: |
|
|
I wrapped it for symmetry. I don't suppose it has much use.
| Quote: | | It is typically unnecessary to call the Stop method, because the code stops executing when the process exits. |
|
|
| Back to top |
|
 |
fasto
Joined: 22 May 2009 Posts: 12
|
Posted: Fri Dec 11, 2009 12:46 am Post subject: |
|
|
zip library for dotnet from http://dotnetzip.codeplex.com/
| Code: |
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. |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Sat Dec 12, 2009 2:49 am Post subject: |
|
|
| fasto wrote: | | it's not compatible with COM_L. | COM_L has been updated to fix this issue.
| Quote: | | 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.
| Quote: | | 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).
| Quote: | | 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):
| Code: | 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:
| Code: | obj := COM_CreateObject("System.Collections.ArrayList")
MsgBox % obj.ToString()
|
|
|
| Back to top |
|
 |
fasto
Joined: 22 May 2009 Posts: 12
|
Posted: Sat Dec 12, 2009 3:01 pm Post subject: |
|
|
| Quote: | | 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.
| Quote: | | 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.
| Quote: | | 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.
Now I get it!
Some silly requests and sweet, detailed guides from the master!
Thanks, Lexikos |
|
| Back to top |
|
 |
fincs
Joined: 05 May 2007 Posts: 1159 Location: Seville, Spain
|
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Sun Dec 13, 2009 1:05 am Post subject: |
|
|
|
|
| Back to top |
|
 |
fincs
Joined: 05 May 2007 Posts: 1159 Location: Seville, Spain
|
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Sun Jan 10, 2010 9:55 am Post subject: |
|
|
CLR_CompileAssembly wasn't working in COM_L/U, so I've uploaded a new version of CLR with the following changes:
- AutoHotkey_L or AutoHotkeyU required. (However, the old version is still available.)
- CLR_LoadLibrary, CLR_CreateObject, and CLR_CompileC#/VB return a COM wrapper object.
- 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:
| Code: | CorLib := CLR_LoadLibrary("mscorlib")
stk := CorLib.CreateInstance("System.Collections.Stack")
que := CorLib.CreateInstance("System.Collections.Queue")
...
|
| Code: | CLR_CompileC#("class c{public void F(){System.Windows.Forms.MessageBox.Show(""Hello, world!"");}}" ,"System.dll|System.Windows.Forms.dll").CreateInstance("c").F()
|
|
|
| Back to top |
|
 |
CyberGeek
Joined: 18 Apr 2010 Posts: 159
|
Posted: Thu May 20, 2010 12:44 pm Post subject: |
|
|
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 _________________
 |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Thu May 20, 2010 2:59 pm Post subject: |
|
|
| CyberGeek wrote: | | 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:
| Code: | ; 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)
|
| Code: | ; CLR_L only
mscorlib := CLR_LoadLibrary("mscorlib")
cookie := CLR_CreateObject(mscorlib, "System.Object")
MsgBox % cookie.ToString()
cookie := "" ; optional in this case
|
|
|
| Back to top |
|
 |
CyberGeek
Joined: 18 Apr 2010 Posts: 159
|
Posted: Fri May 21, 2010 9:32 am Post subject: |
|
|
Thx for the reply,
but how can i use Math.Tan method (http://msdn.microsoft.com/en-us/library/system.math.tan.aspx)
| Code: | 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 |
_________________
 |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Fri May 21, 2010 4:38 pm Post subject: |
|
|
CLR_CreateObject is only for instantiating a class. Static methods like Math.Tan are difficult to call via COM as it requires...
- getting an instance of the Type object representing the type which contains the method,
- initializing a SAFEARRAY of VARIANTs containing parameters,
- calling the appropriate overload of Type.InvokeMember via a complicated DllCall, and
- interpreting the result VARIANT as necessary.
For instance, this is a slightly modified (for readability) extract from CLR.ahk:
| Code: | ; pApp is a pointer to an AppDomain as returned by CLR_GetDefaultDomain()
p_App := COM_QueryInterface(pApp,"{05F696DC-2B29-3663-AD8B-C4389CF2A713}")
...
; 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:
| Code: | | Assembly.LoadFrom(sLibrary) |
It's generally easier to use C#.
| Code: | 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.
| Code: | SetFormat, FloatFast, 0.14 ; for results closer to the above
MsgBox % Tan(1.0)
MsgBox % Tan(0.5) |
|
|
| Back to top |
|
 |
CyberGeek
Joined: 18 Apr 2010 Posts: 159
|
Posted: Wed May 26, 2010 11:29 am Post subject: |
|
|
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 _________________
 |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7290 Location: Australia
|
Posted: Wed May 26, 2010 1:25 pm Post subject: |
|
|
| CyberGeek wrote: | | 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.
| Quote: | | 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. |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Sat May 29, 2010 6:02 pm Post subject: ironahk |
|
|
Lexikos,
Can we expect to see an IronAhk_L ? |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|