vb.net's CreateFile

S

Stephen Remde

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, _
ByVal hTemplateFile As Long) As Long

Private Declare Function closeHandle_ _
Lib "kernel32" _
Alias "CloseHandle" (ByVal hObject As Long) As Long

How to convert these to vb.net - or is there some other way of obtaining a
file handle. Well actually its "\\.\C:" i want to get to pass to another api
function

Stephen
 
H

Herfried K. Wagner [MVP]

* "Stephen Remde said:
Private Declare Function CreateFile _
Lib "kernel32" Alias "CreateFileA" _

Declare the function as 'Auto' ('Declare Auto Function...'), then remove
the alias.
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long

Replace all 'As Long' with 'As Int32', 'lpSecurityAttributes' can be
declared as 'Int32' (...), 'hTemplateFile' as 'IntPtr'. The return
value is an 'IntPtr'.
Private Declare Function closeHandle_ _
Lib "kernel32" _
Alias "CloseHandle" (ByVal hObject As Long) As Long

Remove the alias, declare its parameter as 'IntPtr' and its return value
as 'Boolean'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
S

Stephen Remde

thanks, that worked...

now heres a tough one:

Private Declare Function NtFsControlFile _
Lib "ntdll.dll" _
(ByVal hFileHandle As Long, _
ByVal hEvent As Long, _
ByRef ApcRoutine As Any, _
ByRef ApcContext As Long, _
ByRef IoStatusBlock As IO_STATUS_BLOCK, _
ByVal FsControlCode As Long, _
ByRef InputBuffer As Any, _
ByVal InputBufferLength As Long, _
ByRef OutputBuffer As Any, _
ByVal OutputBufferLength As Long) As Long

i've tried

Public Structure IO_STATUS_BLOCK
Public Status As Integer ' (were long's in vb6)
Public Information As Integer
End Structure

Declare Auto Function NtFsControlFile Lib "ntdll.dll" _
(ByVal hFileHandle As Integer, _
ByVal hEvent As Integer, _
ByRef ApcRoutine As Integer, _
ByVal ApcContext As Integer, _
ByRef IoStatusBlock As IO_STATUS_BLOCK, _
ByVal FsControlCode As Integer, _
ByRef InputBuffer As Object, _
ByVal InputBufferLength As Integer, _
ByVal OutputBuffer As Object, _
ByRef OutputBufferLength As Integer) As Integer

but i get return values of &hC0000023 {Buffer Too Small} The buffer is too
small to contain the entry. No information has been written to the buffer.

so im thinking that theres something wrong ith these lines

ByRef InputBuffer As Object, _
ByVal InputBufferLength As Integer, _
ByVal OutputBuffer As Object, _
ByRef OutputBufferLength As Integer

the call i am making is

Dim a as Long
Dim b(bSize) as Byte

NtFsControlFile( ... , ... , ... , ... , ... , ... , a , 8 , b , bSize)

Thanks in advance,
Stephen
 
B

Bob

For starters you should be converting VB6 longs to VB.Net Int32... not
integer... you have declared a variable that is too small to take the long
datatype value being returned...

Herfield stated this in his reply to your first post...

Cheers
 
S

Stephen Remde

Bob,

VB.NETs Integer's are 32bit, the same as Int32... (se below) exchanging
the Integer types for Int32s make no difference.

i think the problem is with

OutputBuffer - needs to pass a memory pointer to the actual data of the
array. this is where the api call will write its response.

Thanks,

quoted from the msdn
(ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vadatInteger.htm):

Integer Data Type

Integer variables are stored as signed 32-bit (4-byte) integers ranging in
value from -2,147,483,648 through 2,147,483,647.

The Integer data type provides optimal performance on a 32-bit processor, as
the smaller integral types are slower to load and store from and to memory.

-snip-

*** The equivalent .NET data type is System.Int32. ***


Stephen
 
H

Herfried K. Wagner [MVP]

* "Bob said:
For starters you should be converting VB6 longs to VB.Net Int32... not
integer... you have declared a variable that is too small to take the long
datatype value being returned...

