본문 바로가기
Development/Visual C#

[C#] Kiwoom 로그인 수동 제어

by qWooWp 2022. 9. 13.
반응형

로그인 창을 수동 제어 하기 위해 필요한 함수들. 

using System.Runtime.InteropServices;
using System.Threading;

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
public static extern int findWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);

[DllImport("user32.dll")]        
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);

[DllImport("user32")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
const int BM_CLICK = 0x00F5;
const int EM_SETSEL = 0x00B1;
const int EM_REPLACESEL = 0x00C2;
const uint WM_CLOSE = 0x10;
void auto_login_control()
{
    IntPtr nhwnd = IntPtr.Zero;
    int iWaitWindowCnt = 10;

    // 일정 시간 동안 로그인 창이 뜨는걸 기다린다. 
    while (iWaitWindowCnt-- > 0)
    {
        nhwnd = FindWindow(null, "Open API Login");
        if (nhwnd != IntPtr.Zero)
            break;
        Thread.Sleep(1000);
    }

    if (nhwnd != IntPtr.Zero)
    {
        // 각 컨트롤에 원하는 데이터를 입력한다. 
        IntPtr edit_id = GetDlgItem(nhwnd, 0x3E8);
        IntPtr edit_pass = GetDlgItem(nhwnd, 0x3E9);
        IntPtr edit_cert = GetDlgItem(nhwnd, 0x3EA);
        IntPtr btn_login = GetDlgItem(nhwnd, 0x1);

        enter_keys(edit_id, "id");
        enter_keys(edit_pass,"password");
        enter_keys(edit_cert,"cert password");
        click_button(btn_login);
     }
}
 
private void enter_keys(IntPtr hwnd, string data)
{
    SendMessage(hwnd, EM_SETSEL, 0, -1);
    SendMessage(hwnd, EM_REPLACESEL, 0, data);
    Thread.Sleep(300);
}

private void click_button(IntPtr hwnd)
{
    PostMessage(hwnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
    Thread.Sleep(100);
    PostMessage(hwnd, WM_LBUTTONUP, 0, IntPtr.Zero);
    Thread.Sleep(300);
}
반응형

댓글