|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Visual Basic 2005 CreateFileI'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.
Show quote
Hide quote
"Fla" <fla_gro***@tiscali.it> schrieb: The declaration is wrong for VB.NET:> 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) \\\ 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/> 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... -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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. | 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 "Fla" <fla_gro***@tiscali.it> schrieb: I suggest to post the code you are using.> 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? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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?
Automatic type casting?
Reference To Controls On A Form Crash in VB.NET 2005 DateDiff(DateInterval.DayOfYear Read mail from exchange 2003 Problems with FileAccess and PcitoreBox Insert related tables into database. Automated Refactoring w/ CodeDom and VBCodeProvider... Listbox DataSource Design binding problem TreeNode.Remove(Boolean notify) exception |
|||||||||||||||||||||||