u 디스크 사용 안 함 다시 사용

4268 단어
u디스크를 CDROM으로 양산한 후 ISO에 삽입한 후 u디스크를 다시 삽입해야 새로운 내용에 접근할 수 있습니다.이 글에서 보여준 코드는 윈도우즈가 cdrom을 다시 읽을 수 있도록 삽입하지 않고 시뮬레이션할 수 있습니다.
인터넷 참고 자료에 한계가 있어서 스스로 여러 가지 방법을 시험하여 마침내 성공하였다.
 
먼저 DeviceIoControl을 호출하여 디바이스를 마운트 해제합니다.그리고 CM 으로Disable_DevNode에서 장치를 비활성화하고 CMEnable_DevNode 는 디바이스를 활성화합니다.
c# 키 코드:
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const int FILE_SHARE_READ = 1;
public const int OPEN_EXISTING = 3;
public const int FILE_ATTRIBUTE_NORMAL = 0x80;

public const int FSCTL_IS_VOLUME_MOUNTED = 0x00090028;
public const int FSCTL_DISMOUNT_VOLUME = 0x00090020; public const int CM_DISABLE_HARDWARE = 0x00000002; public struct DiskExtent { public uint DiskNumber; public long StartingOffset; public long ExtentLength; } public struct DiskExtents { public int numberOfExtents; public DiskExtent[] first; } [StructLayout(LayoutKind.Sequential)] class SP_DEVINFO_DATA { public int cbSize; public Guid ClassGuid; public int DevInst; public ulong Reserved; } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport("CfgMgr32.dll", SetLastError = true)] public static extern int CM_Enable_DevNode(ref SafeFileHandle pdnDevInst, int ulFlags); [DllImport("CfgMgr32.dll", SetLastError = true)] public static extern int CM_Disable_DevNode(ref SafeFileHandle pdnDevInst, int ulFlags); private SafeFileHandle getHandleByLetter(char letter) { string lpFileName; lpFileName = "\\\\.\\" + letter.ToString() + ":"; SafeFileHandle hDevice = CreateFile(lpFileName, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); return hDevice; } SafeFileHandle hDevice = getHandleByLetter(CDROMLetter[i]); int bytesReturned = 0; DiskExtents diskExtents = new DiskExtents(); DeviceIoControl(hDevice, FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, ref diskExtents, Marshal.SizeOf(diskExtents), ref bytesReturned, IntPtr.Zero); CM_Disable_DevNode(ref hDevice, CM_DISABLE_HARDWARE); CM_Enable_DevNode(ref hDevice, 0); hDevice.Close();

 
Golang 참조
package main

import (
	"syscall"
)

var (
	CfgMgr32           = syscall.NewLazyDLL("CfgMgr32.dll")
	CM_Disable_DevNode = CfgMgr32.NewProc("CM_Disable_DevNode")
	CM_Enable_DevNode  = CfgMgr32.NewProc("CM_Enable_DevNode")
)

const FSCTL_DISMOUNT_VOLUME = 0x00090020
const CM_DISABLE_HARDWARE = 0x00000002

func main() {
	var hDev syscall.Handle
	disk := "\\\\.\\G:"
	hDev, _ = syscall.Open(disk, syscall.O_RDONLY, 0777)
	syscall.DeviceIoControl(hDev, FSCTL_DISMOUNT_VOLUME, nil, 0, nil, 0, nil, nil)
	CM_Disable_DevNode.Call(uintptr(hDev), CM_DISABLE_HARDWARE)
	CM_Enable_DevNode.Call(uintptr(hDev), uintptr(0))
}

 
c++ 참조
#include 

#pragma comment(lib, "Cfgmgr32")

...
DeviceIoControl(hDev, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, NULL, NULL);
CM_Disable_DevNode(DEVINST(hDev), CM_DISABLE_HARDWARE);
CM_Enable_DevNode(DEVINST(hDev), NULL);
...

 
참조 링크:
https://blog.csdn.net/bhw98/article/details/19662?tdsourcetag=s_pctim_aiomsg
https://docs.microsoft.com/en-us/windows/desktop/api/cfgmgr32/nf-cfgmgr32-cm_disable_devnode
https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.win32.safehandles.safefilehandle?view=netframework-4.8
https://stackoverflow.com/questions/37532548/deviceiocontrol-with-ioctl-volume-get-volume-disk-extents-c-sharp

좋은 웹페이지 즐겨찾기