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
ivanez26
  • Members
  • 1 posts
  • Last active: Mar 02 2014 02:40 AM
  • Joined: 01 Mar 2014

Greetings!

 

I'm new to CLR and it's hard to understand for me… Could you please tell me, what should I write in the ahk script code in order to be able to do DllCall of the dll file which is made in Microsoft Visual Studio 2010?

 

The code of the dll file is the following:

Function hypotenuse(ByVal side1 As Single, ByVal side2 As Single) As Single
    Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))
End Function

Best regards,

Ivan



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

I recently wanted to call an AutoHotkey function when a row was selected in xptable, and since both xptable and events are mentioned on this thread I thought I'd post the example since I had a lot of trouble figuring it out even with perlguy's example.

 

Example of xptable and events:

#Persistent

xptable_reference := CLR_LoadLibrary("lib\dll\XPTable.dll")
xp_table := xptable_initialize(xptable_reference)

; Create Columns/Rows for Table
xptable_Add_Column_And_Rows(xptable_reference, xp_table)

; event handling
xptable_EventHandler_initialize(xptable_reference, xp_table)




Gui, Add, Text, x22 y390 w80 h20 , Test:

Gui, 1:Default
Gui, Submit
hwnd := xp_table.Handle
Gui, +LastFound
DllCall("SetParent", UInt, hwnd, UInt, WinExist())
Gui, Show, x133 y168 h493 w426, XPTable Event Example
return


xptable_initialize(xptable_reference)
{
  xp_table := CLR_CreateObject(xptable_reference,"XPTable.Models.Table")
  xp_table.ColumnModel := CLR_CreateObject(xptable_reference,"XPTable.Models.ColumnModel")
  xp_table.TableModel := CLR_CreateObject(xptable_reference,"XPTable.Models.TableModel")
  
  ; Table Size
  xp_table.Left := 10
  xp_table.Top := 10
  xp_table.Width := 400
  xp_table.Height := 350
  return xp_table
}

xptable_Add_Column_And_Rows(xptable_reference, xp_table)
{
  ; Create Columns for Table
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.CheckBoxColumn", "Column 1", 88))
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.TextColumn", "Column 2", 205))
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.TextColumn", "Column 3", 55))
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.TextColumn", "Column 4", 48))
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.TextColumn", "Column 5", 200))
  xp_table.ColumnModel.Columns.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.TextColumn", "Column 6", 0))
  
  
  ; Add rows
  Loop 10
  {
    row := A_Index - 1
    xp_table.TableModel.Rows.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Row"))
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "", true)) ; checkbox column
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "Test Columna " . row)) ; text column
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "" + 1)) ; text column
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "" + 2)) ; text column
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "" + "Test Columnb " . row)) ; text column
    xp_table.TableModel.Rows.Item(row).Cells.Add(CLR_CreateObject(xptable_reference, "XPTable.Models.Cell", "" + 3)) ; text column
  }
}

xptable_EventHandler_initialize(xptable_reference, xp_table)
{
  c# =
  (
      using System;
      using System.Runtime.InteropServices;
      
      class Xptable_Event_Handler_Helper {
          public Delegate Delegate4FuncPtr(uint ptr, Type t) {
              return Marshal.GetDelegateForFunctionPointer((IntPtr)ptr, t);
          }
      }
  )
  ; Get typeof(System.EventHandler)
  typeof_SelectionEventHandler := xptable_reference.GetType_2("XPTable.Events.SelectionEventHandler")
  
  ; Create Event Handling object
  obj := CLR_CompileC#(c#, "System.dll").CreateInstance("Xptable_Event_Handler_Helper")
  
  ; Create a .NET Delegate for the xptable_event_handler() callback.
  net_Delegate_for_callback := obj.Delegate4FuncPtr( RegisterCallback("xptable_event_handler"), typeof_SelectionEventHandler)
  
  ; Register the event handler. (Events export add_event() and remove_event()).
  xp_table.add_SelectionChanged(COM_Parameter(13,net_Delegate_for_callback))
}

xptable_event_handler(vt, junk1, sender, junk2, eventArgs)
{
  global xp_table
  static last_selection
  Loop % xp_table.TableModel.rows.Count
  {
    this_index := A_Index - 1
    if((xp_table.TableModel.rows.Item(this_index).SelectedCellCount != 0) && (this_index != last_selection))
    {
	  last_selection := this_index
	  msgbox row selected: %this_index%`nNote table is zero-based
    }
  }
}

Note: I'm pretty sure you need COM.ahk, for the COM_Parameter() as I found ComObjParameter() didn't work.

You will also need Lexikos's CLR.ahk for the XPTable.dll and compiling the c#.

I have both COM.ahk and CLR.ahk in my lib folder, and I'm running the script from a folder up from the lib, i.e. some_dir/this_script/lib


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


joviermark
  • New members
  • 1 posts
  • Last active: Aug 10 2015 06:01 AM
  • Joined: 10 Aug 2015

More about.....Common Language Runtime

 

Mark



evilc
  • Members
  • 340 posts
  • Last active: Oct 27 2015 11:07 PM
  • Joined: 17 Nov 2005

I wanted to check out XPTable, but I cannot get it to work.

 

I get the following error:

test.ahk (14) : ==> Call to nonexistent function.
     Specifically: COM_Invoke(table,"ColumnModel=","+" columnModel)
>Exit code: 2    Time: 0.1798


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
That script is designed to utilize COM.ahk, hence COM_Invoke. It was written before COM support was added to AutoHotkey. That line does the equivalent of table.ColumnModel := columnModel, except that table and columnModel are pointers.

djvj
  • Members
  • 70 posts
  • Last active: Jul 22 2016 02:54 AM
  • Joined: 19 Sep 2010

Have you tested this with AHK_H v2 because I get all sorts of errors trying to include CLR_L with a script running with it?

 

If not, is there another way to use .net framework or do I need to stick with a different version AHK? I need H so I can use the multithreaded dll.

 

If you do a compare to this to see the changes, I was able to get it to load with an Include:

 

http://pastebin.com/G1BrL6p6