0

I use VB .Net to call the Kernel32.dll WriteFile API:

Public Declare Function WriteFile Lib "kernel32" _ ( _ ByVal hFile As IntPtr, _ ByVal lpBuffer As Byte(), _ ByVal nNumberOfBytesToWrite As Int32, _ ByRef lpNumberOfBytesWritten As Int32, _ ByVal lpOverlapped As IntPtr _ ) _ As Boolean

Can anybody tell me how to create an Overlapped structure (lpOverlapped) for this function, and how to correctly pass it (the API expects a pointer?) Please show a working code snippet, if possible...

All info I found either didn't show usable examples or were too complicated to understand for me, or just weren't for VB .Net ...

N00b
  • 29
  • 5

2 Answers2

1

Probably resolved... I guess it's done like this:

Dim structPtr As IntPtr
'Create an empty pointer
structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(my_struct))
'Copy the structure and data to the pointer in memory
Marshal.StructureToPtr(my_struct, structPtr, True)
Nikita
  • 880
  • 1
  • 16
  • 23
N00b
  • 29
  • 5
0

You don't need to pass a pointer to an overlapped structure and if you are finding the examples too complicated don't use it. WinAPI can be complicated in general and it is often easier to accomplish what you need directly in .Net and avoid WinAPI altogehter.

To answer your question the structure would be defined as:

Public Structure OVERLAPPED
    Public Internal As Long
    Public InternalHigh As Long
    Public offset As Long
    Public OffsetHigh As Long
    Public hEvent As Long
End Structure

However, System.Threading has two classes Overlapped and NativeOverlapped meant to make life easier. Overlapped is a .Net class which you can pack into a NativeOverlapped. The structure allows you to set a call back to be fired:

In C# you would define it like this:

Overlapped overlapped = new Overlapped();
NativeOverlapped* nativeOverlapped = overlapped.Pack(
DeviceWriteControlIOCompletionCallback,
null);    

Note I said in C#, that's because Net. doesn't support the return type in VB because it is unsafe code:

Visual Basic does not support APIs that consume or return unsafe types.

So if you are really looking to do Overlapped IO, it might be a lot easier for you code your overlapped methods in a C# class library, which you can then reference in and call in your VB application.

From All API:

· lpOverlapped Points to an OVERLAPPED structure. This structure is required if hFile was opened with FILE_FLAG_OVERLAPPED. If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the write operation is complete. If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the write operation starts at the current file position and WriteFile does not return until the operation has been completed. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile does not return until the write operation has been completed.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • Thanks for your detailled response, *BUT* the question was how to create an overlapped structure *that can be handed to the WriteFile API*, and *how to do that in VB .Net* ... I *need* to do this in VB .Net because the whole program is written in that language already, and I can't do it in any other way because I want to explicitely test something with WriteFile and Overlapped (namely if write succeeds if file is opened without FILE_FLAG_OVERLAPPED and offset is given in the overlapped structure). Might solve my problem stated here: https://stackoverflow.com/questions/45116162/ – N00b Jul 15 '17 at 20:35