问题描述
最近想用vb.net做一个卸载u盘的程序,但是网上这方面的程序都是用同一个vb模块,里面有很多vb.net已经不支持或者不适用的函数。但是,以我的水平,还有不少看不懂的东西,没有办法重新写。请问可不可以帮忙解释一下这个模块,或者提供一些适合.net环境的方法?代码是c#或者vb.net都可以(因为某些原因,不能用vb6.0)模块见1楼
解决方案
解决方案二:
OptionExplicitPrivateTypeGUIDData1AsLongData2AsIntegerData3AsIntegerData4(0To7)AsByteEndTypePrivateTypeSP_DEVICE_INTERFACE_DETAIL_DATAcbSizeAsLongstrDevicePathAsString*260EndTypePrivateTypeSP_DEVICE_INTERFACE_DATAcbSizeAsLongInterfaceClassGuidAsGUIDflagsAsLongReservedAsLongEndTypePrivateTypeSP_DEVINFO_DATAcbSizeAsLongClassGuidAsGUIDDevInstAsLongReservedAsLongEndTypePrivateTypeSTORAGE_DEVICE_NUMBERdwDeviceTypeAsLongdwDeviceNumberAsLongdwPartitionNumberAsLongEndTypePrivateEnumPNP_VETO_TYPEPNP_VetoTypeUnknownPNP_VetoLegacyDevicePNP_VetoPendingClosePNP_VetoWindowsAppPNP_VetoWindowsServicePNP_VetoOutstandingOpenPNP_VetoDevicePNP_VetoDriverPNP_VetoIllegalDeviceRequestPNP_VetoInsufficientPowerPNP_VetoNonDisableablePNP_VetoLegacyDriverPNP_VetoInsufficientRightsEndEnumPrivateConstDIGCF_PRESENT=&H2PrivateConstDIGCF_DEVICEINTERFACE=&H10PrivateConstGENERIC_READ=&H80000000PrivateConstFILE_SHARE_READ=&H1PrivateConstOPEN_EXISTING=3PrivateConstFILE_SHARE_WRITE=&H2PrivateConstIOCTL_STORAGE_BASEAsLong=&H2DPrivateConstMETHOD_BUFFERED=0PrivateConstFILE_ANY_ACCESS=0PrivateDeclareFunctionSetupDiGetClassDevsLib"setupapi.dll"Alias"SetupDiGetClassDevsA"(ByValClassGuidAsLong,ByValEnumeratorAsLong,ByValHwndParentAsLong,ByValflagsAsLong)AsLongPrivateDeclareFunctionSetupDiEnumDeviceInterfacesLib"setupapi.dll"(ByValDeviceInfoSetAsLong,ByValDeviceInfoDataAsLong,ByRefInterfaceClassGuidAsGUID,ByValMemberIndexAsLong,ByRefDeviceInterfaceDataAsSP_DEVICE_INTERFACE_DATA)AsLongPrivateDeclareFunctionSetupDiGetDeviceInterfaceDetailLib"setupapi.dll"Alias"SetupDiGetDeviceInterfaceDetailA"(ByValDeviceInfoSetAsLong,ByRefDeviceInterfaceDataAsSP_DEVICE_INTERFACE_DATA,DeviceInterfaceDetailDataAsAny,ByValDeviceInterfaceDetailDataSizeAsLong,ByRefRequiredSizeAsLong,DeviceInfoDataAsAny)AsLongPrivateDeclareFunctionSetupDiDestroyDeviceInfoListLib"setupapi.dll"(ByValDeviceInfoSetAsLong)AsLongPrivateDeclareFunctionCreateFileLib"kernel32"Alias"CreateFileA"(ByVallpFileNameAsString,ByValdwDesiredAccessAsLong,ByValdwShareModeAsLong,lpSecurityAttributesAsAny,ByValdwCreationDispositionAsLong,ByValdwFlagsAndAttributesAsLong,ByValhTemplateFileAsLong)AsLongPrivateDeclareFunctionCM_Get_ParentLib"cfgmgr32.dll"(pdwDevInstAsLong,ByValdwDevInstAsLong,ByValulFlagsAsLong)AsLong
解决方案三:
PrivateDeclareFunctionCM_Request_Device_EjectWLib"setupapi.dll"(ByValdwDevInstAsLong,ByValpVetoTypeAsLong,ByValpszVetoNameAsString,ByValulNameLengthAsLong,ByValulFlagsAsLong)AsLongPrivateDeclareFunctionDeviceIoControlLib"kernel32"(ByValhDeviceAsLong,ByValdwIoControlCodeAsLong,lpInBufferAsAny,ByValnInBufferSizeAsLong,lpOutBufferAsAny,ByValnOutBufferSizeAsLong,lpBytesReturnedAsLong,lpOverlappedAsAny)AsLongPrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLongPrivateDeclareFunctionGetDriveTypeLib"kernel32"Alias"GetDriveTypeA"(ByValnDriveAsString)AsLongPrivateDeclareFunctionQueryDosDeviceLib"kernel32"Alias"QueryDosDeviceA"(ByVallpDeviceNameAsString,ByVallpTargetPathAsString,ByValucchMaxAsLong)AsLongPrivateDeclareSubSleepLib"kernel32"(ByValdwMillisecondsAsLong)PrivateDeclareSubCopyMemoryLib"kernel32"Alias"RtlMoveMemory"(DestinationAsAny,SourceAsAny,ByValLengthAsLong)PrivateFunctionCTL_CODE(ByVallDeviceTypeAsLong,ByVallFunctionAsLong,ByVallMethodAsLong,ByVallAccessAsLong)AsLongCTL_CODE=(lDeviceType*2^16&)Or(lAccess*2^14&)Or(lFunction*2^2)Or(lMethod)EndFunctionPrivateFunctionIOCTL_STORAGE_GET_DEVICE_NUMBER()AsLongIOCTL_STORAGE_GET_DEVICE_NUMBER=CTL_CODE(IOCTL_STORAGE_BASE,&H420,METHOD_BUFFERED,FILE_ANY_ACCESS)EndFunctionPrivateFunctionGetDrivesDevInstByDeviceNumber(ByVallngDeviceNumberAsLong,ByValuDriveTypeAsLong,ByValszDosDeviceNameAsString)AsLongDimobjGuidAsGUID,hDevInfoAsLong,dwIndexAsLong,lngResAsLong,dwSizeAsLongDimobjSpdidAsSP_DEVICE_INTERFACE_DATA,objSpddAsSP_DEVINFO_DATA,objPspdiddAsSP_DEVICE_INTERFACE_DETAIL_DATADimhDriveAsLong,objSdnAsSTORAGE_DEVICE_NUMBER,dwBytesReturnedAsLongDimdwReturnAsLongWithobjGuid.Data2=&HB6BF.Data3=&H11D0&.Data4(0)=&H94&.Data4(1)=&HF2&.Data4(2)=&H0&.Data4(3)=&HA0&.Data4(4)=&HC9&.Data4(5)=&H1E&.Data4(6)=&HFB&.Data4(7)=&H8B&SelectCaseuDriveTypeCase2IfInStr(szDosDeviceName,"Floppy")Then.Data1=&H53F56311Else.Data1=&H53F56307EndIfCase3.Data1=&H53F56307Case5.Data1=&H53F56308EndSelectEndWithhDevInfo=SetupDiGetClassDevs(VarPtr(objGuid),0,0,DIGCF_PRESENTOrDIGCF_DEVICEINTERFACE)IfhDevInfo=-1ThenGetDrivesDevInstByDeviceNumber=0ExitFunctionEndIfobjSpdid.cbSize=Len(objSpdid)DoWhile1lngRes=SetupDiEnumDeviceInterfaces(hDevInfo,0,objGuid,dwIndex,objSpdid)IflngRes=0ThenExitDodwSize=0CallSetupDiGetDeviceInterfaceDetail(hDevInfo,objSpdid,ByVal0&,0,dwSize,ByVal0&)IfdwSize<>0AnddwSize<=1024ThenobjPspdidd.cbSize=5objSpdd.cbSize=Len(objSpdd)lngRes=SetupDiGetDeviceInterfaceDetail(hDevInfo,objSpdid,objPspdidd,ByValdwSize,dwReturn,objSpdd)IflngRes>0ThenhDrive=CreateFile(objPspdidd.strDevicePath,0,FILE_SHARE_READOrFILE_SHARE_WRITE,ByVal0&,OPEN_EXISTING,0,0)IfhDrive<>-1ThenlngRes=DeviceIoControl(hDrive,IOCTL_STORAGE_GET_DEVICE_NUMBER,ByVal0&,0,objSdn,Len(objSdn),dwBytesReturned,ByVal0&)IflngResThenIflngDeviceNumber=objSdn.dwDeviceNumberThenCallCloseHandle(hDrive)SetupDiDestroyDeviceInfoListhDevInfoGetDrivesDevInstByDeviceNumber=objSpdd.DevInstExitFunctionEndIfEndIfCallCloseHandle(hDrive)EndIfEndIfEndIfdwIndex=dwIndex+1LoopCallSetupDiDestroyDeviceInfoList(hDevInfo)EndFunctionPublicFunctionRemoveUsbDrive(ByValszDosDeviceNameAsString,ByValblnIsShowNoteAsBoolean)AsBooleanDimstrDriveAsString,dwDeviceNumberAsLong,hVolumeAsLong,objSdnAsSTORAGE_DEVICE_NUMBER,dwBytesReturnedAsLongDimlngResAsLong,uDriveTypeAsLong,strDosDriveNameAsString,hDevInstAsLong,uTypeAsPNP_VETO_TYPEDimstrVetoNameAsString,blnSuccessAsBoolean,dwDevInstParentAsLong,iAsInteger,pVetoTypeAsLongstrDrive=Right(szDosDeviceName,2)dwDeviceNumber=-1hVolume=CreateFile(szDosDeviceName,0,FILE_SHARE_READOrFILE_SHARE_WRITE,ByVal0&,OPEN_EXISTING,0,0)IfhVolume=-1ThenRemoveUsbDrive=FalseExitFunctionEndIflngRes=DeviceIoControl(hVolume,IOCTL_STORAGE_GET_DEVICE_NUMBER,ByVal0&,0,objSdn,Len(objSdn),dwBytesReturned,ByVal0&)IflngResThendwDeviceNumber=objSdn.dwDeviceNumberEndIfCallCloseHandle(hVolume)IfdwDeviceNumber=-1ThenRemoveUsbDrive=FalseExitFunctionEndIfuDriveType=GetDriveType(strDrive)strDosDriveName=String(280,Chr(0))lngRes=QueryDosDevice(strDrive,strDosDriveName,280)strDosDriveName=Left(strDosDriveName,InStr(strDosDriveName,Chr(0))-1)IflngRes=0ThenRemoveUsbDrive=FalseExitFunctionEndIfhDevInst=GetDrivesDevInstByDeviceNumber(dwDeviceNumber,uDriveType,strDosDriveName)IfhDevInst=0ThenRemoveUsbDrive=FalseExitFunctionEndIfstrVetoName=String(260,Chr(0))lngRes=CM_Get_Parent(dwDevInstParent,hDevInst,0)Fori=0To3IfblnIsShowNoteThenlngRes=CM_Request_Device_EjectW(dwDevInstParent,ByValVarPtr(pVetoType),vbNullString,0,0)ElselngRes=CM_Request_Device_EjectW(dwDevInstParent,uType,strVetoName,260,0)EndIfIflngRes=0AnduType=PNP_VetoTypeUnknownThenblnSuccess=TrueExitForEndIfSleep300NextRemoveUsbDrive=blnSuccessEndFunction
解决方案四:
太长了,我是来顶你的
解决方案五:
google出来的,没仔细看,看看是否符合你的要求:EjectUSBdisksusingC#http://www.codeproject.com/KB/system/usbeject.aspx
解决方案六:
引用3楼wenh7788的回复:
太长了,我是来顶你的
me,too我也正需要呢~
解决方案七:
看了下,代码转换应该比较简单。给几个意见:1TYPE要转为structure比如PrivateTypeGUIDData1AsLongData2AsIntegerData3AsIntegerData4(0To7)AsByteEndType就改为PrivateStructureGUIDDimdata1asLongdimdata2asintegerDimdata3asintegerDimdata4(7)asByteEndStructure2数组声明做个变化比如源代码是Data4(0To7)AsByte就改为Dimdata4(7)asByte其他我建议你哪里跑不过贴哪里出来,vb6转vb.net还是比较简单的
解决方案八:
其实可以直接将这个模块写成dll,然后。net调用,完全没问题。
解决方案九:
路过,学习学习
解决方案十:
顶4楼。下下来学习学习。
解决方案十一:
我看看,来学习学习,感觉还很不错的
解决方案十二:
引用楼主sharefantasy的回复:
最近想用vb.net做一个卸载u盘的程序,但是网上这方面的程序都是用同一个vb模块,里面有很多vb.net已经不支持或者不适用的函数。但是,以我的水平,还有不少看不懂的东西,没有办法重新写。请问可不可以帮忙解释一下这个模块,或者提供一些适合.net环境的方法?代码是c#或者vb.net都可以(因为某些原因,不能用vb6.0)模块见1楼
看看這個,是否對你有幫助ForEachDiskAsStringInEnvironment.GetLogicalDrivesSelectCaseMy.Computer.FileSystem.GetDriveInfo(Disk).DriveTypeCaseIO.DriveType.CDRom'光盘设备MessageBox.Show("光盘设备"&Disk)CaseIO.DriveType.Fixed'固定磁盘MessageBox.Show("固定磁盘"&Disk)CaseIO.DriveType.Network'网络驱动器MessageBox.Show("网络驱动器"&Disk)CaseIO.DriveType.NoRootDirectory'驱动器没有根目录MessageBox.Show("驱动器没有根目录"&Disk)CaseIO.DriveType.Ram'RAM磁盘MessageBox.Show("RAM磁盘"&Disk)CaseIO.DriveType.Removable'可移动存储设备,如软盘或闪存IfInStr(Disk,"A")=0ThenMessageBox.Show("可移动存储设备,如软盘或闪存"&Disk)EndIfCase(IO.DriveType.Unknown)'类型未知EndSelectNext