How to use AutoItX in .NET (C#) without registering

后端 未结 4 968
抹茶落季
抹茶落季 2020-12-07 23:06

How do I use AutoitX (OCX/ActiveX library) in a .NET C# application without registering it?

I would like to create an application with it without need to use adminis

相关标签:
4条回答
  • 2020-12-07 23:38

    Via PInvoke

            var assemblies = new NRegFreeCom.AssemblySystem();
            var module = assemblies.LoadFrom(Path.Combine(Environment.CurrentDirectory, "AutoItX3.dll"));
            var createdDirectly = NRegFreeCom.ActivationContext.CreateInstanceDirectly(module, clsid) as IAutoItX3;
            createdDirectly.Run("Notepad");
    

    Via manifests

    AutoItX3.dll.manifest:

    
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
    <assemblyIdentity type="win32" name="AutoItX3.dll" version="3.3.8.1" />
    <file name = "AutoItX3.dll">
    <comClass
    clsid="{1A671297-FA74-4422-80FA-6C5D8CE4DE04}"
    threadingModel = "Free" />
    <typelib tlbid="{F8937E53-D444-4E71-9725-35B64210CC3B}"
    version="1.0" helpdir=""/>
    </file>
    <comInterfaceExternalProxyStub
    name="IAutoItX3"
    iid="{3D54C6B8-D283-40E0-8FAB-C97F05947EE8}"
    proxyStubClsid32="{00020424-0000-0000-C000-000000000046}"
    baseInterface="{00000000-0000-0000-C000-000000000046}"
    tlbid = "{F8937E53-D444-4E71-9275-35B64210CC3B}" />
    </assembly>

    AutoItX3Dependency.manifest:

    
    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <dependency>
    <dependentAssembly asmv2:codebase="AutoItX3.dll.manifest">
    <assemblyIdentity name="AutoItX3.dll" version="3.3.8.1" type="win32" />
    </dependentAssembly>
    </dependency>
    </asmv1:assembly>

    Program.cs Main:

    
                var createdViaManifest = NRegFreeCom.ActivationContext.CreateInstanceWithManifest(new Guid("{1A671297-FA74-4422-80FA-6C5D8CE4DE04}"), "AutoItX3Dependency.manifest");
                var autoItWithManifest = (IAutoItX3)createdViaManifest;
                autoItWithManifest.Run("Notepad");
    

    Code uses tool for reg free in C# (https://github.com/asd-and-Rizzo/NRegFreeCom). Code snipped from (https://github.com/asd-and-Rizzo/pyautoit)

    0 讨论(0)
  • 2020-12-07 23:44

    In your C# project from Visual Studio, just go to Reference -> Add Reference -> Browse to your AutoIt dll and you're done. There's no need to register it seperately. But using this method you have to register.

    A better way is to use the DLL directly, with [DllImport] statements. Here is a sample class that you can use: http://www.autoitscript.com/forum/topic/72905-c-use-of-the-dll-some-idears-for-you/

    It defines functions like this:

    [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static public extern int AU3_MouseUp([MarshalAs(UnmanagedType.LPStr)] string Button);
    
    0 讨论(0)
  • 2020-12-07 23:52

    Copy and paste the AutoItX3.dll file to /bin/Debug or /bin/Release folder. Or for Post-build event set the following command line:

    copy /Y "$(SolutionDir)\packages\AutoItX.3.3.12.0\AutoItX3.dll" "$(ProjectDir)\bin\Debug"
    

     

    copy /Y "$(SolutionDir)\packages\AutoItX.3.3.12.0\AutoItX3.dll" "$(ProjectDir)\bin\Release"
    

    An example to upload a file through Windows system window using Firefox as browser. I use AutoItX v3.3.12.0.

        /// <summary>
        /// Method which allows you to upload a file through windows system window using firefox as browser
        /// </summary>
        /// <param name="file">path file</param>
        /// <param name="winTitle">Window title</param>
        /// <param name="idEditBox">Text box identifier (es. [CLASS:Edit; INSTANCE:1])</param>
        /// <param name="idBtnLoad">Open button identifier (es. [CLASS:Button; INSTANCE:1])</param>
        /// <returns>void</returns>
        /// <author>Michele Delle Donne</author
    
        public static void UploadFileByWindowsFireFoxDialog(string file, string winTitle, string idEditBox, string idBtnLoad)
        {
    
            AutoItX.Init();
    
            AutoItX.WinWait(winTitle);
            AutoItX.WinActivate(winTitle);
    
            AutoItX.ControlSetText(winTitle, "", idEditBox, file);
            AutoItX.ControlClick(winTitle, "", idBtnLoad);            
        }
    
    0 讨论(0)
  • 2020-12-07 23:55

    It's very easy. You just need add library from your project. Click right your reference project - Add reference - Browse - Go to location AutoitX3Lib.dll (C:\Program files\AutoitX3\AutoitX\AutoitX3.dll)

    AutoItX3Lib.AutoItX3 autoit = new AutoItX3Lib.AutoItX3();
    

    For more details visit here

    0 讨论(0)
提交回复
热议问题