By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Translation of CreateFile dll in dot net

P: n/a
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.
Mar 29 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message news:u8**************@tk2msftngp13.phx.gbl...
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.

Mar 29 '06 #2

P: n/a
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" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.
--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message

news:u8**************@tk2msftngp13.phx.gbl...
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.


Mar 29 '06 #3

P: n/a
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.

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message news:eC**************@tk2msftngp13.phx.gbl...
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" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message

news:u8**************@tk2msftngp13.phx.gbl...
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.



Mar 29 '06 #4

P: n/a
I tried it but that doesn't work either.
thanks.

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message

news:eC**************@tk2msftngp13.phx.gbl...
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" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message

news:u8**************@tk2msftngp13.phx.gbl...
> 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.
>
>



Mar 29 '06 #5

P: n/a
"news.microsoft.com" <Yv**********@hotmail.com> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 29 '06 #6

P: n/a
See inline

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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 ...


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
Mar 29 '06 #7

P: n/a
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.

--
Al Reid

"Claes Bergefall" <cl*************@nospam.nospam> wrote in message news:em**************@tk2msftngp13.phx.gbl...
See inline

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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 ...


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

Mar 29 '06 #8

P: n/a
That did it.
Thank you all.

"Claes Bergefall" <cl*************@nospam.nospam> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
See inline

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
--
Al Reid

"news.microsoft.com" <Yv**********@hotmail.com> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
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 ...


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

Mar 30 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.