Indeed, supposedly you can do list.Cast<object>().ToArray()... But I would assume there must be a way to extract the variables directly...evilC wrote:I couldn't work out if it was possible, in the end I just returned an array of strings
.NET Framework Interop (CLR, C#, VB)
Re: .NET Framework Interop (CLR, C#, VB)
Re: .NET Framework Interop (CLR, C#, VB)
sancarn wrote:How do you parse a List object from CLR to AHK?
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Include %A_ScriptDir%\CLR.ahk
code =
(
Imports System
Imports System.Collections.Generic
Class MainClass
Function TestList() as List(Of String)
return New List(Of String)(New String(){"This","is","a","list"})
end function
end class
)
asm := CLR_CompileVB(code, "System.dll")
obj := CLR_CreateObject(asm,"MainClass")
list := obj.TestList()
ICollection := ComObject(9, ComObjQuery(list, "{DE8DB6F8-D101-3A92-8D1C-E72E5F10E992}"), 1)
IList := ComObject(9, ComObjQuery(ICollection, "{7BCFA00F-F764-3113-9140-3BBD127A96BB}"), 1)
Loop % ICollection.Count()
MsgBox % IList.Item(A_Index - 1)
Re: .NET Framework Interop (CLR, C#, VB)
Well I never! This is great to know!qwerty12 wrote:sancarn wrote:How do you parse a List object from CLR to AHK?Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. #Include %A_ScriptDir%\CLR.ahk code = ( Imports System Imports System.Collections.Generic Class MainClass Function TestList() as List(Of String) return New List(Of String)(New String(){"This","is","a","list"}) end function end class ) asm := CLR_CompileVB(code, "System.dll") obj := CLR_CreateObject(asm,"MainClass") list := obj.TestList() ICollection := ComObject(9, ComObjQuery(list, "{DE8DB6F8-D101-3A92-8D1C-E72E5F10E992}"), 1) IList := ComObject(9, ComObjQuery(ICollection, "{7BCFA00F-F764-3113-9140-3BBD127A96BB}"), 1) Loop % ICollection.Count() MsgBox % IList.Item(A_Index - 1)
I myself was looking into [this for a possible solution](https://maul-esel.github.io/tutorials/C ... faces.html) but couldn't find the header file for ICollection. By the way... How did you get/find the IID for ICollection and IList?
EDIT:
I didn't realize that all interfaces and IIDs are stored in HKEY_CLASSES_ROOT\Interface\!! This is really useful, though I need to find a quicker way of searching for it Perhaps a function could even be created to query the registry for an interface given by name!
Edit 2:
Something along the lines of this:
Code: Select all
findIID(name){
Loop, Reg, HKEY_CLASSES_ROOT\Interface\, KV
{
RegRead, interface, HKEY_CLASSES_ROOT\Interface\%A_LoopRegName%
if (interface = name)
return A_LoopRegName
}
}
Last edited by sancarn on 23 Jun 2017, 08:32, edited 3 times in total.
Re: .NET Framework Interop (CLR, C#, VB)
I took your idea of using ComObjType, but with CLSID as Param2 instead and got given a GUID for Object. When Googling it, the results indicated I could find what I was looking for in mscorlib.tlb, so I used oleview.exe on said file. I then took a guess on IList and ICollection being the appropriate interfaces...sancarn wrote:By the way... How did you get/find the IID for ICollection and IList?
Re: .NET Framework Interop (CLR, C#, VB)
Anyone got any ideas why this code throws an error?
Is inheritance not supported?
Is inheritance not supported?
Code: Select all
c# =
(
class Foo : Bar {
public int Test() {
return 2;
}
}
class Bar {
}
)
asm := CLR_CompileC#(c#)
obj := CLR_CreateObject(asm, "Foo")
msgbox % obj.Test()
return
Re: .NET Framework Interop (CLR, C#, VB)
Add the public modifier to your classes.
Re: .NET Framework Interop (CLR, C#, VB)
Hmm, I swear the original code I was using had public modifiers for all the classes, but I could be wrong. Will double-check tonight, thanks!
Re: .NET Framework Interop (CLR, C#, VB)
Yeah, it wasn't just the lack of public, I rewrote the code as a simple AHK script using dynamic compilation, and obviously I simplified too far...
I tried to repro with another dynamically compiled sample, but I could not. Upon further investigation, there appears to be a difference in behavior between pre-compiled DLLs and dynamically compiled code.
This code works fine, however if you make a project in VS using the same C# code and compile, then calling obj.innerOne.Test() throws an error.
In the compiled version, if you change public class InnerOne : InnerBase to public class InnerOne, then the problem goes away.
Most odd.
SLN attached
I tried to repro with another dynamically compiled sample, but I could not. Upon further investigation, there appears to be a difference in behavior between pre-compiled DLLs and dynamically compiled code.
Code: Select all
#SingleInstance force
#NoEnv
#include <CLR>
c# =
(
public class Outer
{
public InnerOne innerOne = new InnerOne();
public class InnerOne : InnerBase
{
public string Test()
{
return "OK";
}
}
public class InnerBase
{
}
}
)
asm := CLR_CompileC#(c#)
obj := CLR_CreateObject(asm, "Outer")
msgbox % obj.innerOne.Test()
return
^Esc::
ExitApp
In the compiled version, if you change public class InnerOne : InnerBase to public class InnerOne, then the problem goes away.
Most odd.
SLN attached
- Attachments
-
- ClrTest.zip
- (15.2 KiB) Downloaded 319 times
Re: .NET Framework Interop (CLR, C#, VB)
My guess is that it has something to do with COM visibility. A quick search turned up some details supporting this:
See Properties\AssemblyInfo.cs.
I think that some methods used by CLR.ahk bypass COM visibility; e.g. because Assembly and Type are COM visible.
- Public classes are COM visible by default.
- Visual Studio overrides this default, making it false.
See Properties\AssemblyInfo.cs.
Code: Select all
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
Re: .NET Framework Interop (CLR, C#, VB)
OK, that would make some sense, so the difference is in the way VS packages the code.
Will give this a try, thanks.
I am thinking that it may make some sense to have some sort of CLR for AHK cribsheet / docs?
eg nuggets of info like this, and maybe some basic patterns to show people how to achieve some common goals (eg how to do callbacks, stuff like the list example from a couple of posts back, etc)
I am happy to contribute, but am conscious that my lack of depth of knowledge in this area may mean some hokey code or bad assumptions, is anyone else willing to pitch in?
Will give this a try, thanks.
I am thinking that it may make some sense to have some sort of CLR for AHK cribsheet / docs?
eg nuggets of info like this, and maybe some basic patterns to show people how to achieve some common goals (eg how to do callbacks, stuff like the list example from a couple of posts back, etc)
I am happy to contribute, but am conscious that my lack of depth of knowledge in this area may mean some hokey code or bad assumptions, is anyone else willing to pitch in?
Re: .NET Framework Interop (CLR, C#, VB)
I can confirm that setting ComVisible to true fixed this problem.
Many thanks, I doubt I would have got there alone
Many thanks, I doubt I would have got there alone
Re: .NET Framework Interop (CLR, C#, VB)
evilC wrote:OK, that would make some sense, so the difference is in the way VS packages the code.
Will give this a try, thanks.
I am thinking that it may make some sense to have some sort of CLR for AHK cribsheet / docs?
eg nuggets of info like this, and maybe some basic patterns to show people how to achieve some common goals (eg how to do callbacks, stuff like the list example from a couple of posts back, etc)
I am happy to contribute, but am conscious that my lack of depth of knowledge in this area may mean some hokey code or bad assumptions, is anyone else willing to pitch in?
This would be extremely handy, I agree.
Would be willing to pitch in but I fear I am more inexperienced than yourself xD
Where to make it? Perhaps on github like JXA cookbook. Somewhere where anyone can just go and edit in some examples and stuff.
Re: .NET Framework Interop (CLR, C#, VB)
Oh, and @Lexikos - is there any chance of building CLR into AHK?
Re: .NET Framework Interop (CLR, C#, VB)
No. I'd rather not have second-rate CLR support built-in, and that's the only kind possible via COM callable wrappers.
Putting that aside, I've said before that I don't even use CLR.ahk, and I have no interest in using .NET. So for me, re-implementing CLR support in a different form with the same limitations would be a complete waste of time.
Putting that aside, I've said before that I don't even use CLR.ahk, and I have no interest in using .NET. So for me, re-implementing CLR support in a different form with the same limitations would be a complete waste of time.
Re: .NET Framework Interop (CLR, C#, VB)
Care to elaborate on the "second-rate" comment?
Re: .NET Framework Interop (CLR, C#, VB)
Haven't you tried calling overloaded or static methods? There are also enumerations, static fields, and probably other things that require Reflection or some intermediary .NET code.
Though for static methods, one can possibly wrap the Type in a dynamic object.
Though for static methods, one can possibly wrap the Type in a dynamic object.
-
- Posts: 9
- Joined: 04 Jul 2017, 06:41
Re: .NET Framework Interop (CLR, C#, VB)
Hi, I'm trying to launch a script which #includes CLR.ahk and I get an error "Functions cannot contain functions" at line 11: CLR_LoadLibrary(AssemblyName, AppDomain=0)
I'm using AHK1.1.26 on 64bit windows.
Any idea?
Thanks!
I'm using AHK1.1.26 on 64bit windows.
Any idea?
Thanks!
Re: .NET Framework Interop (CLR, C#, VB)
You have mismatched braces in your script, or you have used #include inside a function. (This is not specific to CLR.ahk; it would happen with any file containing a function definition.)
-
- Posts: 9
- Joined: 04 Jul 2017, 06:41
Re: .NET Framework Interop (CLR, C#, VB)
Big Thanks!lexikos wrote:You have mismatched braces in your script, or you have used #include inside a function. (This is not specific to CLR.ahk; it would happen with any file containing a function definition.)
I didn't believe it but you where right, kind-of, one of the imported scripts was being treated as commented out.
Re: .NET Framework Interop (CLR, C#, VB)
Thank you very much lexikos, you saved my day with this insane .ahk !
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: CoffeeChaton, effel, Google [Bot] and 30 guests