在VB .NET中调用CreateFile API [英] CreateFile API call in VB .NET
问题描述
我正在尝试创建软盘的磁盘映像。由于我无法使用system.io方法打开设备,我正在尝试使用CreateFile
API为我获取句柄。但是呼叫失败了(返回-1)。
这里是代码:
Const GENERIC_READ =& H80000000
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ =& H1
Const FILE_SHARE_WRITE =& H2
Const FILE_ATTRIBUTE_NORMAL =& H80
Const FILE_FLAG_NO_BUFFERING =& H20000000
声明函数CreateFile Lib" kernel32"
Alias" CreateFileA" (ByVal lpFileName As String,_
ByVal dwDesiredAccess As Long,_
ByVal dwShareMode As Long,_
ByVal lpSecurityAttributes As Long,_
ByVal dwCreationDistribution As Long,_
ByVal dwFlagsAndAttributes As Long,_
ByVal hTemplateFile As Long)作为IntPtr
Dim fh As IntPtr = CreateFile(" \\.\A:",_
GENERIC_READ,_
FILE_SHARE_READ或FILE_SHARE_WRITE,_
0,_
OPEN_EXISTING,_
FILE_ATTRIBUTE_NORMAL或FILE_FLAG_NO_BUFFERING,_
0)
我希望有人可以告诉我这里有什么问题,并帮我解决这个问题所以我
得到一个有效的手柄。
>
***通过开发人员指南 http://www.developersdex.com 发送***
I''m trying to create a disk image of a floppy disk. Since I can''t open
the device using the system.io methods, i''m trying to use the CreateFile
API to get a handle for me. But the call fails (returning a -1).
Here''s the code:
Const GENERIC_READ = &H80000000
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_FLAG_NO_BUFFERING = &H20000000
Declare Function CreateFile Lib "kernel32"
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDistribution As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As IntPtr
Dim fh As IntPtr = CreateFile("\\.\A:", _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
0, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, _
0)
I''m hoping someone can tell me what''s wrong here and help me fix it so I
get a valid handle returned.
*** Sent via Developersdex http://www.developersdex.com ***
推荐答案
" Terry Olsen" <到****** @ hotmail.com> schrieb:
"Terry Olsen" <to******@hotmail.com> schrieb:
我正在尝试创建一张软盘的磁盘映像。由于我无法使用system.io方法打开设备,我正在尝试使用CreateFile
API为我获取句柄。但是调用失败了(返回-1)。
这里是代码:
Const GENERIC_READ =& H80000000
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ =& H1
Const FILE_SHARE_WRITE =& H2
Const FILE_ATTRIBUTE_NORMAL =& H80
Const FILE_FLAG_NO_BUFFERING =& H20000000
声明全部常量为''Int32''。
声明函数CreateFile Lib" kernel32"
Alias" CreateFileA" (ByVal lpFileName As String,_
ByVal dwDesiredAccess As Long,_
ByVal dwShareMode As Long,_
ByVal lpSecurityAttributes As Long,_
ByVal dwCreationDistribution As Long,_
ByVal dwFlagsAndAttributes As Long,_
ByVal hTemplateFile As Long)作为IntPtr
I''m trying to create a disk image of a floppy disk. Since I can''t open
the device using the system.io methods, i''m trying to use the CreateFile
API to get a handle for me. But the call fails (returning a -1).
Here''s the code:
Const GENERIC_READ = &H80000000
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_FLAG_NO_BUFFERING = &H20000000
Declare all of the constants as ''Int32''.
Declare Function CreateFile Lib "kernel32"
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDistribution As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As IntPtr
使用此声明代替(未经测试):
\\\
进口System.Runtime.InteropServices
..
..
..
私有声明自动功能CreateFile Lub" kernel32.dll" (_
ByVal lpFileName As String,_
ByVal dwDesiredAccess As Int32,_
ByVal dwShareMode As Int32,_
ByRef lpSecurityAttributes作为SECURITY_ATTRIBUTES,_
ByVal dwCreationDistribution作为Int32,_
ByVal dwFlagsAndAttributes作为Int32,_
ByVal hTemplateFile作为IntPtr _
)作为IntPtr
私有结构SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
结束结构
///
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>
Use this declaration instead (untested):
\\\
Imports System.Runtime.InteropServices
..
..
..
Private Declare Auto Function CreateFile Lub "kernel32.dll" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDistribution As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr _
) As IntPtr
<StructLayout(LayoutKind.Sequential)> _
Private Structure SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
从Err.lastDLLError返回的错误代码是87,它将
等同于参数不正确。现在我们在谈论哪个参数
左右?仍然无法弄明白。
***通过Developersdex发送 http://www.developersdex.com ***
The error code being returned from Err.lastDLLError is 87, which equates
to "The parameter is incorrect." Now which parameter are we talking
about? Still can''t figure it out.
*** Sent via Developersdex http://www.developersdex.com ***
试一试,
声明自动函数CreateFile Lib" kernel32.dll" (ByVal lpFileName As
String,_
ByVal dwDesiredAccess As Integer,ByVal dwShareMode As Integer,_
ByVal lpSecurityAttributes As IntPtr,ByVal dwCreationDisposition As
整数,_
ByVal dwFlagsAndAttributes As Integer,ByVal hTemplateFile as IntPtr)
As IntPtr
Dim handle As IntPtr
handle = CreateFile(" \\.\\ A:",GENERIC_READ,FILE_SHARE_READ或
FILE_SHARE_WRITE,IntPtr.Zero ,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero)
" Terry Olsen" <到****** @ hotmail.com>在消息中写道
news:e9 ************** @ TK2MSFTNGP15.phx.gbl ...
Give this a try,
Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As
String, _
ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As
Integer, _
ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr)
As IntPtr
Dim handle As IntPtr
handle = CreateFile("\\.\\A:", GENERIC_READ, FILE_SHARE_READ Or
FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero)
"Terry Olsen" <to******@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl...
返回的错误代码来自Err.lastDLLError的是87,它将
等同于参数不正确。现在我们在谈论哪个参数?仍然无法弄清楚。
***通过开发人员指南 http发送://www.developersdex.com ***
The error code being returned from Err.lastDLLError is 87, which equates
to "The parameter is incorrect." Now which parameter are we talking
about? Still can''t figure it out.
*** Sent via Developersdex http://www.developersdex.com ***
这篇关于在VB .NET中调用CreateFile API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!