Convert to VB.NET

A

Andrew Clark

Hello,

Recently, I converted my VB6 app to VB.NET. I got the standard upgrade
messages and fixed them so now I can run my app. I have noticed, though,
that some of the library functions will not work in .NET, namely:

CreateFile
LockFile
UnlockFile
CloseHandle

declared as

Public Declare Auto Function CreateFile Lib "kernel32.dll" Alias
"CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As
Int32, ByVal dwShareMode As Int32, ByRef lpSecurityAttributes As IntPtr,
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As
Int32, ByVal hTemplateFile As IntPtr) As IntPtr
Public Declare Auto Function LockFile Lib "kernel32.dll" (ByVal hFile
As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal dwFileOffsetHigh As
Int32, ByVal nNumberOfBytesToLockLow As Int32, ByVal
nNumberOfBytesToLockHigh As Int32) As Int32
Public Declare Auto Function UnlockFile Lib "kernel32.dll" (ByVal
hFile As IntPtr, ByVal dwFileOffsetLow As Int32, ByVal dwFileOffsetHigh
As Int32, ByVal nNumberOfBytesToUnlockLow As Int32, ByVal
nNumberOfBytesToUnlockHigh As Int32) As Int32
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
hObject As IntPtr) As Boolean

How do I fix this problem?

Thanks,
Andrew
 
D

David Browne

Andrew Clark said:
Hello,

Recently, I converted my VB6 app to VB.NET. I got the standard upgrade
messages and fixed them so now I can run my app. I have noticed, though,
that some of the library functions will not work in .NET, namely:

CreateFile
LockFile
UnlockFile
CloseHandle
The conversion process does not do a good job with Win32 functions. You
should either replace the Win32 API calls with the built-in functionality
from the .net frametwork, or go research the proper vb.net Win32 api
declares (www.pinvoke.net).

David
 
D

Dragon

Hi Andrew,

In CreateFile declaration change ~ ByRef lpSecurityAttibutes As IntPtr ~
either to ~ ByVal lpSecurityAttributes As IntPtr ~ or to ~ ByRef
lpSecurityAttributes As SECURITY_ATTRIBUTES ~.

Also please post code in which you use these functions.

Roman
 
A

Andrew Clark

Hi Andrew,

In CreateFile declaration change ~ ByRef lpSecurityAttibutes As IntPtr ~
either to ~ ByVal lpSecurityAttributes As IntPtr ~ or to ~ ByRef
lpSecurityAttributes As SECURITY_ATTRIBUTES ~.

Also please post code in which you use these functions.

Roman

I made the suggested change. hFile still gets -1 (and I am sure the file
exists). Here is a snippet where I use CreateFile,

Dim hFile As IntPtr
hFile = IntPtr.Zero

hFile = CreateFile( _
"test03.txt", _
GENERIC_READ, _
0, _
IntPtr.Zero, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL, _
IntPtr.Zero)

This will always set hFile to -1. The values of the constants are listed
below,

GENERIC_READ 1179785
OPEN_EXISTING 3
FILE_ATTRIBUTE_NORMAL 128

I am not finding a whole lot of help on the 'net for this :(

Andrew
 
H

Herfried K. Wagner [MVP]

In addition to the other replies:
Public Declare Auto Function CreateFile Lib "kernel32.dll" Alias
"CreateFileA"

Remove the 'Alias "CreateFileA"' alias.
Public Declare Auto Function LockFile Lib "kernel32.dll" (ByVal hFile

Remove 'Auto'.
Public Declare Auto Function UnlockFile Lib "kernel32.dll" (ByVal

Remove 'Auto'.
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
hObject As IntPtr) As Boolean

Remove 'Auto'.
 
D

Dragon

GENERIC_READ 1179785

Ah, now I see...
It's

~
Const GENERIC_READ As Integer = &H80000000
~

Roman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top