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
tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
This looks very cool. I'll have to give it a try. Thanks

tinku99
  • Members
  • 560 posts
  • Last active: Feb 08 2015 12:54 AM
  • Joined: 03 Aug 2007
Thanks for this useful CLR interop...

I tried a simple modification to get cpp managed code, but
This doesn't seem to work:
cpp =
(
#using <mscorlib.dll>
using namespace System;
void main() {
   Console::WriteLine(S"Hello World using Managed Extensions for C++!");
}
)
CLR_Start()
asm := CLR_CompileCPP(cpp, "System.dll | mscorlib.dll", 0, "cpptest.exe")
return

CLR_CompileCPP(Code, References, pAppDomain=0, FileName="", CompilerOptions="")
{
    return CLR_CompileAssembly(Code, References
        , "System", "Microsoft.VisualC.CppCodeProvider", pAppDomain
        , FileName, CompilerOptions)
}


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
First, the ProviderAssembly parameter of CLR_CompileAssembly must be the assembly which contains the code provider type. CppCodeProvider is in cppcodeprovider.dll, not system.dll.

Second, cppcodeprovider.dll is included in the Visual Studio install directory, under Common7\IDE\PublicAssemblies. ProviderAssembly must be a full path in this case. It is not included by the .NET Framework.

Third, CppCodeProvider

Provides a base class for a C/C++ implementation of CodeDomProvider.

Presumably you should use CppCodeProvider7.

Finally, CppCodeProvider and CppCodeProvider7 do not seem to support CreateCompiler() or CompileAssemblyFromSource(). Either one will set _hResult_ to E_INVALIDARG, despite CreateCompiler's complete lack of arguments. Looks like you're out of luck.

vjc
  • Members
  • 5 posts
  • Last active: Mar 19 2009 07:05 PM
  • Joined: 17 Dec 2008
Thanks for the cool CLR functionality...

I am having trouble passing an external variable to the test sub. I am wanting to pass the filename that the user picks from the fileselectfile call but all I get is a blank message box. I know that it is something simple that I am doing wrong, but for the life of me I just can't see it.

#Include CLR.ahk
#Include COM.ahk


FileSelectFile, filename, 3,, Open the desired Application, Control Builder Files (*.prj)

vb =
(
    Imports System.Windows.Forms
	
    Class Control_Open
		
		Private cb AS CONTROLBUILDERLib.CBOpenIf
		
		Public Sub Test(ByVal filename As String)
			Dim owner As IWin32Window
			
			'cb = NEW CONTROLBUILDERLib.CBOpenIf
			MessageBox.Show(owner, filename)
			
        End Sub
		
    End Class
)

CLR_Start()

asm := CLR_CompileVB(vb, "System.dll | System.Windows.Forms.dll | Interop.CONTROLBUILDERLib.dll")
obj := CLR_CreateObject(asm, "Control_Open")

COM_Invoke_(obj, "Test", "str", "filename")
COM_Release(obj), COM_Release(asm)


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
COM_Invoke_ doesn't accept DllCall type names - it accepts VT_* constants such as VT_BSTR=8.

vjc
  • Members
  • 5 posts
  • Last active: Mar 19 2009 07:05 PM
  • Joined: 17 Dec 2008
Ok, I am still confused...

Is it possible to pass a variable(a string value) to the message box in the test sub so that it displays that string value?

Or should I be using a different method?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
You've used:
COM_Invoke_(obj, "Test", [color=red]"str"[/color], "filename")
Try:
COM_Invoke_(obj, "Test", [color=green]8[/color], "filename")
Clearer now?

vjc
  • Members
  • 5 posts
  • Last active: Mar 19 2009 07:05 PM
  • Joined: 17 Dec 2008
Yes it it clearer now...

I must be missing the sun from QLD.. lol

Thanks for you help Lexikos

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
Lexikos.....
We all know I'm an idiot. My many rambling posts prove it. So given the context of who I am. I hope you will accept my sincere appreciation for CLR which I hadn't previously seen or searched for such. I hadn't even dared hope it was actually possible. I wouldnt have seen it at all except Sean mentioned it here. So as an avid Sean zealot, I looked it up. VB.net was previously my poison of choice and when my employer announced a position using AHK I abandoned it. Thank you I have been half doing with COM and AHK some fairly complex tasks that for the sake of existing documentation is just easier in .net. Now I get the ability to bridge the final gap.
It is a sad little world I exist in when I can use AHK but I cannot download or install Visual Studio Express suite. Even tho it is perfectly acceptable to use COM or any code I can store directly within an ahk file. :? Yea I know it makes absolutely no sense to me either.

