AutoIt没有直接支持.net环境的dll文件,要想在C#(我用的IDE是VS2012)中使用AutoIt API需要做一些准备工作。在网络上找了很多资料问了很多人,方法各种各样,甚至有人说需要交叉编译。。后来找到老外一篇文章跟着测试了一下可用,这里把所有步骤记录下来:
- 到AutoIt官方下载AutoIt Full Installation(注意,项目中使用的dll文件一定要是AutoIt Full Installation安装后文件夹中的那个dll,我测试过AutoIt- Self Extracting中的dll会出错 )http://www.autoitscript.com/site/autoit/downloads/
- 找到这个文件AutoItX3.dll
- 在VS2012中创建一个新项目
- 在Solution Explorer窗口中右击References,然后添加AutoItX3.dll到项目中
- AutoItX3.dll被添加后,在Solution Explorer窗口右击AutoItX3Lib并选择“属性”
- 在弹出的窗口中将Isolated属性改为true
- 在cs文件头部添加命名空间引用using System.Runtime.InteropServices
- 复制第一条横线之后第一条横线之前的代码,张贴到命名空间右花括号之前,你的代码之后
- 输入第二条横线之后的代码到你的项目中
- 生成你的项目,看是否成功运行
-----------------------------------------------------------------------------------------
#region AutoIt Functions internal static class AutoItX3Declarations { //NOTE: This is based on AutoItX v3.3.0.0 which is a Unicode version //NOTE: My comments usually have "jcd" appended where I am uncertain. //NOTE: Optional parameters are not supported in C# yet so fill in all fields even if just "". //NOTE: Be prepared to play around a bit with which fields need values and what those value are. //NOTE: In previous versions we used byte[] to return values like this: //byte[] returnclip = new byte[200]; //at least twice as long as the text string expected +2 for null, (Unicode is 2 bytes) //AutoItX3Declarations.AU3_ClipGet(returnclip, returnclip.Length); //clipdata = System.Text.Encoding.Unicode.GetString(returnclip).TrimEnd('\0'); //Now we are returning Unicode we can use StringBuilder instead like this: //StringBuilder clip = new StringBuilder(); //passing a parameter here will not work, we must asign a length //clip.Length = 200; //the number of chars expected plus 1 for the terminating null //AutoItX3Declarations.AU3_ClipGet(clip,clip.Length); //MessageBox.Show(clip.ToString()); //NOTE: The big advantage of using AutoItX3 like this is that you don't have to register //the dll with windows and more importantly you get away from the many issues involved in //publishing the application and the binding to the dll required. //The below constants were found by Registering AutoItX3.dll in Windows //, adding AutoItX3Lib to References in IDE //,declaring an instance of it like this: // AutoItX3Lib.AutoItX3 autoit; // static AutoItX3Lib.AutoItX3Class autoit; //,right clicking on the AutoItX3Class and then Goto Definitions //and seeing the equivalent of the below in the MetaData Window. //So far it is working //NOTE: easier way is to use "DLL Export Viewer" utility and get it to list Properties also //"DLL Export Viewer" is from http://www.nirsoft.net // Definitions public const int AU3_INTDEFAULT = -2147483647; // "Default" value for _some_ int parameters (largest negative number) public const int error = 1; public const int SW_HIDE = 2; public const int SW_MAXIMIZE = 3; public const int SW_MINIMIZE = 4; public const int SW_RESTORE = 5; public const int SW_SHOW = 6; public const int SW_SHOWDEFAULT = 7; public const int SW_SHOWMAXIMIZED = 8; public const int SW_SHOWMINIMIZED = 9; public const int SW_SHOWMINNOACTIVE = 10; public const int SW_SHOWNA = 11; public const int SW_SHOWNOACTIVATE = 12; public const int SW_SHOWNORMAL = 13; public const int version = 110; //was 109 if previous non-unicode version / Exported functions of AutoItXC.dll / //AU3_API void WINAPI AU3_Init(void); //Uncertain if this is needed jcd [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_Init(); //AU3_API long AU3_error(void); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern int AU3_error(); //AU3_API long WINAPI AU3_AutoItSetOption(LPCWSTR szOption, long nValue); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern int AU3_AutoItSetOption([MarshalAs(UnmanagedType.LPWStr)] string Option, int Value); //AU3_API void WINAPI AU3_BlockInput(long nFlag); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_BlockInput(int Flag); //AU3_API long WINAPI AU3_CDTray(LPCWSTR szDrive, LPCWSTR szAction); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern int AU3_CDTray([MarshalAs(UnmanagedType.LPWStr)] string Drive , [MarshalAs(UnmanagedType.LPWStr)] string Action); //AU3_API void WINAPI AU3_ClipGet(LPWSTR szClip, int nBufSize); //Use like this: //StringBuilder clip = new StringBuilder(); //clip.Length = 4; //AutoItX3Declarations.AU3_ClipGet(clip,clip.Length); //MessageBox.Show(clip.ToString()); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_ClipGet([MarshalAs(UnmanagedType.LPWStr)]StringBuilder Clip, int BufSize); //AU3_API void WINAPI AU3_ClipPut(LPCWSTR szClip); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_ClipPut([MarshalAs(UnmanagedType.LPWStr)] string Clip); //AU3_API long WINAPI AU3_ControlClick(LPCWSTR szTitle, LPCWSTR szText, LPCWSTR szControl //, LPCWSTR szButton, long nNumClicks, /*[in,defaultvalue(AU3_INTDEFAULT)]*/long nX //, /*[in,defaultvalue(AU3_INTDEFAULT)]*/long nY); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern int AU3_ControlClick([MarshalAs(UnmanagedType.LPWStr)] string Title , [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control , [MarshalAs(UnmanagedType.LPWStr)] string Button, int NumClicks, int X, int Y); //AU3_API void WINAPI AU3_ControlCommand(LPCWSTR szTitle, LPCWSTR szText, LPCWSTR szControl //, LPCWSTR szCommand, LPCWSTR szExtra, LPWSTR szResult, int nBufSize); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_ControlCommand([MarshalAs(UnmanagedType.LPWStr)] string Title , [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control , [MarshalAs(UnmanagedType.LPWStr)] string Command, [MarshalAs(UnmanagedType.LPWStr)] string Extra , [MarshalAs(UnmanagedType.LPWStr)] StringBuilder Result, int BufSize); //AU3_API void WINAPI AU3_ControlListView(LPCWSTR szTitle, LPCWSTR szText, LPCWSTR szControl //, LPCWSTR szCommand, LPCWSTR szExtra1, LPCWSTR szExtra2, LPWSTR szResult, int nBufSize); [DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)] static public extern void AU3_ControlListView([MarshalAs(UnmanagedType.LPWStr)] string Title , [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control , [MarshalAs(UnmanagedType.LPWStr)] string Command, [MarshalAs(UnmanagedType.LPWStr)] string Extral1 , [MarshalAs(UnmanagedType.LPWStr)] string Extra2, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder Result , int BufSize); //AU3_API long WINAPI AU3_ControlDisable(LPCWSTR szTitle, LPCWSTR szText, L