473,782 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CreateFile (was: Convert to VB.NET)

Hello,

Thanks for all replies on this subject. I still cannot get CreateFile to
retun a good value though. I went to PInvoke.net and saw the VB.NET
declaration of this function:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Private Shared Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttri butes
As IntPtr, ByVal dwCreationDispo sition As ECreationDispos ition, ByVal
dwFlagsAndAttri butes As EFileAttributes , ByVal hTemplateFile As IntPtr)
As IntPtr
End Function

VB.NET did not like the 'Private Shared' part, so I changed that to
'Public', and all the EFile* types I changed to Integer, so this is what
I end up with:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Public Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttri butes As IntPtr, ByVal
dwCreationDispo sition As Integer, ByVal dwFlagsAndAttri butes As Integer,
ByVal hTemplateFile As IntPtr) As IntPtr
End Function

Now VB.NET does not complain at least. I put a break on this statement:

hFile = CreateFile(File Name, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING,
FILE_ATTRIBUTE_ NORMAL, IntPtr.Zero)

and the values are as following:

GENERIC_READ -2147483648
IntPtr.Zero 0
OPEN_EXISTING 3
FILE_ATTRIBUTE_ NORMAL 128

but still I get -1 returned! Thoughts?

Thanks,
Andrew
Nov 21 '05 #1
5 6328
Why not use the .NET Framework instead of kernel32?

You can do something like this to open up a file:

Dim fs As FileStream = New FileStream("C:\ filename.txt", FileMode.Open)
Dim arrayOfBytes(10 0) As Byte

Do While fs.Read(arrayOf Bytes, 0, arrayOfBytes.Le ngth) > 0
'Process your file
Loop

Or something like this:
Dim sw As StreamReader = File.OpenText(" C:\filename.txt ")
Dim fileContents As String = sw.ReadToEnd()

Take a look at the System.IO namespace for different classes for File IO.

"Andrew Clark" wrote:
Hello,

Thanks for all replies on this subject. I still cannot get CreateFile to
retun a good value though. I went to PInvoke.net and saw the VB.NET
declaration of this function:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Private Shared Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttri butes
As IntPtr, ByVal dwCreationDispo sition As ECreationDispos ition, ByVal
dwFlagsAndAttri butes As EFileAttributes , ByVal hTemplateFile As IntPtr)
As IntPtr
End Function

VB.NET did not like the 'Private Shared' part, so I changed that to
'Public', and all the EFile* types I changed to Integer, so this is what
I end up with:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Public Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttri butes As IntPtr, ByVal
dwCreationDispo sition As Integer, ByVal dwFlagsAndAttri butes As Integer,
ByVal hTemplateFile As IntPtr) As IntPtr
End Function

Now VB.NET does not complain at least. I put a break on this statement:

hFile = CreateFile(File Name, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING,
FILE_ATTRIBUTE_ NORMAL, IntPtr.Zero)

and the values are as following:

GENERIC_READ -2147483648
IntPtr.Zero 0
OPEN_EXISTING 3
FILE_ATTRIBUTE_ NORMAL 128

but still I get -1 returned! Thoughts?

Thanks,
Andrew

Nov 21 '05 #2
In addition, if it's a file handle that you seek, you can also do something
like this:

Dim fs As FileStream = New FileStream("C:\ filename.txt", FileMode.Open)
Dim filePtr As IntPtr = fs.Handle
hFile = filePtr.ToInt32 ()

Hope this helps. I haven't tested this, but it's off the top of my head.
Let me know if it works for you.

"rmacias" wrote:
Why not use the .NET Framework instead of kernel32?

You can do something like this to open up a file:

Dim fs As FileStream = New FileStream("C:\ filename.txt", FileMode.Open)
Dim arrayOfBytes(10 0) As Byte

Do While fs.Read(arrayOf Bytes, 0, arrayOfBytes.Le ngth) > 0
'Process your file
Loop

Or something like this:
Dim sw As StreamReader = File.OpenText(" C:\filename.txt ")
Dim fileContents As String = sw.ReadToEnd()

Take a look at the System.IO namespace for different classes for File IO.

"Andrew Clark" wrote:
Hello,

Thanks for all replies on this subject. I still cannot get CreateFile to
retun a good value though. I went to PInvoke.net and saw the VB.NET
declaration of this function:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Private Shared Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttri butes
As IntPtr, ByVal dwCreationDispo sition As ECreationDispos ition, ByVal
dwFlagsAndAttri butes As EFileAttributes , ByVal hTemplateFile As IntPtr)
As IntPtr
End Function

VB.NET did not like the 'Private Shared' part, so I changed that to
'Public', and all the EFile* types I changed to Integer, so this is what
I end up with:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Public Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttri butes As IntPtr, ByVal
dwCreationDispo sition As Integer, ByVal dwFlagsAndAttri butes As Integer,
ByVal hTemplateFile As IntPtr) As IntPtr
End Function

Now VB.NET does not complain at least. I put a break on this statement:

hFile = CreateFile(File Name, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING,
FILE_ATTRIBUTE_ NORMAL, IntPtr.Zero)

and the values are as following:

GENERIC_READ -2147483648
IntPtr.Zero 0
OPEN_EXISTING 3
FILE_ATTRIBUTE_ NORMAL 128

but still I get -1 returned! Thoughts?

