首先下载安装版的AutoIt3,安装后有个AutoItX3.dll,复制到程序bin目录下。
然后添加类文件:
using System.Text;
using System.Runtime.InteropServices;
namespace audoit
{
public 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, LPCWSTR szControl);
[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern int AU3_ControlDisable([MarshalAs(UnmanagedType.LPWStr)] string Title
, [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control);
//AU3_API long WINAPI AU3_ControlEnable(LPCWSTR szTitle, LPCWSTR szText, LPCWSTR szControl);
[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern int AU3_ControlEnable([MarshalAs(UnmanagedType.LPWStr)] string Title
, [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control);
//AU3_API long WINAPI AU3_ControlFocus(LPCWSTR szTitle, LPCWSTR szText, LPCWSTR szControl);
[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern int AU3_ControlFocus([MarshalAs(UnmanagedType.LPWStr)] string Title
, [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control);
//AU3_API void WINAPI AU3_ControlGetFocus(LPCWSTR szTitle, LPCWSTR szText, LPWSTR szControlWithFocus
//, int nBufSize);
[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern void AU3_ControlGetFocus([MarshalAs(UnmanagedType.LPWStr)] string Title
, [MarshalAs(UnmanagedType.LPWStr)] string Text, [Ma