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 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Windows.Forms;
using KeePass.UI;
using KeePass.Util;
using KeePassLib.Utility;
using NativeLib = KeePassLib.Native.NativeLib;
namespace KeePass.Native
{
internal static partial class NativeMethods
{
internal static string GetWindowText(IntPtr hWnd, bool bTrim)
{
// cc may be greater than the actual length;
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowtextlengthw
int cc = GetWindowTextLength(hWnd);
if(cc <= 0) return string.Empty;
// StringBuilder sb = new StringBuilder(cc + 2);
// int ccReal = GetWindowText(hWnd, sb, cc + 1);
// if(ccReal <= 0) { Debug.Assert(false); return string.Empty; }
// // The text isn't always NULL-terminated; trim garbage
// if(ccReal < sb.Length)
// sb.Remove(ccReal, sb.Length - ccReal);
// string strWindow = sb.ToString();
string strWindow;
IntPtr p = IntPtr.Zero;
try
{
int cbChar = Marshal.SystemDefaultCharSize;
int cb = (cc + 2) * cbChar;
p = Marshal.AllocCoTaskMem(cb);
if(p == IntPtr.Zero) { Debug.Assert(false); return string.Empty; }
byte[] pbZero = new byte[cb];
Marshal.Copy(pbZero, 0, p, cb);
int ccReal = GetWindowText(hWnd, p, cc + 1);
if(ccReal <= 0) { Debug.Assert(false); return string.Empty; }
if(ccReal <= cc)
{
// Ensure correct termination (in case GetWindowText
// copied too much)
int ibZero = ccReal * cbChar;
for(int i = 0; i < cbChar; ++i)
Marshal.WriteByte(p, ibZero + i, 0);
}
else { Debug.Assert(false); return string.Empty; }
strWindow = (Marshal.PtrToStringAuto(p) ?? string.Empty);
}
finally { if(p != IntPtr.Zero) Marshal.FreeCoTaskMem(p); }
return (bTrim ? strWindow.Trim() : strWindow);
}
/* internal static string GetWindowClassName(IntPtr hWnd)
{
try
{
StringBuilder sb = new StringBuilder(260);
if(GetClassName(hWnd, sb, 258) > 0)
return sb.ToString();
else { Debug.Assert(false); }
return string.Empty;
}
catch(Exception) { Debug.Assert(false); }
return null;
} */
internal static IntPtr GetForegroundWindowHandle()
{
if(!NativeLib.IsUnix())
return GetForegroundWindow(); // Windows API
try
{
return new IntPtr(long.Parse(RunXDoTool(
"getactivewindow").Trim()));
}
catch(Exception) { Debug.Assert(false); }
return IntPtr.Zero;
}
private static readonly char[] m_vWindowTrim = { '\r', '\n' };
internal static void GetForegroundWindowInfo(out IntPtr hWnd,
out string strWindowText, bool bTrimWindow)
{
hWnd = GetForegroundWindowHandle();
if(!NativeLib.IsUnix()) // Windows
strWindowText = GetWindowText(hWnd, bTrimWindow);
else // Unix
{
strWindowText = RunXDoTool("getactivewindow getwindowname");
if(!string.IsNullOrEmpty(strWindowText))
{
if(bTrimWindow) strWindowText = strWindowText.Trim();
else strWindowText = strWindowText.Trim(m_vWindowTrim);
}
}
}
internal static bool IsWindowEx(IntPtr hWnd)
{
if(!NativeLib.IsUnix()) // Windows
return IsWindow(hWnd);
return true;
}
internal static int GetWindowStyle(IntPtr hWnd)
{
return GetWindowLong(hWnd, GWL_STYLE);
}
internal static IntPtr GetClassLongPtrEx(IntPtr hWnd, int nIndex)
{
if(IntPtr.Size == 4) return GetClassLong(hWnd, nIndex);
return GetClassLongPtr(hWnd, nIndex);
}
internal static bool SetForegroundWindowEx(IntPtr hWnd)
{
if(!NativeLib.IsUnix())
return SetForegroundWindow(hWnd);
return (RunXDoTool("windowactivate " +
hWnd.ToInt64().ToString()).Trim().Length == 0);
}
internal static bool EnsureForegroundWindow(IntPtr hWnd)
{
if(!IsWindowEx(hWnd)) return false;
IntPtr hWndInit = GetForegroundWindowHandle();
if(!SetForegroundWindowEx(hWnd))
{
Debug.Assert(false);
return false;
}
int nStartMS = Environment.TickCount;
while((Environment.TickCount - nStartMS) < 1000)
{
IntPtr h = GetForegroundWindowHandle();
if(h == hWnd) return true;
// Some applications (like Microsoft Edge) have multiple
// windows and automatically redirect the focus to other
// windows, thus also break when a different window gets
// focused (except when h is zero, which can occur while
// the focus transfer occurs)
if((h != IntPtr.Zero) && (h != hWndInit)) return true;
Application.DoEvents();
}
return false;
}
internal static IntPtr FindWindow(string strTitle)
{
if(strTitle == null) { Debug.Assert(false); return IntPtr.Zero; }
if(!NativeLib.IsUnix())
return FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, strTitle);
// Not --onlyvisible (due to not finding minimized windows)
string str = RunXDoTool("search --name \"" + strTitle + "\"").Trim();
if(str.Length == 0) return IntPtr.Zero;
long l;
if(long.TryParse(str, out l)) return new IntPtr(l);
return IntPtr.Zero;
}
internal static bool LoseFocus(Form fCurrent)
{
if(NativeLib.IsUnix())
return LoseFocusUnix(fCurrent);
try
{
IntPtr hCurrentWnd = ((fCurrent != null) ? fCurrent.Handle : IntPtr.Zero);
IntPtr hWnd = GetWindow(hCurrentWnd, GW_HWNDNEXT);
while(true)
{
if(hWnd != hCurrentWnd)
{
int nStyle = GetWindowStyle(hWnd);
if(((nStyle & WS_VISIBLE) != 0) &&
(GetWindowTextLength(hWnd) > 0))
{
// Skip the taskbar window (required for Windows 7,
// when the target window is the only other window
// in the taskbar)
if(!IsTaskBar(hWnd)) break;
}
}
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
if(hWnd == IntPtr.Zero) break;
}
if(hWnd == IntPtr.Zero) return false;
Debug.Assert(GetWindowText(hWnd, true) != "Start");
return EnsureForegroundWindow(hWnd);
}
catch(Exception) { Debug.Assert(false); }
return false;
}
internal static bool IsTaskBar(IntPtr hWnd)
{
Process p = null;
try
{
string strText = GetWindowText(hWnd, true);
if(strText == null) return false;
if(!strText.Equals("Start", StrUtil.CaseIgnoreCmp)) return false;
uint uProcessId;
NativeMethods.GetWindowThreadProcessId(hWnd, out uProcessId);
p = Process.GetProcessById((int)uProcessId);
string strExe = UrlUtil.GetFileName(p.MainModule.FileName).Trim();
return strExe.Equals("Explorer.exe", StrUtil.CaseIgnoreCmp);
}
catch(Exception) { Debug.Assert(false); }
finally
{
try { if(p != null) p.Dispose(); }
catch(Exception) { Debug.Assert(false); }
}
return false;
}
/* internal static bool IsMetroWindow(IntPtr hWnd)
{
if(hWnd == IntPtr.Zero) { Debug.Assert(false); return false; }
if(NativeLib.IsUnix() || !WinUtil.IsAtLeastWindows8)
return false;
try
{
uint uProcessId;
NativeMethods.GetWindowThreadProcessId(hWnd, out uProcessId);
if(uProcessId == 0) { Debug.Assert(false); return false; }
IntPtr h = NativeMethods.OpenProcess(NativeMethods.PROCESS_QUERY_INFORMATION,
false, uProcessId);
if(h == IntPtr.Zero) return false; // No assert
bool bRet = NativeMethods.IsImmersiveProcess(h);
NativeMethods.CloseHandle(h);
return bRet;
}
catch(Exception) { Debug.Assert(false); }
return false;
} */
public static bool IsInvalidHandleValue(IntPtr p)
{
long h = p.ToInt64();
if(h == -1) return true;
if(h == 0xFFFFFFFF) return true;
return false;
}
public static int GetHeaderHeight(ListView lv)
{
if(lv == null) { Debug.Assert(false); return 0; }
try
{
if((lv.View == View.Details) && (lv.HeaderStyle !=
ColumnHeaderStyle.None) && (lv.Columns.Count > 0) &&
!NativeLib.IsUnix())
{
IntPtr hHeader = NativeMethods.SendMessage(lv.Handle,
NativeMethods.LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
if(hHeader != IntPtr.Zero)
{
NativeMethods.RECT rect = new NativeMethods.RECT();
if(NativeMethods.GetWindowRect(hHeader, ref rect))
return (rect.Bottom - rect.Top);
else { Debug.Assert(false); }
}
else { Debug.Assert(false); }
}
}
catch(Exception) { Debug.Assert(false); }
return 0;
}
// Workaround for only partially visible list view items
/* public static void EnsureVisible(ListView lv, int nIndex, bool bPartialOK)
{
Debug.Assert(lv != null); if(lv == null) return;
Debug.Assert(nIndex >= 0); if(nIndex < 0) return;
Debug.Assert(nIndex < lv.Items.Count); if(nIndex >= lv.Items.Count) return;
int nPartialOK = (bPartialOK ? 1 : 0);
try
{
NativeMethods.SendMessage(lv.Handle, LVM_ENSUREVISIBLE,
new IntPtr(nIndex), new IntPtr(nPartialOK));
}
catch(Exception) { Debug.Assert(false); }
} */
public static int GetScrollPosY(IntPtr hWnd)
{
if(NativeLib.IsUnix()) return 0;
try
{
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)ScrollInfoMask.SIF_POS;
if(GetScrollInfo(hWnd, (int)ScrollBarDirection.SB_VERT, ref si))
return si.nPos;
Debug.Assert(false);
}
catch(Exception) { Debug.Assert(false); }
return 0;
}
public static void Scroll(ListView lv, int dx, int dy)
{
if(lv == null) throw new ArgumentNullException("lv");
if(NativeLib.IsUnix()) return;
try { SendMessage(lv.Handle, LVM_SCROLL, (IntPtr)dx, (IntPtr)dy); }
catch(Exception) { Debug.Assert(false); }
}
/* public static void ScrollAbsY(IntPtr hWnd, int y)
{
try
{
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)ScrollInfoMask.SIF_POS;
si.nPos = y;
SetScrollInfo(hWnd, (int)ScrollBarDirection.SB_VERT, ref si, true);
}
catch(Exception) { Debug.Assert(false); }
} */
/* public static void Scroll(IntPtr h, int dx, int dy)
{
if(h == IntPtr.Zero) { Debug.Assert(false); return; } // No throw
try
{
ScrollWindowEx(h, dx, dy, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero, SW_INVALIDATE);
}
catch(Exception) { Debug.Assert(false); }
} */
/* internal static void ClearIconicBitmaps(IntPtr hWnd)
{
// TaskbarList.SetThumbnailClip(hWnd, new Rectangle(0, 0, 1, 1));
// TaskbarList.SetThumbnailClip(hWnd, null);
try { DwmInvalidateIconicBitmaps(hWnd); }
catch(Exception) { Debug.Assert(!WinUtil.IsAtLeastWindows7); }
} */
internal static uint? GetLastInputTime()
{
try
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
if(!GetLastInputInfo(ref lii)) { Debug.Assert(false); return null; }
return lii.dwTime;
}
catch(Exception)
{
Debug.Assert(NativeLib.IsUnix() || WinUtil.IsWindows9x);
}
return null;
}
internal static bool SHGetFileInfo(string strPath, int nPrefImgDim,
out Image img, out string strDisplayName)
{
img = null;
strDisplayName = null;
try
{
SHFILEINFO fi = new SHFILEINFO();
IntPtr p = SHGetFileInfo(strPath, 0, ref fi, (uint)Marshal.SizeOf(typeof(
SHFILEINFO)), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_DISPLAYNAME);
if(p == IntPtr.Zero) return false;
if(fi.hIcon != IntPtr.Zero)
{
using(Icon ico = Icon.FromHandle(fi.hIcon)) // Doesn't take ownership
{
img = new Bitmap(nPrefImgDim, nPrefImgDim);
using(Graphics g = Graphics.FromImage(img))
{
g.Clear(Color.Transparent);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawIcon(ico, new Rectangle(0, 0, nPrefImgDim, nPrefImgDim));
}
}
if(!DestroyIcon(fi.hIcon)) { Debug.Assert(false); }
}
strDisplayName = fi.szDisplayName;
return true;
}
catch(Exception) { Debug.Assert(false); }
return false;
}
/// <summary>
/// Method for testing whether a file exists or not. Also
/// supports NTFS alternate data streams.
/// </summary>
/// <param name="strFilePath">Path of the file or stream.</param>
/// <returns><c>true</c> if the file exists.</returns>
public static bool FileExists(string strFilePath)
{
if(strFilePath == null) throw new ArgumentNullException("strFilePath");
try
{
// https://sourceforge.net/p/keepass/discussion/329221/thread/65244cc9/
if(!NativeLib.IsUnix())
return (GetFileAttributes(strFilePath) != INVALID_FILE_ATTRIBUTES);
}
catch(Exception) { Debug.Assert(false); }
// Fallback to .NET method (for Unix-like systems)
try { return File.Exists(strFilePath); }
catch(Exception) { Debug.Assert(false); } // Invalid path
return false;
}
/* internal static LVGROUP GetGroupInfoByIndex(ListView lv, uint uIndex)
{
if(lv == null) throw new ArgumentNullException("lv");
if(uIndex >= (uint)lv.Groups.Count)
throw new ArgumentOutOfRangeException("uIndex");
const int nStrLen = 1024;
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(typeof(LVGROUP));
g.mask = ...;
g.pszHeader = new StringBuilder(nStrLen);
g.cchHeader = nStrLen - 1;
g.pszFooter = new StringBuilder(nStrLen);
g.cchFooter = nStrLen - 1;
g.pszSubtitle = new StringBuilder(nStrLen);
g.cchSubtitle = (uint)(nStrLen - 1);
g.pszTask = new StringBuilder(nStrLen);
g.cchTask = (uint)(nStrLen - 1);
g.pszDescriptionTop = new StringBuilder(nStrLen);
g.cchDescriptionTop = (uint)(nStrLen - 1);
g.pszDescriptionBottom = new StringBuilder(nStrLen);
g.cchDescriptionBottom = (uint)(nStrLen - 1);
g.pszSubsetTitle = new StringBuilder(nStrLen);
g.cchSubsetTitle = (uint)(nStrLen - 1);
SendMessageLVGroup(lv.Handle, LVM_GETGROUPINFOBYINDEX,
new IntPtr((int)uIndex), ref g);
return g;
} */
/* internal static uint GetGroupStateByIndex(ListView lv, uint uIndex,
uint uStateMask, out int iGroupID)
{
if(lv == null) throw new ArgumentNullException("lv");
if(uIndex >= (uint)lv.Groups.Count)
throw new ArgumentOutOfRangeException("uIndex");
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(g);
g.mask = (LVGF_STATE | LVGF_GROUPID);
g.stateMask = uStateMask;
SendMessageLVGroup(lv.Handle, LVM_GETGROUPINFOBYINDEX,
new IntPtr((int)uIndex), ref g);
iGroupID = g.iGroupId;
return g.state;
}
internal static void SetGroupState(ListView lv, int iGroupID,
uint uStateMask, uint uState)
{
if(lv == null) throw new ArgumentNullException("lv");
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(g);
g.mask = LVGF_STATE;
g.stateMask = uStateMask;
g.state = uState;
SendMessageLVGroup(lv.Handle, LVM_SETGROUPINFO,
new IntPtr(iGroupID), ref g);
} */
/* internal static int GetGroupIDByIndex(ListView lv, uint uIndex)
{
if(lv == null) { Debug.Assert(false); return 0; }
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(g);
g.AssertSize();
g.mask = NativeMethods.LVGF_GROUPID;
if(SendMessageLVGroup(lv.Handle, LVM_GETGROUPINFOBYINDEX,
new IntPtr((int)uIndex), ref g) == IntPtr.Zero)
{
Debug.Assert(false);
}
return g.iGroupId;
}
internal static void SetGroupTask(ListView lv, int iGroupID,
string strTask)
{
if(lv == null) { Debug.Assert(false); return; }
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(g);
g.AssertSize();
g.mask = LVGF_TASK;
g.pszTask = strTask;
g.cchTask = (uint)((strTask != null) ? strTask.Length : 0);
if(SendMessageLVGroup(lv.Handle, LVM_SETGROUPINFO,
new IntPtr(iGroupID), ref g) == (new IntPtr(-1)))
{
Debug.Assert(false);
}
} */
/* internal static void SetListViewGroupInfo(ListView lv, int iGroupID,
string strTask, bool? obCollapsible)
{
if(lv == null) { Debug.Assert(false); return; }
if(!WinUtil.IsAtLeastWindowsVista) return;
LVGROUP g = new LVGROUP();
g.cbSize = (uint)Marshal.SizeOf(g);
g.AssertSize();
if(strTask != null)
{
g.mask |= LVGF_TASK;
g.pszTask = strTask;
g.cchTask = (uint)strTask.Length;
}
if(obCollapsible.HasValue)
{
g.mask |= LVGF_STATE;
g.stateMask = LVGS_COLLAPSIBLE;
g.state = (obCollapsible.Value ? LVGS_COLLAPSIBLE : 0);
}
if(g.mask == 0) return;
if(SendMessageLVGroup(lv.Handle, LVM_SETGROUPINFO,
new IntPtr(iGroupID), ref g) == (new IntPtr(-1)))
{
Debug.Assert(false);
}
}
internal static int GetListViewGroupID(ListViewGroup lvg)
{
if(lvg == null) { Debug.Assert(false); return -1; }
Type t = typeof(ListViewGroup);
PropertyInfo pi = t.GetProperty("ID", (BindingFlags.Instance |
BindingFlags.NonPublic));
if(pi == null) { Debug.Assert(false); return -1; }
if(pi.PropertyType != typeof(int)) { Debug.Assert(false); return -1; }
return (int)pi.GetValue(lvg, null);
} */
private static bool GetDesktopName(IntPtr hDesk, out string strAnsi,
out string strUni)
{
strAnsi = null;
strUni = null;
const uint cbZ = 12; // Minimal number of terminating zeros
const uint uBufSize = 64 + cbZ;
IntPtr pBuf = Marshal.AllocCoTaskMem((int)uBufSize);
byte[] pbZero = new byte[uBufSize];
Marshal.Copy(pbZero, 0, pBuf, pbZero.Length);
try
{
uint uReqSize = uBufSize - cbZ;
bool bSuccess = GetUserObjectInformation(hDesk, 2, pBuf,
uBufSize - cbZ, ref uReqSize);
if(uReqSize > (uBufSize - cbZ))
{
Marshal.FreeCoTaskMem(pBuf);
pBuf = Marshal.AllocCoTaskMem((int)(uReqSize + cbZ));
pbZero = new byte[uReqSize + cbZ];
Marshal.Copy(pbZero, 0, pBuf, pbZero.Length);
bSuccess = GetUserObjectInformation(hDesk, 2, pBuf,
uReqSize, ref uReqSize);
Debug.Assert((uReqSize + cbZ) == (uint)pbZero.Length);
}
if(bSuccess)
{
try { strAnsi = Marshal.PtrToStringAnsi(pBuf).Trim(); }
catch(Exception) { }
try { strUni = Marshal.PtrToStringUni(pBuf).Trim(); }
catch(Exception) { }
return true;
}
}
finally { Marshal.FreeCoTaskMem(pBuf); }
Debug.Assert(false);
return false;
}
// The GetUserObjectInformation function apparently returns the
// desktop name using ANSI encoding even on Windows 7 systems.
// As the encoding is not documented, we test both ANSI and
// Unicode versions of the name.
internal static bool? DesktopNameContains(IntPtr hDesk, string strName)
{
if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return false; }
string strAnsi, strUni;
if(!GetDesktopName(hDesk, out strAnsi, out strUni)) return null;
if((strAnsi == null) && (strUni == null)) return null;
try
{
if((strAnsi != null) && (strAnsi.IndexOf(strName,
StringComparison.OrdinalIgnoreCase) >= 0))
return true;
}
catch(Exception) { Debug.Assert(false); }
try
{
if((strUni != null) && (strUni.IndexOf(strName,
StringComparison.OrdinalIgnoreCase) >= 0))
return true;
}
catch(Exception) { Debug.Assert(false); }
return false;
}
internal static bool? IsKeyDownMessage(ref Message m)
{
if(m.Msg == NativeMethods.WM_KEYDOWN) return true;
if(m.Msg == NativeMethods.WM_KEYUP) return false;
if(m.Msg == NativeMethods.WM_SYSKEYDOWN) return true;
if(m.Msg == NativeMethods.WM_SYSKEYUP) return false;
return null;
}
/// <summary>
/// PRIMARYLANGID macro.
/// </summary>
internal static ushort GetPrimaryLangID(ushort uLangID)
{
return (ushort)(uLangID & 0x3FFU);
}
internal static uint MapVirtualKey3(uint uCode, uint uMapType, IntPtr hKL)
{
if(hKL == IntPtr.Zero) return MapVirtualKey(uCode, uMapType);
return MapVirtualKeyEx(uCode, uMapType, hKL);
}
internal static ushort VkKeyScan3(char ch, IntPtr hKL)
{
if(hKL == IntPtr.Zero) return VkKeyScan(ch);
return VkKeyScanEx(ch, hKL);
}
/// <returns>
/// Null, if there exists no translation or an error occured.
/// An empty string, if the key is a dead key.
/// Otherwise, the generated Unicode string (typically 1 character,
/// but can be more when a dead key is stored in the keyboard layout).
/// </returns>
internal static string ToUnicode3(int vKey, byte[] pbKeyState, IntPtr hKL)
{
const int cbState = 256;
IntPtr pState = IntPtr.Zero;
try
{
uint uScanCode = MapVirtualKey3((uint)vKey, MAPVK_VK_TO_VSC, hKL);
pState = Marshal.AllocHGlobal(cbState);
if(pState == IntPtr.Zero) { Debug.Assert(false); return null; }
if(pbKeyState != null)
{
if(pbKeyState.Length == cbState)
Marshal.Copy(pbKeyState, 0, pState, cbState);
else { Debug.Assert(false); return null; }
}
else
{
// Windows' GetKeyboardState function does not return
// the current virtual key array; as a workaround,
// calling GetKeyState is mentioned sometimes, but
// this doesn't work reliably either;
// http://pinvoke.net/default.aspx/user32/GetKeyboardState.html
// GetKeyState(VK_SHIFT);
// if(!GetKeyboardState(pState)) { Debug.Assert(false); return null; }
Debug.Assert(false);
return null;
}
const int cchUni = 30;
StringBuilder sbUni = new StringBuilder(cchUni + 2);
int r;
if(hKL == IntPtr.Zero)
r = ToUnicode((uint)vKey, uScanCode, pState, sbUni,
cchUni, 0);
else
r = ToUnicodeEx((uint)vKey, uScanCode, pState, sbUni,
cchUni, 0, hKL);
if(r < 0) return string.Empty; // Dead key
if(r == 0) return null; // No translation
string str = sbUni.ToString();
if(string.IsNullOrEmpty(str)) { Debug.Assert(false); return null; }
// Extra characters may be returned, but are invalid
// and should be ignored;
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms646320.aspx
if(r < str.Length) str = str.Substring(0, r);
return str;
}
catch(Exception) { Debug.Assert(false); }
finally { if(pState != IntPtr.Zero) Marshal.FreeHGlobal(pState); }
return null;
}
}
}
|