K
Ketchup
Hello everyone,
I have been stuck with this problem for quite some time now. I am working in VB.NET, using framework 1.0. I have to keep the compatibility down to the original .NET framework as much as possible. I am forced to use API calls for several functions, one of which is ReadFile. The reason is because the framework seems to use ANSI versions of all API functions, limiting the MAX_PATH for filenames to about 260 chars for everything in System.IO. API Wide functions (Unicode) do not have this limitation.
I am having issues implementing the ReadFile function. I seem to only get various exceptions whenever I try to use it. It's declared as follows:
<DllImport("kernel32", EntryPoint:="ReadFile", CharSet:=CharSet.Unicode, SetLastError:=True, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Shared Function ReadFile(ByVal hFile As Integer, ByRef lpBuffer As Object, _
ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
I believe the issue is with the lpBuffer arguement. It's an <out> arguement that basically populates a buffer with the bytes read from the file. In C++ its declared as a long void pointer.
I have tried various ways of using this function, passing various types of variables to it. There is always an exception though. For example, this implementation throws an System.ExecutionEngineException exception.
Dim cBuff(4096) as Char
for i = 0 to cBuff.Length -1
cBuff(i) = " "
Next
iRetVal = ReadFile(hFlHandle, cBuff, 100, iBytesRead, OL)
OL, iBytesRead, and hFlHandle are all declared eariler. The hFlHandle is valid.
When I use a string for the buffer, I get the "System.Runtime.InteropServices.InvalidOleVariantTypeException: Specified OLE variant is invalid" exception.
Dim sBuffer as String = space(100)
iRetVal = ReadFile(hFlHandle, sBufferPtr, sBuffer.Length, iBytesRead, OL)
I am not entirely sure what the problem is. I must be missing something. Has anyone used the ReadFile function (or anything that requires an LPVOID passed to it) in VB.NET? Can anyone shine some light on this?
Thanks,
Dmitry
I have been stuck with this problem for quite some time now. I am working in VB.NET, using framework 1.0. I have to keep the compatibility down to the original .NET framework as much as possible. I am forced to use API calls for several functions, one of which is ReadFile. The reason is because the framework seems to use ANSI versions of all API functions, limiting the MAX_PATH for filenames to about 260 chars for everything in System.IO. API Wide functions (Unicode) do not have this limitation.
I am having issues implementing the ReadFile function. I seem to only get various exceptions whenever I try to use it. It's declared as follows:
<DllImport("kernel32", EntryPoint:="ReadFile", CharSet:=CharSet.Unicode, SetLastError:=True, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Shared Function ReadFile(ByVal hFile As Integer, ByRef lpBuffer As Object, _
ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
I believe the issue is with the lpBuffer arguement. It's an <out> arguement that basically populates a buffer with the bytes read from the file. In C++ its declared as a long void pointer.
I have tried various ways of using this function, passing various types of variables to it. There is always an exception though. For example, this implementation throws an System.ExecutionEngineException exception.
Dim cBuff(4096) as Char
for i = 0 to cBuff.Length -1
cBuff(i) = " "
Next
iRetVal = ReadFile(hFlHandle, cBuff, 100, iBytesRead, OL)
OL, iBytesRead, and hFlHandle are all declared eariler. The hFlHandle is valid.
When I use a string for the buffer, I get the "System.Runtime.InteropServices.InvalidOleVariantTypeException: Specified OLE variant is invalid" exception.
Dim sBuffer as String = space(100)
iRetVal = ReadFile(hFlHandle, sBufferPtr, sBuffer.Length, iBytesRead, OL)
I am not entirely sure what the problem is. I must be missing something. Has anyone used the ReadFile function (or anything that requires an LPVOID passed to it) in VB.NET? Can anyone shine some light on this?
Thanks,
Dmitry