标签: deviceiocontrol

在线/离线获取磁盘

我有一个程序正在对Windows中的磁盘执行原始IO.

如果目标磁盘在线,则一切正常.但是,某些Windows操作系统中的默认行为是使新磁盘最初脱机.

我很难找到在Windows上执行此操作的正确API.等效的命令行将是这样的:

"select disk 2", "online disk" | diskpart
Run Code Online (Sandbox Code Playgroud)

但是我需要能够在代码中执行此操作.我查看了DeviceIoControl Win32 API(我认为是正确的)但无法确定要使用的控件代码.我无法找到它的事实让我觉得我可能错过了一个更好的API来使用.

c# winapi deviceiocontrol

5
推荐指数
1
解决办法
3755
查看次数

为什么在C#中读取的原始磁盘会从稍微偏移的偏移中读取?

我正在尝试读取原始磁盘。我已经成功打开驱动器并获得了有效的句柄(CreateFile),将该句柄的偏移量设置为零(SetFilePointerEx),并将数据读取到缓冲区Byte[]ReadFile)。到目前为止,一切都很好。但是由于某种未知的原因,当我将缓冲区(花一点时间来实际弄清楚)与第三者实用程序(Disk Investigator)显示的内容进行比较时。它不包含启动信息(跳转指令),但包含正确的磁盘数据,但从偏移量85 = 0x55开始。因此,在卷启动信息中间。

这是为什么?有什么东西,我想念(显然是)吗?

系统:Windows 8(在VmWare工作站中)

码:

// moves the pointer to a given offset
Int64 offset = 0; 
Int64 newOffset;
Int32 bufferSize = (Int32) clusterSizeInBytes; 
SetFilePointerEx(driveHandle.Handle, offset, out newOffset, WinMoveMethod.Begin);
Int32 error = Marshal.GetLastWin32Error();

// reads the raw buffer
Int32 numberOfBytesRead;
rawData = new Byte[bufferSize];
Boolean result = ReadFile(driveHandle.Handle, rawData, bufferSize, 
                          out numberOfBytesRead, IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)

CreateFile(在其他位置)

driveHandle = CreateFile("\\.\PhysicalDrive1", WinFileAccessMode.GenericRead, 
           WinFileSharedAccess.All, IntPtr.Zero, WinFileMode.OpenExisting,
           WinFileAttribute.None, IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)

PInvoke方法:

