473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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("kernel32.dll", SetLastError:=True)> Private Shared Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttributes
As IntPtr, ByVal dwCreationDisposition As ECreationDisposition, ByVal
dwFlagsAndAttributes 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("kernel32.dll", SetLastError:=True)> Public Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As IntPtr, ByVal
dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes 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(FileName, 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 6257
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(100) As Byte

Do While fs.Read(arrayOfBytes, 0, arrayOfBytes.Length) > 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("kernel32.dll", SetLastError:=True)> Private Shared Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttributes
As IntPtr, ByVal dwCreationDisposition As ECreationDisposition, ByVal
dwFlagsAndAttributes 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("kernel32.dll", SetLastError:=True)> Public Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As IntPtr, ByVal
dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes 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(FileName, 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(100) As Byte

Do While fs.Read(arrayOfBytes, 0, arrayOfBytes.Length) > 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("kernel32.dll", SetLastError:=True)> Private Shared Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttributes
As IntPtr, ByVal dwCreationDisposition As ECreationDisposition, ByVal
dwFlagsAndAttributes 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("kernel32.dll", SetLastError:=True)> Public Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,
ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As IntPtr, ByVal
dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes 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(FileName, 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 lpSecurityAttributes As IntPtr" does not make sense. As I've
already mentioned, change it to ByVal (or ByRef ... As
SECURITY_ATTRIBUTES if you want them).
2. Check the value of Marshal.GetLastWin32Error() 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*****@hotmail.com> сообщил/сообщила в новостях
следующее: news:1128603805.64e390952c6e40cc742b0ff1f5a1fc78@t eranews...
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("kernel32.dll", SetLastError:=True)> Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As
EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As ECreationDisposition, ByVal
dwFlagsAndAttributes 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("kernel32.dll", SetLastError:=True)> Public Function
CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes 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(FileName, 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*****@newsgroup.nospam> wrote in
news:9C**********************************@microsof t.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:#EfqEvqyFHA.460
@TK2MSFTNGP15.phx.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
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...
3
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...
8
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...
0
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...
5
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...
8
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...
3
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...
2
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.