Again if you can find it in yourself to accept the gratitude of a rambling idiot
Thank you :D
Never lose.
WIN or LEARN.

Leonidas225
  • Members
  • 12 posts
  • Last active: Sep 24 2009 08:47 AM
  • Joined: 30 Jan 2009
i have some trouble to call this function:
;the c# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SNGEGT
{
    class ICM
    {
private void handFromIndex(int index, int[] cards)
        {
            //for (int i = 0; i < 169; i++)
            //{
            for (int a = 0; a < 13; a++)
            {
                for (int b = 0; b < 13; b++)
                {
                    if (indexArray[a, b] == index)
                    {
                        // suited
                        if (a > b)
                        {
                            cards[0] = a;
                            cards[1] = b;
                        }
                        else if (b > a)
                        {
                            cards[0] = b;
                            cards[1] = a + 13;
                        }
                        else if (a == b)
                        {
                            cards[0] = a;
                            cards[1] = b + 13;
                        }
                        else
                            Debug.WriteLine("Nyt joku kusi pahasti korteissa!");
                        //Debug.WriteLine("{0} = [{1}][{2}]", i, a, b);
                        //Debug.WriteLine("{0} = [{1}][{2}]", index, a, b);
                        return;
                    }
                }
            }
            
        }
   }
}


#Include CLR.ahk
#Include COM.ahk

CLR_Start()

asm := CLR_LoadLibrary("SNGEGT.dll")
obj := CLR_CreateObject(asm, "SNGEGT.ICMClass")

hand := COM_Invoke(obj, "handFromIndex", "5")  ;problem

COM_Release(obj)
COM_Release(asm)

cards is an array but i have no idea how to handle it.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
There are few problems with your post:
[*:3i4yvb74]indexArray is not defined.
[*:3i4yvb74]handFromIndex is private, and therefore not accessible to the script.
[*:3i4yvb74]ICMClass does not exist, only ICM.Arrays must be in SafeArray format. You must replace COM_Invoke with COM_Invoke_ in order to indicate that the parameter is an array. An array of int would be VT_ARRAY|VT_I4; that is, 0x2003. Most primitive types may be passed as VT_BSTR:=8, but objects must be passed as VT_DISPATCH:=9.

The simplest way to create the array is probably via .NET. For instance:
C# =
(
using System;
using System.Windows.Forms;

namespace SNGEGT
{
    class ICM
    {
        public int cardFromIndex(int index, int[] cards) {
            return cards[index];
        }
        public int[] deal() {
            return new int[] { 3, 1, 2 };
        }
   }
}
)

CLR_Start()

asm := CLR_CompileC#(C#, "System.dll | System.Windows.Forms.dll")
obj := CLR_CreateObject(asm, "SNGEGT.ICM")

cards := COM_Invoke(obj, "deal")
Loop 3
    MsgBox % COM_Invoke_(obj, "cardFromIndex", 8, A_Index-1, 0x2003, cards)

COM_Release(obj)
COM_Release(asm)


  • Guests
  • Last active:
  • Joined: --
Can someone post an example for a simple plot using ZedGraph with the CLR?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I know nothing of ZedGraph, but I think you're much more likely to find success by searching for a C# (or VB) example and using that via CLR_CompileC# (or CLR_CompileVB). If you have any problems invoking the C# (or VB) code, post what you've tried.

BoBo³
  • Guests
  • Last active:
  • Joined: --
Here the challenge to create (one or several) samples of code which would show that modules capabilities :!:
OK, OK. A real low level BoBo attempt to get you interested ... :oops:

[Exchange Web Services - Operations/XML Elements]
[Independentsoft - Exchange Web Services ...]

Please feed me :(

yourbuddypal
  • Members
  • 32 posts
  • Last active: May 17 2013 06:50 AM
  • Joined: 29 Jun 2009
Great stuff you have here, I love it. I had the idea to interop to my dlls while commuting to work this morning, and pulled this up in no time. Excellent work.

Looking over your documentation, I have a question:

CLR_Stop()

Stops the Common Language Runtime. Once stopped, it cannot be reinitialized into the same process.


Why would you ever want to use this method, if you can't reinitialize the CLR in your script? Or did I misinterpret something there? I was considering starting the CLR only to communicate with my DLL in certain intervals and then stopping it after completing that operation.