Visual Basic 2005 CreateFile

F

Fla

Hy!
I'm a newbie to VB 2005 and I have to connect my program to a driver
previously developed for a custom ISA card.
With my old VB 6 code I used the routine CreateFileA exported from
kernel32 with this defination:

Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
As Long, ByVal NULLSecurityAttributes As Long, ByVal
dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long,
ByVal NULLTemplateFile As Long) As Long

The routine was called with

Public hCVxD As Long
hCVxD = CreateFile("\\.\Driver", GENERIC_READ Or GENERIC_WRITE, 0,
sNULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, sNULL)

.... and this code works properly.

I parsed this code with the same args in VB 2005, without getting any
error during build and run-time but hCVxD goes in overflow.
What I'm doing wrong? Does anybody know a solution? Where can I find
any suggestion?

Thanks for your replies.
 
H

Herfried K. Wagner [MVP]

Fla said:
I'm a newbie to VB 2005 and I have to connect my program to a driver
previously developed for a custom ISA card.
With my old VB 6 code I used the routine CreateFileA exported from
kernel32 with this defination:

Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
As Long, ByVal NULLSecurityAttributes As Long, ByVal
dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long,
ByVal NULLTemplateFile As Long) As Long

The routine was called with

Public hCVxD As Long
hCVxD = CreateFile("\\.\Driver", GENERIC_READ Or GENERIC_WRITE, 0,
sNULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, sNULL)

The declaration is wrong for VB.NET:

\\\
Private Declare Auto Function CreateFile Lib "kernel32.dll" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr _
) As IntPtr
....
Dim hDVxD As IntPtr = CreateFile(..., IntPtr.Zero)
///
 
J

Jay B. Harlow [MVP - Outlook]

Fla,
In addition to Herfried's comments.

http://www.pinvoke.net/ is an excellent resource for finding the .NET
definitions of Win32 APIs.

For example:

http://www.pinvoke.net/default.aspx/kernel32.CreateFile

Is the page what shows CreateFile.

The only "caveat" about pinvoke.net is sometimes it uses the P/Invoke
(DllImport) syntax instead of the Declare syntax, I generally prefer the
Declare syntax. However its relatively easy to convert between the two...


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hy!
| I'm a newbie to VB 2005 and I have to connect my program to a driver
| previously developed for a custom ISA card.
| With my old VB 6 code I used the routine CreateFileA exported from
| kernel32 with this defination:
|
| Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal
| lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
| As Long, ByVal NULLSecurityAttributes As Long, ByVal
| dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long,
| ByVal NULLTemplateFile As Long) As Long
|
| The routine was called with
|
| Public hCVxD As Long
| hCVxD = CreateFile("\\.\Driver", GENERIC_READ Or GENERIC_WRITE, 0,
| sNULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, sNULL)
|
| ... and this code works properly.
|
| I parsed this code with the same args in VB 2005, without getting any
| error during build and run-time but hCVxD goes in overflow.
| What I'm doing wrong? Does anybody know a solution? Where can I find
| any suggestion?
|
| Thanks for your replies.
|
 
F

Fla

Thanks you both for the replies.
I tried with your suggestions but I get always the same value for hCVxD
( 312) while, with VB 6 equivalent code I get different values for
hCVxD each time I run my old program. Is it due to .NET framework or am
I doing mistakes?

Thanks
 
H

Herfried K. Wagner [MVP]

Fla said:
I tried with your suggestions but I get always the same value for hCVxD
( 312) while, with VB 6 equivalent code I get different values for
hCVxD each time I run my old program. Is it due to .NET framework or am
I doing mistakes?

I suggest to post the code you are using.
 
F

Fla

Ok, thanks for your attention; I checked and fixed the CreateFile code
with an exception. Now I can connect via handle to the the driver, but
I can't read datas from it despite I'm using ReadFile as reported in
the folllowing URL
http://support.microsoft.com/default.aspx?scid=kb;en-us;823179&Product=vb6
The code I'm using, is the following:

Try
Success = ReadFile(hSerialPort, Buffer, BytesWritten,
BytesRead, IntPtr.Zero)
If Success = False Then
Throw New CommException("Unable to read from driver")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try

but I can't get datas from driver cause I get an exception ("Unable to
read from driver").

What am I doing wrong?
 

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