Translation of CreateFile dll in dot net

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hello,

i'm having problems to call a api dll in vb dot net.
I absolutely want to use this api call and none of the frame calls.

This is my declaration:
*****************
Public Structure SECURITY_ATTRIBUTES
Public nLength As UInt32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Int32
End Structure

' Desired Access
Public Const FILE_READ_DATA = 1
Public Const FILE_WRITE_DATA = 2
Public Const FILE_APPEND_DATA = 4

' ShareMode
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2

' CreationDisposition
Public Const CREATE_NEW = 1
Public Const OPEN_EXISTING = 3

Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As IntPtr) _
As Long


This is the call:
***********

Dim lFile As Long = LibWrap.CreateFile("c:\test\myfile.dat",
LibWrap.FILE_READ_DATA, LibWrap.FILE_SHARE_READ, Nothing,
LibWrap.OPEN_EXISTING, 0, Nothing)


Anyone an idea?
This is my first api call.

Grtz.
 
You need to be aware of data type mapping between .Net and the API calls and also be aware that if you are using declarations from
VB5/6 that a VB5/6 Long maps to a VB.Net Integer.

Without looking in any detail, first try changing all Long to Integer in the CreateFile declaration.
 
I've changed the code with no luck:

Declaration:
*********
Const FILE_READ_DATA = 1
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80

<StructLayout(LayoutKind.Sequential)> _
Structure SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure

Declare Auto Function CreateFile Lib "kernel32" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByRef lpSecurityAttributes As IntPtr, _
ByVal dwCreationDistribution As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr _
) As IntPtr

Call:
****
Dim fh As IntPtr

fh = CreateFile("c:\test\test.txt", FILE_READ_DATA, FILE_SHARE_READ,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)

If fh.ToInt32 = -1 Then
MessageBox.Show("Invalid")
End If


I don't see what's wrong ...

Al Reid said:
You need to be aware of data type mapping between .Net and the API calls
and also be aware that if you are using declarations from
 
That's because you changed the Longs to Int32, which was no change at all. You need to use either Integer or Int16. A DWORD is two
(2) 8 bit words or a 16-bit integer.
 
I tried it but that doesn't work either.
thanks.

Al Reid said:
That's because you changed the Longs to Int32, which was no change at all.
You need to use either Integer or Int16. A DWORD is two
 
news.microsoft.com said:
i'm having problems to call a api dll in vb dot net.
I absolutely want to use this api call and none of the frame calls.

Any reason for using 'CreateFile' instead of .NET's 'System.IO.FileStream'
class or similar classes?
 
See inline

Al Reid said:
That's because you changed the Longs to Int32, which was no change at all.
You need to use either Integer or Int16. A DWORD is two
(2) 8 bit words or a 16-bit integer.

Ehh, no. A DWORD is 32 bits, same as Long in VB6 and Int32/Integer in VB.NET


Try this:

Const FILE_READ_DATA As Int32 = 1
Const OPEN_EXISTING As Int32 = 3
Const FILE_SHARE_READ As Int32 = &H1
Const FILE_SHARE_WRITE As Int32 = &H2
Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80

<StructLayout(LayoutKind.Sequential)> _
Structure SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure

Declare Auto Function CreateFile Lib "kernel32" ( _
<MarshalAs(UnmanagedType.LPTStr)> 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

If this doesn't work try changing the lpSecurityAttributes parameter to
ByVal instead of ByRef

/claes
 
Sorry, ehh, you're right. My bad. Anyhow, when converting VB5/6 Declarations, the VB5/6 Long converts to a VB.Net Integer.

Also the SECURITY_ATTRIBUTES structure needs to be adjusted as well.
 
That did it.
Thank you all.

Claes Bergefall said:
See inline



Ehh, no. A DWORD is 32 bits, same as Long in VB6 and Int32/Integer in VB.NET

Try this:

Const FILE_READ_DATA As Int32 = 1
Const OPEN_EXISTING As Int32 = 3
Const FILE_SHARE_READ As Int32 = &H1
Const FILE_SHARE_WRITE As Int32 = &H2
Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80

<StructLayout(LayoutKind.Sequential)> _
Structure SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure

Declare Auto Function CreateFile Lib "kernel32" ( _
<MarshalAs(UnmanagedType.LPTStr)> 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

If this doesn't work try changing the lpSecurityAttributes parameter to
ByVal instead of ByRef

/claes
 
Back
Top