Thanks,
Andrew

Nov 21 '05 #3
Hello,

1. "ByRef lpSecurityAttri butes As IntPtr" does not make sense. As I've
already mentioned, change it to ByVal (or ByRef ... As
SECURITY_ATTRIB UTES if you want them).
2. Check the value of Marshal.GetLast Win32Error() after the unsuccessful
call to CreateFile and look it up in
http://msdn.microsoft.com/library/en...error_codes.as
p

HTH,
Roman

"Andrew Clark" <la*****@hotmai l.com> ñîîáùèë/ñîîáùèëà â íîâîñòÿõ
ñëåäóþùåå: news:1128603805 .64e390952c6e40 cc742b0ff1f5a1f c78@teranews...
Hello,

Thanks for all replies on this subject. I still cannot get CreateFile to retun a good value though. I went to PInvoke.net and saw the VB.NET
declaration of this function:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Private Shared Function CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttri butes As IntPtr, ByVal dwCreationDispo sition As ECreationDispos ition, ByVal
dwFlagsAndAttri butes As EFileAttributes , ByVal hTemplateFile As IntPtr) As IntPtr
End Function

VB.NET did not like the 'Private Shared' part, so I changed that to
'Public', and all the EFile* types I changed to Integer, so this is what I end up with:

<DllImport("ker nel32.dll", SetLastError:=T rue)> Public Function
CreateFile(ByVa l lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttri butes As IntPtr, ByVal dwCreationDispo sition As Integer, ByVal dwFlagsAndAttri butes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
End Function

Now VB.NET does not complain at least. I put a break on this statement:
hFile = CreateFile(File Name, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL, IntPtr.Zero)

and the values are as following:

GENERIC_READ -2147483648
IntPtr.Zero 0
OPEN_EXISTING 3
FILE_ATTRIBUTE_ NORMAL 128

but still I get -1 returned! Thoughts?

Thanks,
Andrew

Nov 21 '05 #4
"=?Utf-8?B?cm1hY2lhcw= =?=" <rm*****@newsgr oup.nospam> wrote in
news:9C******** *************** ***********@mic rosoft.com:
In addition, if it's a file handle that you seek, you can also do
something like this:

Dim fs As FileStream = New FileStream("C:\ filename.txt",
FileMode.Open) Dim filePtr As IntPtr = fs.Handle
hFile = filePtr.ToInt32 ()

Hope this helps. I haven't tested this, but it's off the top of my
head. Let me know if it works for you.


Yes, I use CreateFile because I need a file handle. I will try this,
thanks!

Andrew
Nov 21 '05 #5
"Dragon" <no@spam.please > wrote in news:#EfqEvqyFH A.460
@TK2MSFTNGP15.p hx.gbl:
Hello,


Thanks, but I've decided to scrap the Win32 API. Now I have another issue
and I think I will start another thread.

Thanks to all!
Andrew
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1696
by: Nitin Narang | last post by:
I have a piece of code which is moved to .NET VC7 recently from VC6 and there has been an apparent change in behavior using CreateFile/Writefile functionality. I am creating a file on a shared drive using the win32 Api and using the handle returned, i call writefile giving number of bytes to write as 1MB. Shared directory on the machine maps to F: drive ( "shared" is the directory name maps to " F:") In VC6 on executing...
3
7474
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess,
8
7178
by: Stephen Remde | last post by:
Hi, Private Declare Function CreateFile _ Lib "kernel32" Alias "CreateFileA" _ (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Any, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _
0
2544
by: JohnnyBoy | last post by:
I am playing with a Microsoft code example that uses the creatFile call to open a stream to a serial COM port. When I execute the example, the createFile call fails with an error code 5 (ERROR_ACCESS_DENIED) as if the COM port is inaccessible from the program. I am including a code snippit with the createFile call in hopes that someone out there has seen this before and can help. abytes is a string "COM1", and imode is set to the overlapped...
5
3082
by: Fla | last post by:
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,...
8
2114
by: news.microsoft.com | last post by:
Hello, i'm having problems to call a api dll in vb dot net. I absolutely want to use this api call and none of the frame calls. This is my declaration: ***************** Public Structure SECURITY_ATTRIBUTES Public nLength As UInt32 Public lpSecurityDescriptor As IntPtr
3
2871
by: Scott Bell | last post by:
Hello all, What is the equivalent in the .NET Framework for accessing a device at the block/sector level such as one would when using CreateFile on a device like "\\.\D:"? Attempting to create a BinaryReader on a FileStream opened (read-only) on "D:" yields an Access Denied exception. Thanks,
2
3139
by: Jim Flanagan | last post by:
Hi... I am using VB.net Express to experiment with the Win32 API functions that are available. The current project is an application that will read the raw sectors of a logical drive so that a CRC-32 calculation can be performed on all of the bytes of the logical drive. This is my first experience with the CreateFile function. Here is the problem(s) that I am having.
2
1264
by: Kerem Gümrükcü | last post by:
Hi all, sorry for Crossposting, but i dont know whether someone can help me here. If i remember right, there was a Class or a Member function in the .NET Framework that could do mostly the same as CreateFile can do for me, but i cant remember its Name. By saying the "same" i mean i was able to use it to open Handles to Devices and anything that could be opened with CreateFile and had a Symbolic Name that has been exported from Kernel...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.