Home All Groups Group Topic Archive Search About

Visual Basic 2005 CreateFile

Author
6 Feb 2006 10:05 AM
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.

Author
6 Feb 2006 11:41 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Fla" <fla_gro***@tiscali.it> schrieb:
> 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)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
6 Feb 2006 12:59 PM
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


Show quoteHide quote
"Fla" <fla_gro***@tiscali.it> wrote in message
news:1139220326.258778.261390@o13g2000cwo.googlegroups.com...
| 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.
|
Author
6 Feb 2006 2:36 PM
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
Author
6 Feb 2006 2:57 PM
Herfried K. Wagner [MVP]
"Fla" <fla_gro***@tiscali.it> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Feb 2006 9:43 AM
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?