-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClaudeCodeControl.Interop.cs
More file actions
548 lines (454 loc) · 18.7 KB
/
ClaudeCodeControl.Interop.cs
File metadata and controls
548 lines (454 loc) · 18.7 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* *******************************************************************************************************************
* Application: ClaudeCodeExtension
*
* Autor: Daniel Carvalho Liedke
*
* Copyright © Daniel Carvalho Liedke 2026
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Carvalho Liedke is strictly forbidden.
*
* Purpose: Win32 API interop declarations and structures
*
* *******************************************************************************************************************/
using System;
using System.Runtime.InteropServices;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Win32 Constants
// SetWindowPos flags
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_NOACTIVATE = 0x0010;
private const uint SWP_FRAMECHANGED = 0x0020;
// ShowWindow commands
private const int SW_SHOW = 5;
private const int SW_HIDE = 0;
// Window styles
private const int GWL_STYLE = -16;
private const int WS_CHILD = 0x40000000;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
private const int WS_MINIMIZE = 0x20000000;
private const int WS_MAXIMIZE = 0x01000000;
private const int WS_POPUP = unchecked((int)0x80000000);
private const int WS_SYSMENU = 0x00080000;
// Mouse event flags
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
// Window messages
private const uint WM_CLOSE = 0x0010;
private const uint WM_KEYDOWN = 0x0100;
private const uint WM_KEYUP = 0x0101;
private const uint WM_CHAR = 0x0102;
private const uint WM_MOUSEMOVE = 0x0200;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
private const uint WM_MOUSEWHEEL = 0x020A;
// RedrawWindow flags
private const uint RDW_INVALIDATE = 0x0001;
private const uint RDW_ERASE = 0x0004;
private const uint RDW_ALLCHILDREN = 0x0080;
private const uint RDW_UPDATENOW = 0x0100;
private const uint RDW_FRAME = 0x0400;
// Virtual key codes
private const int VK_TAB = 0x09;
private const int VK_RETURN = 0x0D;
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_SPACE = 0x20;
private const int VK_UP = 0x26;
private const int VK_RIGHT = 0x27;
private const int VK_DOWN = 0x28;
private const int VK_C = 0x43;
private const int VK_F5 = 0x74;
// Input type constants
private const uint INPUT_KEYBOARD = 1;
private const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
private const uint KEYEVENTF_KEYUP = 0x0002;
#endregion
#region Win32 Structures
/// <summary>
/// Rectangle structure for window coordinates
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
/// <summary>
/// Input structure for SendInput
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public uint type;
public INPUTUNION u;
}
/// <summary>
/// Union for different input types
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct INPUTUNION
{
[FieldOffset(0)]
public KEYBDINPUT ki;
}
/// <summary>
/// Keyboard input structure
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
#endregion
#region Win32 API Declarations - Window Management
/// <summary>
/// Sets the parent window for a child window
/// </summary>
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
/// <summary>
/// Changes the size and position of a window
/// </summary>
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
/// <summary>
/// Shows or hides a window
/// </summary>
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
/// <summary>
/// Determines whether the specified window handle identifies an existing window
/// </summary>
[DllImport("user32.dll")]
private static extern bool IsWindow(IntPtr hWnd);
/// <summary>
/// Determines the visibility state of the specified window
/// </summary>
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
/// <summary>
/// Retrieves the dots per inch (DPI) of a window
/// </summary>
[DllImport("user32.dll")]
private static extern uint GetDpiForWindow(IntPtr hwnd);
/// <summary>
/// Retrieves the name of the class to which the specified window belongs
/// </summary>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
/// <summary>
/// Changes an attribute of the specified window
/// </summary>
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
/// <summary>
/// Retrieves information about the specified window
/// </summary>
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
/// <summary>
/// Retrieves the dimensions of the bounding rectangle of a window
/// </summary>
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
/// <summary>
/// Invalidates the client area of a window
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
/// <summary>
/// Updates the client area of a window by sending a paint message if needed
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UpdateWindow(IntPtr hWnd);
/// <summary>
/// Invalidates or validates the specified portions of a window
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags);
#endregion
#region Win32 API Declarations - Window Enumeration
/// <summary>
/// Delegate for window enumeration callback
/// </summary>
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
/// <summary>
/// Enumerates all top-level windows
/// </summary>
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
/// <summary>
/// Retrieves the process identifier of the thread that created the window
/// </summary>
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
#endregion
#region Win32 API Declarations - Input and Focus
/// <summary>
/// Sets keyboard focus to the specified window
/// </summary>
[DllImport("user32.dll")]
private static extern bool SetFocus(IntPtr hWnd);
/// <summary>
/// Brings the window to the foreground and activates it
/// </summary>
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Synthesizes keystrokes, mouse motions, and button clicks
/// </summary>
[DllImport("user32.dll")]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
/// <summary>
/// Posts a message to the message queue of a window
/// </summary>
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Sends a message to a window and waits for it to be processed
/// </summary>
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Synthesizes a keystroke
/// </summary>
[DllImport("user32.dll")]
private static extern void keybd_event(int bVk, int bScan, uint dwFlags, UIntPtr dwExtraInfo);
#endregion
#region Win32 API Declarations - Mouse and Cursor
/// <summary>
/// Moves the cursor to the specified screen coordinates
/// </summary>
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int x, int y);
/// <summary>
/// Synthesizes mouse motion and button clicks
/// </summary>
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
#endregion
#region Win32 API Declarations - GDI
/// <summary>
/// Deletes a logical pen, brush, font, bitmap, region, or palette
/// </summary>
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
#endregion
#region Win32 Constants - Console
private const int STD_OUTPUT_HANDLE = -11;
private const uint TMPF_TRUETYPE = 4;
private const int FW_NORMAL = 400;
#endregion
#region Win32 Structures - Console Font
/// <summary>
/// Coordinate structure for console buffer
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct COORD
{
public short X;
public short Y;
}
/// <summary>
/// Extended console font information
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct CONSOLE_FONT_INFOEX
{
public uint cbSize;
public uint nFont;
public COORD dwFontSize;
public uint FontFamily;
public uint FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string FaceName;
}
#endregion
#region Win32 API Declarations - Console
/// <summary>
/// Attaches the calling process to the console of the specified process
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(uint dwProcessId);
/// <summary>
/// Detaches the calling process from its console
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
/// <summary>
/// Retrieves a handle to the specified standard device
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
/// <summary>
/// Sets extended information about the current console font
/// </summary>
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
/// <summary>
/// Retrieves extended information about the current console font
/// </summary>
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx);
#endregion
#region Win32 API Declarations - Hooks
/// <summary>
/// Low-level keyboard hook identifier
/// </summary>
private const int WH_KEYBOARD_LL = 13;
/// <summary>
/// Low-level mouse hook identifier
/// </summary>
private const int WH_MOUSE_LL = 14;
/// <summary>
/// Point structure for low-level mouse hook
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
/// <summary>
/// Low-level mouse hook data structure
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
/// <summary>
/// Low-level keyboard hook data structure
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public IntPtr dwExtraInfo;
}
/// <summary>
/// GUI thread information structure for detecting which window has keyboard focus
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct GUITHREADINFO
{
public uint cbSize;
public uint flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rcCaret;
}
/// <summary>
/// Delegate for low-level keyboard hook callback
/// </summary>
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Delegate for low-level mouse hook callback
/// </summary>
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Installs an application-defined hook procedure into a hook chain (keyboard)
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
/// <summary>
/// Installs an application-defined hook procedure into a hook chain (mouse)
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
/// <summary>
/// Removes a hook procedure installed in a hook chain
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
/// <summary>
/// Passes the hook information to the next hook procedure in the current hook chain
/// </summary>
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// Retrieves a module handle for the specified module
/// </summary>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
/// <summary>
/// Determines whether a key is up or down at the time the function is called
/// </summary>
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
/// <summary>
/// Retrieves information about the active window or a specified GUI thread
/// </summary>
[DllImport("user32.dll")]
private static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
/// <summary>
/// Determines whether a window is a child of a specified parent window
/// </summary>
[DllImport("user32.dll")]
private static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);
#endregion
#region Win32 Structures - Process Snapshot
/// <summary>
/// Describes an entry from a list of processes in the system address space when taking a snapshot
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szExeFile;
}
#endregion
#region Win32 API Declarations - Process Snapshot
private const uint TH32CS_SNAPPROCESS = 0x00000002;
/// <summary>
/// Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes
/// </summary>
[DllImport("kernel32.dll")]
private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
/// <summary>
/// Retrieves information about the first process encountered in a system snapshot
/// </summary>
[DllImport("kernel32.dll")]
private static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
/// <summary>
/// Retrieves information about the next process recorded in a system snapshot
/// </summary>
[DllImport("kernel32.dll")]
private static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
/// <summary>
/// Closes an open object handle
/// </summary>
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
#endregion
}
}