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

Convert to VB.NET

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Andrew Clark" <la*****@hotmail.com> wrote in message
news:1128102107.693793a8f2ca80434a2a9538de42f1ce@t eranews...
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
Nov 21 '05 #2

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

"Andrew Clark" <la*****@hotmail.com> сообщил/сообщила в новостях
следующее: news:1128102107.693793a8f2ca80434a2a9538de42f1ce@t eranews...
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

Nov 21 '05 #3

P: n/a
"Dragon" <no@spam.please> wrote in news:Oa2kj9exFHA.3256
@TK2MSFTNGP09.phx.gbl:
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
Nov 21 '05 #4

P: n/a
"Andrew Clark" <la*****@hotmail.com> schrieb:

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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5

P: n/a
> GENERIC_READ 1179785

Ah, now I see...
It's

~
Const GENERIC_READ As Integer = &H80000000
~

Roman
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.