Herfield stated this in his reply to your first post...

?!?

In VB.NET 'Integer' = 'System.Int32' (= VB6's 'Long' datatype).

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
S

Stephen Remde

maybe i converted it to vb6 wrong in the first place. here is the c version:

typedef UINT NTSTATUS;

//
// Io Status block
//
typedef struct _IO_STATUS_BLOCK {
NTSTATUS Status;
ULONG Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;


//
// Apc Routine
//
typedef VOID (*PIO_APC_ROUTINE) (
PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG Reserved
);


//
// The undocumented NtFsControlFile
//

NTSTATUS (__stdcall *NtFsControlFile)(
HANDLE FileHandle,
HANDLE Event, // optional
PIO_APC_ROUTINE ApcRoutine, // optional
PVOID ApcContext, // optional
PIO_STATUS_BLOCK IoStatusBlock,
ULONG FsControlCode,
PVOID InputBuffer, // optional
ULONG InputBufferLength,
PVOID OutputBuffer, // optional
ULONG OutputBufferLength
);
 
T

Tom Shelton

Stephen,

Here is my rough, first pass, and untested version :) This is probably
where I would start. If it doesn't work, let me know... And maybe a
little more information about what this function is supposed to do (even
a little project that uses it would be nice) - so that I can refine.
maybe i converted it to vb6 wrong in the first place. here is the c version:

typedef UINT NTSTATUS;

//
// Io Status block
//
typedef struct _IO_STATUS_BLOCK {
NTSTATUS Status;
ULONG Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;

<StructLayout(LayoutKind.Sequential)> _
Structure IO_STATUS_BLOCK
Public Status As Integer
Public Information As Integer
End Structure
//
// Apc Routine
//
typedef VOID (*PIO_APC_ROUTINE) (
PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG Reserved
);

' if you need the call back, it should look something like this
' since the above is defining a pointer to a function that returns
' void and takes 3 parameters...
Delegate Sub PIO_APC_ROUTINE _
(ByVal AbcContext As IntPtr, _
ByRef IoStatusBlock As IO_STATUS_BLOCK, _
ByVal Reserved As Integer)
//
// The undocumented NtFsControlFile
//

NTSTATUS (__stdcall *NtFsControlFile)(
HANDLE FileHandle,
HANDLE Event, // optional
PIO_APC_ROUTINE ApcRoutine, // optional
PVOID ApcContext, // optional
PIO_STATUS_BLOCK IoStatusBlock,
ULONG FsControlCode,
PVOID InputBuffer, // optional
ULONG InputBufferLength,
PVOID OutputBuffer, // optional
ULONG OutputBufferLength
);

Declare Function NtFsControlFile Lib "ntdll" _
(ByVal FileHandle As IntPtr, _
ByVal Event As IntPtr, _
ByVal ApcRoutine As PIO_APC_ROUTINE, _
ByVal ApcContext As IntPtr, _
ByRef IoStatusBlock As IO_STATUS_BLOCK, _
ByVal FsControlCode As Integer, _
ByVal InputBuffer() As Byte, _
ByVal InputBufferLength As Integer, _
ByVal OutputBuffer() As Byte, _
ByVal OutputBufferLength As Integer) As IntPtr



Dim controlCode As Integer
Dim inputBuffer(bSize) As Byte
Dim outputBuffer(bSize) As Byte
Dim controlBlock As IO_STATUS_BLOCK

NtFsControlFile( _
FileHandle, _
Event, _
Nothing, _
IntPtr.Zero, _
controlBlock, _
controlCode, _
inputBuffer, _
inputBuffer.Length, _
outputBuffer, _
outputBuffer.Length)
 
D

dabuskol

You want to create a new file. Try this one in .NET

Imports System.IO; //NAMESPACE

public function SaveFile(string strfile)
dim myStreamWriter as StreamWriter myStreamWriter
myStreamWriter = File.AppendText("c:/oss_listen.log")
myStreamWriter.WriteLine(strfile)
myStreamWriter.Flush()
myStreamWriter.Close()
end function

}
 

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