[DllImport("kernel32.dll", …
Run Code Online (Sandbox Code Playgroud)

c# winapi deviceiocontrol

5
推荐指数
1
解决办法
2938
查看次数

无法获取 OneDrive 文件夹的重新分析点信息

我使用下面的代码在我的应用程序中检索重新分析点信息。这对于符号链接和连接非常有效,但对于 OneDrive 文件夹及其所有子项目来说会失败,并显示“不是重新分析点”。

   using (SafeFileHandle srcHandle = NativeMethods.CreateFile(@"C:\Users\UserName\OneDrive",
                                                              0,
                                                              System.IO.FileShare.Read,
                                                              IntPtr.Zero,
                                                              System.IO.FileMode.Open,
                                                              NativeMethods.FileFlags.BackupSemantics | NativeMethods.FileFlags.OpenReparsePoint,
                                                              IntPtr.Zero))
    {
         if (!srcHandle.IsInvalid)
         {
              NativeMethods.REPARSE_DATA_BUFFER rdb = new NativeMethods.REPARSE_DATA_BUFFER();
              IntPtr pMem = Marshal.AllocHGlobal(Marshal.SizeOf(rdb) + sizeof(uint) + sizeof(ushort) + sizeof(ushort) + 0xFFFF);

              var outBufferSize = Marshal.SizeOf(typeof(NativeMethods.REPARSE_DATA_BUFFER));
              var outBuffer = Marshal.AllocHGlobal(outBufferSize);

              // Determine if it's a symbolic link or a junction point
              try
              {
                   int bytesRet = 0;
                   if (NativeMethods.DeviceIoControl(srcHandle, NativeMethods.FSCTL_GET_REPARSE_POINT, IntPtr.Zero, 0, outBuffer, outBufferSize, ref bytesRet, IntPtr.Zero) != 0)
                   {
                        rdb = (NativeMethods.REPARSE_DATA_BUFFER)Marshal.PtrToStructure(pMem, rdb.GetType());
                        ... …
Run Code Online (Sandbox Code Playgroud)

c# winapi reparsepoint deviceiocontrol windows-10

4
推荐指数
1
解决办法
2140
查看次数

3
推荐指数
1
解决办法
2741
查看次数

如何在 Win7 中从 C# .Net 断开蓝牙设备

我想从在 Win 7 x64 上运行的 c# .Net 应用程序断开蓝牙设备。

我知道 MS 在 .Net 上提供的有关 BT 的功能非常少。

我搜索了 32feet.Net,并找到了如何连接、发现、获取信息……但没有找到有关断开连接的信息(我是否错过了什么?)。

然后,我在 Msdn IOCTL_BTH_DISCONNECT_DEVICE上找到了。问题是我不明白如何调用它。看来我应该将DeviceIOControl与 Platform Invoke 一起使用,但恐怕我没有足够的 .Net 技能来自己构建这个。

这是我现在所处的位置:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;

namespace BtDisco
{
    class Program
    {
        const int IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;

        [DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
        public static extern bool DeviceIoControl(
            Microsoft.Win32.SafeHandles.SafeFileHandle hDevice,
            uint dwIoControlCode,
            [MarshalAs(UnmanagedType.AsAny)] [In] object InBuffer,
            uint nInBufferSize,
            [MarshalAs(UnmanagedType.AsAny)] [Out] object OutBuffer, …
Run Code Online (Sandbox Code Playgroud)

.net c# pinvoke bluetooth deviceiocontrol

3
推荐指数
1
解决办法
3636
查看次数

DeviceIoControl对内存位置的无效访问

当我运行以下代码时,我无法访问内存位置

WIN32_FIND_DATAW FD;
WCHAR cPath[MAX_PATH], cFindPath[MAX_PATH];

if (!GetCurrentDirectoryW(MAX_PATH, cPath))
    ErrorExit("GetCurrentDirectory");
else
    printf("Current Path: %s\n", cPath);

StringCchCopyW(cFindPath, MAX_PATH, cPath);
StringCchCatW(cFindPath, MAX_PATH, L"\\*");

BOOL bsuccess;

HANDLE hFind = FindFirstFileW(cFindPath, &FD);

if (INVALID_HANDLE_VALUE != hFind)
{
    vector<wstring> links;
    do
    {
        WCHAR  fullFileName[MAX_PATH];
        StringCchPrintfW(fullFileName, MAX_PATH, L"\\\\?\\%s\\%s", cPath, FD.cFileName);

        DWORD dwBufSize = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
        REPARSE_DATA_BUFFER* rdata;
        rdata = (REPARSE_DATA_BUFFER*)malloc(dwBufSize);

        DWORD bytesReturned;
        HANDLE hFile = CreateFileW(fullFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
        if (INVALID_HANDLE_VALUE != hFile)
        {
            if (DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, NULL, …
Run Code Online (Sandbox Code Playgroud)

c++ winapi reparsepoint deviceiocontrol

1
推荐指数
1
解决办法
329
查看次数

当我尝试通过DeviceIoControl获取物理扇区大小时,我收到Access被拒绝

从我的网络服务器应用程序,我需要检查应用程序所在的硬盘的物理扇区大小.为此,我使用DeviceIoControlIOCTL_STORAGE_QUERY_PROPERTY查询StorageAccessAlignmentProperty.问题是,当我尝试从网络服务器运行这些命令时,我得到" 访问被拒绝 "错误.

如何从webserver应用程序中检索inetpub所在的硬盘的物理扇区大小?

我从https://msdn.microsoft.com/windows/compatibility/advanced-format-disk-compatibility-update了解到,在Windows 8中,Microsoft引入了一个新的API,可以从一个非特权的应用程序启用Calling.API是一个新的信息类的形式,FileFsSectorSizeInformation具有相关的结构FILE_FS_SECTOR_SIZE_INFORMATION,但我不知道如何使它与Delphi一起工作

这是我的实际代码不起作用(用Delphi编写):

{~~~~~~~~~~~~~~~~~~~~~~~~~}
procedure _CheckSectorSize;

type
  STORAGE_PROPERTY_ID  = (StorageDeviceProperty = 0,
                          StorageAdapterProperty,
                          StorageDeviceIdProperty,
                          StorageDeviceUniqueIdProperty,
                          StorageDeviceWriteCacheProperty,
                          StorageMiniportProperty,
                          StorageAccessAlignmentProperty,
                          StorageDeviceSeekPenaltyProperty,
                          StorageDeviceTrimProperty,
                          StorageDeviceWriteAggregationProperty,
                          StorageDeviceDeviceTelemetryProperty,
                          StorageDeviceLBProvisioningProperty,
                          StorageDevicePowerProperty,
                          StorageDeviceCopyOffloadProperty,
                          StorageDeviceResiliencyProperty,
                          StorageDeviceMediumProductType,
                          StorageAdapterCryptoProperty,
                          StorageDeviceIoCapabilityProperty = 48,
                          StorageAdapterProtocolSpecificProperty,
                          StorageDeviceProtocolSpecificProperty,
                          StorageAdapterTemperatureProperty,
                          StorageDeviceTemperatureProperty,
                          StorageAdapterPhysicalTopologyProperty,
                          StorageDevicePhysicalTopologyProperty,
                          StorageDeviceAttributesProperty);
  STORAGE_QUERY_TYPE  = (PropertyStandardQuery = 0,
                         PropertyExistsQuery = 1,
                         PropertyMaskQuery = 2,
                         PropertyQueryMaxDefined = 3);
  _STORAGE_PROPERTY_QUERY = packed record
    PropertyId: STORAGE_PROPERTY_ID;
    QueryType: STORAGE_QUERY_TYPE;
    AdditionalParameters: array[0..9] of Byte; …
Run Code Online (Sandbox Code Playgroud)

windows delphi iis winapi deviceiocontrol

0
推荐指数
1
解决办法
426
查看次数