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've updated the links.

panofish
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007

First Example
The following is a translation of some sample code accompanying XPTable. The sample code (and XPTable itself) can be found at:
XPTable - .NET ListView meets Java's JTable (@ The Code Project)
I haven't yet been able to hook events of .NET objects...


I tried the xptable example, but I assume it doesn't work because I am using autohotkey_L and CLR_L.
Does anyone have an autohotkey_L version of this XPTABLE example code for complex listviews?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I don' thave XPTable atm. It shouldn't be difficult to translate the COM_Invoke calls:
COM_Invoke(table,"ColumnModel=","+" columnModel)
table.ColumnModel := columnModel

COM_Invoke_(table,"TableModel=",13,tableModel)
table.TableModel := ComObjParam(13, tableModel)

COM_Invoke_(Columns,"Add", 13,col1:=CLR_CreateObject(pasm,"XPTable.Models.TextColumn",8,"Text"))
Columns.Add(ComObjParam(13, col1:=CLR_CreateObject(pasm, "XPTable.Models.TextColumn", "Text")))

etc.

COM_Release(var) can be removed or replaced with var := "".

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Hey Lexikos, trying to familiarize myself with CLR_L.
I'm attempting to adapt the C# compile example here for native COM in _L.

1st test is returning: Error: 0x800401F3 - Invalid class string
---> 020: obj := ComObjCreate(asm, "Foo")
#Include C:\Program Files\AutoHotkey\Lib\CLR.ahk
c# =
(
	using System.Windows.Forms;
	class Foo {
		public void Test() {
			MessageBox.Show("Hello, world, from C#!");
		}
	}
)
; CLR_Start() ; as per docs this is not needed ?

asm := CLR_CompileC#(c#, "System.dll | System.Windows.Forms.dll")
obj := ComObjCreate(asm, "Foo")
ComObjQuery(obj, "Test")
ObjRelease(obj)
Also not sure of the substitute for COM_Invoke() ( I tried ComObjQuery() ).
Do the libs compile to the script dir, or somewhere else? I will also ask @ #ahk

Thanks for any info..

Posted Image

don't duplicate, iterate!


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Just use obj.Test(), and get rid of the call to ObjRelease() (it is intended to be used with raw object pointers, which is not the case here).

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Thanks for clarifying the approach fincs Posted Image..

hrmm looks like the libs aren't compiling. I'm still receiving the same error.
I tried debugging an asm value, it returns blank.

Any ideas of how to get it working? Much appreciated as always my friend.
In the meantime I'm going to try watching AHk proc to see if anything is created.

edit: Found this:

AutoHotkey.exe CreateFile C:\Program Files\AutoHotkey\System.dll NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
AutoHotkey.exe CreateFile C:\Program Files\AutoHotkey\System\System.dll PATH NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
AutoHotkey.exe CreateFile C:\Program Files\AutoHotkey\System.exe NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

AutoHotkey.exe CreateFile C:\Program Files\AutoHotkey\System\System.exe PATH NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a

wait.. I think I'm trying to instantiate the wrong object type....

That was it.

Posted Image

don't duplicate, iterate!


magusneo
  • Members
  • 51 posts
  • Last active: May 10 2014 03:15 PM
  • Joined: 30 May 2012

How can I reference c/c++ dll file in c# code which will be compiled by CLR_CompileC#( Code, References [, AppDomain, FileName, CompilerOptions ] )?

example:

c#=
(
using wrapper //It is .net wrapper of a c++.dll
    using System.Windows.Forms;
    class Foo {
        public void Test() {
            MessageBox.Show("Hello, world, from C#!");
        }
    }
)

CLR_CompileC#(c#, "System.dll | System.Windows.Forms.dll | wrapper.dll")

 

The code wii give me an error that wrapper.dll can't find dependencies.

If I use "CLR_CompileC#(c#, "System.dll | System.Windows.Forms.dll | wrapper.dll | C++.dll")",It will return an error that C++.dll format is unsupported.



Kibo
  • Members
  • 36 posts
  • Last active: Aug 09 2015 07:56 PM
  • Joined: 14 Mar 2012

Here is some code to get Hardwareinformation:


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

more about this can be found here: http://www.codeproje....aspx?aid=17973



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

I don' thave XPTable atm. It shouldn't be difficult to translate the COM_Invoke calls:

COM_Invoke(table,"ColumnModel=","+" columnModel)
table.ColumnModel := columnModel

COM_Invoke_(table,"TableModel=",13,tableModel)
table.TableModel := ComObjParam(13, tableModel)

COM_Invoke_(Columns,"Add", 13,col1:=CLR_CreateObject(pasm,"XPTable.Models.TextColumn",8,"Text"))
Columns.Add(ComObjParam(13, col1:=CLR_CreateObject(pasm, "XPTable.Models.TextColumn", "Text")))

etc.
COM_Release(var) can be removed or replaced with var := "".

 

 

I had a good amount of trouble figuring it out so I'll try to save people some time!(Stupid me :) )

