-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpintool.cpp
77 lines (64 loc) · 1.92 KB
/
pintool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (C) 2023 Theo Niessink <theo@taletn.com>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#include <windows.h>
#include <stdio.h>
#include <string.h>
#pragma comment(lib, "user32.lib")
int APIENTRY WinMain(HINSTANCE const hInst, HINSTANCE const hPrevInst, LPSTR const lpCmdLine, const int nCmdShow)
{
const char* pin = lpCmdLine;
while (pin[0] == ' ') pin++;
int len = (int)strlen(pin);
while (len && pin[len - 1] == ' ') len--;
if (!len) return 1;
HWND hWnd = NULL;
HANDLE hMutex = NULL;
DWORD delay = 100, timer = 0;
static const DWORD timeout = 30*1000;
static const int numTries = 2;
for (int idx = 0, tries = 0; timer < timeout;)
{
Sleep(delay);
timer += delay;
if (!hWnd)
{
hWnd = FindWindowA("Credential Dialog Xaml Host", NULL);
if (hWnd)
{
char name[256];
_snprintf_s(name, sizeof(name), sizeof(name), "2b93b074-0703-42ee-9916-bb0d0e3fbc98_%p", hWnd);
hMutex = CreateMutexA(NULL, FALSE, name);
if (!hMutex || GetLastError() == ERROR_ALREADY_EXISTS) return 1;
delay = 1000;
}
}
else if (!IsWindow(hWnd))
{
break;
}
else if (idx < 0) // Waiting before retrying...
{
idx++;
}
else if (idx < len)
{
const int c = pin[idx++];
PostMessageA(hWnd, WM_CHAR, c, (c << 16) | 1);
delay = 250;
}
else
{
const UINT enter = MapVirtualKeyA(VK_RETURN, /* MAPVK_VK_TO_VSC */ 0);
PostMessageA(hWnd, WM_KEYDOWN, VK_RETURN, (enter << 16) | 1);
PostMessageA(hWnd, WM_KEYUP, VK_RETURN, ((KF_UP | KF_REPEAT | enter) << 16) | 1);
if (++tries >= numTries) break;
timer = 0;
// Retry if dialog is still around in 30*100 ms.
idx = -30; delay = 100;
}
}
if (hMutex) CloseHandle(hMutex);
return 0;
}