Changeset 2174
- Timestamp:
- 6/13/2010 2:32:41 PM (3 years ago)
- Location:
- trunk/eraser/Eraser.Util
- Files:
-
- 1 added
- 3 edited
-
Eraser.Util.csproj (modified) (1 diff)
-
NativeMethods/Kernel.cs (modified) (1 diff)
-
PhysicalDriveInfo.cs (added)
-
Win32ErrorCodes.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser/Eraser.Util/Eraser.Util.csproj
r2067 r2174 116 116 <Compile Include="NativeMethods\Mpr.cs" /> 117 117 <Compile Include="NativeMethods\WinUser.cs" /> 118 <Compile Include="PhysicalDriveInfo.cs" /> 118 119 <Compile Include="PostDataBuilder.cs" /> 119 120 <Compile Include="Power.cs" /> -
trunk/eraser/Eraser.Util/NativeMethods/Kernel.cs
r2155 r2174 484 484 public const uint FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000; 485 485 486 /// <summary> 487 /// Defines, redefines, or deletes MS-DOS device names. 488 /// </summary> 489 /// <param name="dwFlags">The controllable aspects of the DefineDosDevice function. This 490 /// parameter can be one or more of the DosDeviceDefineFlags.</param> 491 /// <param name="lpDeviceName">A pointer to an MS-DOS device name string specifying the 492 /// device the function is defining, redefining, or deleting. The device name string must 493 /// not have a colon as the last character, unless a drive letter is being defined, 494 /// redefined, or deleted. For example, drive C would be the string "C:". In no case is 495 /// a trailing backslash ("\") allowed.</param> 496 /// <param name="lpTargetPath">A pointer to a path string that will implement this 497 /// device. The string is an MS-DOS path string unless the DDD_RAW_TARGET_PATH flag 498 /// is specified, in which case this string is a path string.</param> 499 /// <returns>If the function succeeds, the return value is true. 500 /// 501 /// If the function fails, the return value is zero. To get extended error 502 /// information, call Marshal.GetLastWin32Error.</returns> 503 [DllImport("Kernel32.dll", SetLastError = true)] 504 [return: MarshalAs(UnmanagedType.Bool)] 505 public extern static bool DefineDosDevice(DosDeviceDefineFlags dwFlags, 506 string lpDeviceName, string lpTargetPath); 507 508 [Flags] 509 public enum DosDeviceDefineFlags 510 { 511 /// <summary> 512 /// If this value is specified along with DDD_REMOVE_DEFINITION, the function will 513 /// use an exact match to determine which mapping to remove. Use this value to 514 /// ensure that you do not delete something that you did not define. 515 /// </summary> 516 ExactMatchOnRmove = 0x00000004, 517 518 /// <summary> 519 /// Do not broadcast the WM_SETTINGCHANGE message. By default, this message is 520 /// broadcast to notify the shell and applications of the change. 521 /// </summary> 522 NoBroadcastSystem = 0x00000008, 523 524 /// <summary> 525 /// Uses the lpTargetPath string as is. Otherwise, it is converted from an MS-DOS 526 /// path to a path. 527 /// </summary> 528 RawTargetPath = 0x00000001, 529 530 /// <summary> 531 /// Removes the specified definition for the specified device. To determine which 532 /// definition to remove, the function walks the list of mappings for the device, 533 /// looking for a match of lpTargetPath against a prefix of each mapping associated 534 /// with this device. The first mapping that matches is the one removed, and then 535 /// the function returns. 536 /// 537 /// If lpTargetPath is NULL or a pointer to a NULL string, the function will remove 538 /// the first mapping associated with the device and pop the most recent one pushed. 539 /// If there is nothing left to pop, the device name will be removed. 540 /// 541 /// If this value is not specified, the string pointed to by the lpTargetPath 542 /// parameter will become the new mapping for this device. 543 /// </summary> 544 RemoveDefinition = 0x00000002 545 } 546 547 /// <summary> 548 /// Retrieves information about MS-DOS device names. The function can obtain the 549 /// current mapping for a particular MS-DOS device name. The function can also obtain 550 /// a list of all existing MS-DOS device names. 551 /// 552 /// MS-DOS device names are stored as junctions in the object name space. The code 553 /// that converts an MS-DOS path into a corresponding path uses these junctions to 554 /// map MS-DOS devices and drive letters. The QueryDosDevice function enables an 555 /// application to query the names of the junctions used to implement the MS-DOS 556 /// device namespace as well as the value of each specific junction. 557 /// </summary> 558 /// <param name="lpDeviceName">An MS-DOS device name string specifying the target of 559 /// the query. The device name cannot have a trailing backslash; for example, 560 /// use "C:", not "C:\". 561 /// 562 /// This parameter can be NULL. In that case, the QueryDosDevice function will 563 /// store a list of all existing MS-DOS device names into the buffer pointed to 564 /// by lpTargetPath.</param> 565 /// <param name="lpTargetPath">A pointer to a buffer that will receive the result 566 /// of the query. The function fills this buffer with one or more null-terminated 567 /// strings. The final null-terminated string is followed by an additional NULL. 568 /// 569 /// If lpDeviceName is non-NULL, the function retrieves information about the 570 /// particular MS-DOS device specified by lpDeviceName. The first null-terminated 571 /// string stored into the buffer is the current mapping for the device. The other 572 /// null-terminated strings represent undeleted prior mappings for the device. 573 /// 574 /// If lpDeviceName is NULL, the function retrieves a list of all existing MS-DOS 575 /// device names. Each null-terminated string stored into the buffer is the name 576 /// of an existing MS-DOS device, for example, \Device\HarddiskVolume1 or 577 /// \Device\Floppy0.</param> 578 /// <param name="length">The maximum number of TCHARs that can be stored into 579 /// the buffer pointed to by lpTargetPath.</param> 580 /// <returns>If the function succeeds, the return value is the number of TCHARs 581 /// stored into the buffer pointed to by lpTargetPath. 582 /// 583 /// If the function fails, the return value is zero. To get extended error 584 /// information, call Marshal.GetLastWin32Error. 585 /// 586 /// If the buffer is too small, the function fails and the last error code is 587 /// ERROR_INSUFFICIENT_BUFFER.</returns> 588 [DllImport("Kernel32.dll", SetLastError = true)] 589 private static extern uint QueryDosDevice([Optional] string lpDeviceName, 590 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] [Out] char[] lpTargetPath, int length); 591 592 private static string[] QueryDosDeviceInternal(string lpDeviceName) 593 { 594 char[] buffer = new char[32768]; 595 for ( ; ; buffer = new char[buffer.Length * 2]) 596 { 597 uint written = NativeMethods.QueryDosDevice(lpDeviceName, buffer, buffer.Length); 598 599 //Do we have enough space for all the text 600 if (written != 0) 601 break; 602 else if (Marshal.GetLastWin32Error() == Win32ErrorCode.InsufficientBuffer) 603 continue; 604 else 605 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 606 } 607 608 List<string> result = new List<string>(); 609 for (int lastIndex = 0, i = 0; i != buffer.Length; ++i) 610 { 611 if (buffer[i] == '\0') 612 { 613 //If there are no mount points for this volume, the string will only 614 //have one NULL 615 if (i - lastIndex == 0) 616 break; 617 618 //Resolve the DOS name to the device name 619 result.Add(new string(buffer, lastIndex, i - lastIndex)); 620 621 lastIndex = i + 1; 622 if (buffer[lastIndex] == '\0') 623 break; 624 } 625 } 626 627 return result.ToArray(); 628 } 629 630 public static string QueryDosDevice(string lpDeviceName) 631 { 632 string[] result = QueryDosDeviceInternal(lpDeviceName); 633 return result.Length == 0 ? null : result[0]; 634 } 635 636 public static string[] QueryDosDevices() 637 { 638 return QueryDosDeviceInternal(null); 639 } 640 486 641 [DllImport("Kernel32.dll", SetLastError = true)] 487 642 [return: MarshalAs(UnmanagedType.Bool)] -
trunk/eraser/Eraser.Util/Win32ErrorCodes.cs
r2165 r2174 89 89 public const int InvalidParameter = 87; 90 90 public const int DiskFull = 112; 91 public const int InsufficientBuffer = 122; 91 92 public const int MoreData = 234; 92 93 public const int NoMoreItems = 259;
Note: See TracChangeset
for help on using the changeset viewer.