#IfWinActive
!z::
xptables := CLR_LoadLibrary("lib\dll\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

NOTE: There has been an updated version of xptables see here.


Edited by lifeweaver, 12 December 2013 - 01:33 AM.

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


Person93
  • Members
  • 443 posts
  • Last active: Feb 11 2014 12:07 AM
  • Joined: 26 Jan 2012

Hi,

 

I am trying to use a .NET library I found by using this script, but I don't think I'm using the CLR functions properly.

#NoEnv
#Persistent
#Include e:\Documents\AutoHotkey\Lib\
#Include COM.ahk
#Include CLR.ahk

BtLibLoc := "e:\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll"

CLR_Start()

BT := CLR_LoadLibrary(BtLibLoc)

obj := CLR_CreateObject(BT, "InTheHand.Net.BluetoothAddress(BluetoothAddress)") ;I remved my actual bluetooth address before posting fr security reasons

ListVars

Here is a link to the documentation about the class that I am trying to invoke.

http://inthehand.com...oothAddress.htm



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

Hi Person93,

 

First question, have you tried the xptables example to insure using the library works for you?

 

 

I don't think you need to call CLR_Start(), nor do you need the COM.ahk library, unless your using AutoHotkey Basic.

Also this tool greatly helped me, although since you have the documentation you might not need it.

 

Also as Lexikos mentions in the topic, checking if something is an object can really help to tell you your calling something correctly. ( 1 is good)

msgbox % IsObject(YOUR_OBJECT_HERE)

Also I think your creating the object incorrectly, see the example I posted above:

CLR_CreateObject(xptables, "XPTable.Models.TextColumn", "MyTextColumn")

I'm initializing a new 'TextColumn' object with the value of 'MyTextColumn' so your call might look like:

obj := CLR_CreateObject(BT, "InTheHand.Net.BluetoothAddress", "YOUR_BLUE_TOOTH_ADDRESS")

Try this Code:

#NoEnv
#Persistent
#Include e:\Documents\AutoHotkey\Lib\
#Include COM.ahk
#Include CLR.ahk

BtLibLoc := "e:\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll"

BT := CLR_LoadLibrary(BtLibLoc)
msgbox % IsObject(BT)

obj := CLR_CreateObject(BT, "InTheHand.Net.BluetoothAddress", "YOUR_BLUE_TOOTH_ADDRESS")
msgbox % IsObject(obj)

My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


Person93
  • Members
  • 443 posts
  • Last active: Feb 11 2014 12:07 AM
  • Joined: 26 Jan 2012

Hi lifeweaver,

First off, thanks for taking the time to help me.

 

I prefer to use ListVars instead of having a bunch if message boxes if circumstances allow.

 

Anyway, when I ran your code, I did not get any objects, but when I ran my code 'BT' was a COM object.

 

I think I may have downloaded the wrong file from code project.  Can you put up a download link to the correct file?

 

I re-read the documentation for the InTheHand library and I realized that I may not have been calling the constructor function at all.

 

Here is my modified code;

#NoEnv
#Persistent
#Include e:\Documents\AutoHotkey\Lib\CLR.ahk

BtLibLoc := "e:\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll"


BT := CLR_LoadLibrary(BtLibLoc)

VarSetCapacity(BluetoothAddrInt, 64, 0)
NumPut(0x001122334455, &BluetoothAddrInt, 0, "Int64")

BlueToothAddr := CLR_CreateObject(BT, "InTheHand.Net.BluetoothAddress.BluetoothAddress", BluetoothAddrInt)

ListVars

Also, here is the part of the XML that has the function that I am trying to call;

<member name="M:InTheHand.Net.BluetoothAddress.#ctor(System.Int64)">


<summary>
Initializes a new instance of the 
<see cref="T:InTheHand.Net.BluetoothAddress"/>
class with the specified address. 
</summary>


<param name="address">

<see cref="T:System.Int64"/>
representation of the address.
</param>

</member>


lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

Here is how to get the dll.

Note: This is the updated url, xptables was updated around 2007, original around 2005

  1. Go here
  2. click download demo project
  3. The dll is in the file in the XPTable_demo/References folder

 

So you've tried your modified code and it didn't work?


My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm


Person93
  • Members
  • 443 posts
  • Last active: Feb 11 2014 12:07 AM
  • Joined: 26 Jan 2012

Here is what shows up in ListVars when I run my modified code

Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
BlueToothAddr[0 of 0]:  
BluetoothAddrInt[0 of 32]: 䑕∳
BT[Object]: 0x000000000208D630 <= ComObject(0x0009, 0x5FFBA0)
BtLibLoc[56 of 63]: e:\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll
ErrorLevel[1 of 3]: 0

It seems that the library is loading properly but the function is not returning anything.

 

 

 

I downloaded the DLL file from your link and ran this code;

#NoEnv
#Persistent
#Include e:\Documents\AutoHotkey\Lib\CLR.ahk

XpT := CLR_LoadLibrary("e:\XPTable_demo\References\XPTable.dll")

table1 := CLR_CreateObject(XpT,"XPTable.Models.Table")

ListVars

The listvars showed this;

Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
ErrorLevel[1 of 3]: 0
table1[0 of 0]:  
XpT[0 of 0]:  

It seems that the library is not loading at all!



lifeweaver
  • Members
  • 868 posts
  • Last active: Jan 25 2016 01:29 AM
  • Joined: 04 Mar 2013

How strange!

 

Well the only differences I can see:

  • My A_AhkVersion is 1.1.13.01
  • My A_IsUnicode is 1 (true)
  • I also have CLR.ahk in my lib folder, I don't include it.

On the subject of the bluetooth dll, I found a useful site you might be interested in, if you've not found it already.

I can't even get the dll to load on my system, lol similar to your problem with xptables.


My script runs in A_AhkVersion:=1.1.22.07, get the latest version at http://ahkscript.org/download/

Check out this AutoHotkey tutorial: http://ahkscript.git...o/AHK_Tutorial/

Read the documentation: http://ahkscript.org.../AutoHotkey.htm