AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

.NET Framework Interop
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Wed Jul 15, 2009 9:45 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
fasto



Joined: 22 May 2009
Posts: 12

PostPosted: Fri Dec 11, 2009 12:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Sat Dec 12, 2009 2:49 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
fasto



Joined: 22 May 2009
Posts: 12

PostPosted: Sat Dec 12, 2009 3:01 pm    Post subject: Reply with quote

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. Laughing
Now I get it!

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

Thanks, Lexikos
Back to top
View user's profile Send private message
fincs



Joined: 05 May 2007
Posts: 1159
Location: Seville, Spain

PostPosted: Sat Dec 12, 2009 7:28 pm    Post subject: Reply with quote

How do I enumerate a System.Object[]? I have tried this (using COM_L):
Code:
Loop, % array.Length
   myArray%A_Index% := array.GetValue(A_Index-1)

But it doesn't work...
_________________
fincs
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Sun Dec 13, 2009 1:05 am    Post subject: Reply with quote

I wrote:
Arrays must be in SafeArray format.
Source: post
Back to top
View user's profile Send private message Visit poster's website
fincs



Joined: 05 May 2007
Posts: 1159
Location: Seville, Spain

PostPosted: Sun Dec 13, 2009 10:30 am    Post subject: Reply with quote

Thanks, Lexikos.
_________________
fincs
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Sun Jan 10, 2010 9:55 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
CyberGeek



Joined: 18 Apr 2010
Posts: 159

PostPosted: Thu May 20, 2010 12:44 pm    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Thu May 20, 2010 2:59 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
CyberGeek



Joined: 18 Apr 2010
Posts: 159

PostPosted: Fri May 21, 2010 9:32 am    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Fri May 21, 2010 4:38 pm    Post subject: Reply with quote

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. Razz
Code:
SetFormat, FloatFast, 0.14  ; for results closer to the above
MsgBox % Tan(1.0)
MsgBox % Tan(0.5)
Back to top
View user's profile Send private message Visit poster's website
CyberGeek



Joined: 18 Apr 2010
Posts: 159

PostPosted: Wed May 26, 2010 11:29 am    Post subject: Reply with quote

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
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7290
Location: Australia

PostPosted: Wed May 26, 2010 1:25 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
tinku99



Joined: 03 Aug 2007
Posts: 513
Location: Houston, TX

PostPosted: Sat May 29, 2010 6:02 pm    Post subject: ironahk Reply with quote

Lexikos,
Can we expect to see an IronAhk_L ?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
Page 4 of 9

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group