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

↘锁芯ラ 提交于 2019-11-27 05:19:48

问题


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 administrator rights for installation.

I found some pages on MSDN like, Registration-Free Activation of COM Components: A Walkthrough about creating manifest files for DLL files. I tried it and did not succeed. So maybe it is possible and I created it wrongly. Unfortunately I lost the XML files so I can't post it here.

I also tried setting isolated and enable interop types in reference properties without success.

Is it possible to get AutoItX working in C# without need for registering? If so, how do I do it?

I think it should be possible to use as a DLL and not an OCX, but I don't know how exactly to do it in C#.

Currently, I use it like:

AutoItX3Lib.AutoItX3 autoit = new AutoItX3Lib.AutoItX3();
autoit.AutoItSetOption("WinTitleMatchMode", 2);

etc. So if I would go for direct DLL calls, how would I do it then?


回答1:


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);



回答2:


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)




回答3:


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);            
    }



回答4:


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



来源:https://stackoverflow.com/questions/6810692/how-to-use-autoitx-in-net-c-without-registering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!