2009年08月14日
Market Speedの自動ログインツール
マケスピVersion 8.0用の自動ログインツールを作成しました。
追記:バイナリは、別の記事:マケスピの自動ログインツール再びで公開しています。
C#でこんな感じ。
(1)アプリケーションを自動起動して、
(2)ログインボタンを自動で押して、
(3)IDとパスワードを自動入力します。
MarketSpeed.cs の8〜10行目は環境に応じて適当に変えてください。
Program.cs
namespace AutoLoginMaster
{
class Program {
static void Main(string[] args) {
MarketSpeed.Login("ID", "PASSWORD");
}
}
}
MarketSpeed.cs
using System;
using System.Diagnostics;
namespace AutoLoginMaster
{
public class MarketSpeed
{
public static readonly string WORKING_DIR = @"C:/Program Files/MarketSpeed/MLauncher";
public static readonly string FILE_NAME = @"C:/Program Files/MarketSpeed/MLauncher/MLauncher.exe";
public static readonly string ARGS = "MarketSpeed";
public static void Login(string user, string password)
{
ProcessStartInfo info = new ProcessStartInfo();
info.WorkingDirectory = WORKING_DIR;
info.FileName = FILE_NAME;
info.Arguments = ARGS;
Process.Start(info);
// 本体が起動するのを待つ
IntPtr hWndMarketSpeed = Helper.WaitFindingTopWindow("Market Speed Ver8.0");
// ログインボタンが配置されているエリアのハンドルを取得する
IntPtr hWndLoginArea = Helper.WaitFindingChildWindow(hWndMarketSpeed, "Custom", "ToolMenu");
// Market Speedのログインダイアログを探す
IntPtr hWndLoginDialog = Helper.WaitFindingTopWindow("Market Speed - ログイン",
delegate() {
// ログインボタンを押下する
Helper.RECT pos;
Helper.GetWindowRect(hWndLoginArea, out pos);
int x = pos.right - pos.left - 20;
int y = 20;
Helper.PushButton(hWndLoginArea, x, y);
Thread.Sleep(100);
});
// ユーザーID入力欄に入力する
Helper.SendText(Helper.FindNextWindow(hWndLoginDialog, "ログインID"), user);
// パスワード入力欄に入力する
Helper.SendText(Helper.FindNextWindow(hWndLoginDialog, "パスワード"), password);
// "OK"ボタンを押下する
Helper.PushButton(Helper.WaitFindingChildWindow(hWndLoginDialog, "Button", "OK"), 0, 0);
}
}
Helper.cs
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace AutoLoginMaster
{
public static class Helper
{
#region Win32API
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(
IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, Int32 uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, String lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
[DllImport("User32.Dll")]
public static extern int GetWindowRect(IntPtr hWnd, out RECT rect);
public const uint WM_SETTEXT = 0x000C;
public const uint WM_LBUTTONDOWN = 0x201;
public const uint WM_LBUTTONUP = 0x202;
public const int GW_HWNDNEXT = 2;
// GetWindowRect用型宣言
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
#endregion
public static void SendText(IntPtr hWnd, string text)
{
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, text);
}
public static void PushButton(IntPtr hWnd, int x, int y)
{
int param = y << 16 | x;
PostMessage(hWnd, WM_LBUTTONDOWN, 0, param);
PostMessage(hWnd, WM_LBUTTONUP, 0, param);
}
public static IntPtr FindNextWindow(IntPtr hWndParent, string name)
{
return GetWindow(FindWindowEx(hWndParent, IntPtr.Zero, "Static", name), GW_HWNDNEXT);
}
public delegate void InitHandler();
public static IntPtr WaitFindingTopWindow(string name)
{
return WaitFindingTopWindow(name, null);
}
public static IntPtr WaitFindingTopWindow(string name, InitHandler init)
{
IntPtr hWnd = IntPtr.Zero;
do
{
if (init != null)
{
init.Invoke();
}
hWnd = FindWindow(null, name);
Thread.Sleep(100);
} while (hWnd == IntPtr.Zero);
return hWnd;
}
public static IntPtr WaitFindingChildWindow(IntPtr hWndParent, string clazz, string name)
{
IntPtr hWnd = IntPtr.Zero;
do
{
hWnd = FindWindowEx(hWndParent, IntPtr.Zero, clazz, name);
Thread.Sleep(100);
} while (hWnd == IntPtr.Zero);
return hWnd;
}
}
}
作成に当たり、こちらの方のソースを参考にさせていただきました。
http://blogs.wankuma.com/ch3cooh/archive/2008/02/05/120955.aspx
トラックバックURL
この記事へのコメント
1. Posted by kuro 2009年08月15日 10:36