This is my next question following writing a byte array from as string using an unknown dll
So I managed to write the byte array, now I want to read it back using the same dll.
I have tried the following:
int BufSize = 60000000; // Size of file I/O buffers.
int BufSizeM1M = BufSize - 1000000; // The max amount of data read in at any one time.
using (WinFileIO WFIO = new WinFileIO())
{
WFIO.OpenForReading(path);
WFIO.ReadBlocks(BufSizeM1M);
WFIO.Close();
WFIO.Dispose();
}
this is the WinFileIO.ReadBlocks function:
public int ReadBlocks(int BytesToRead)
{
// This function reads a total of BytesToRead at a time. There is a limit of 2gb per call.
int BytesReadInBlock = 0, BytesRead = 0, BlockByteSize;
byte* pBuf = (byte*)pBuffer;
// Do until there are no more bytes to read or the buffer is full.
do
{
BlockByteSize = Math.Min(BlockSize, BytesToRead - BytesRead);
if (!ReadFile(pHandle, pBuf, BlockByteSize, &BytesReadInBlock, 0))
{
Win32Exception WE = new Win32Exception();
ApplicationException AE = new ApplicationException("WinFileIO:ReadBytes - Error occurred reading a file. - "
+ WE.Message);
throw AE;
}
if (BytesReadInBlock == 0)
break;
BytesRead += BytesReadInBlock;
pBuf += BytesReadInBlock;
} while (BytesRead < BytesToRead);
return BytesRead;
}
My question is, how would one use the function to read an actual file?
To try and answer your question at a slightly higher level of abstraction, if you are working with an “unknown” .NET assembly, one thing you can try is to decompile the assembly and work out what is going on from the source.
Decompilation is easier than it sounds. Just open the assembly (DLL) using the free tool ILSpy. You can then view the source for the methods you are trying to use in C# (or even CIL).
Looking at the source code will let you see which parts of the .NET base class library are being used, and consult documentation for that, or even post another question here about the source code you do not understand.
Without that source, all we can do is guess at the possible API and workflow your unknown assembly supports.
In spite of my advice above, here is my guess:
BufSizeM1Mbytes from a file without checking it contains that much data.And here are a few other comments on your code:
WFIO.Dispose();, butWFIOis created in a using statement, so that line is unnecessary (this won’t be the error though).WFIO.Close()is redundant when you are also having the object disposed.EDIT:
So it looks like
WinFileIO.ReadBlocksuses the Win32ReadFilefunction to readBytesToReadbytes into a buffer pointed at bypBuffer. I guess you need to find another method onWinFileIOthat gives you access to the buffer. The trick would be to look at which other methods do something withpBuffer. For example, there could be a method that converts it to abyte[]and returns it to you like that, and that might look something like this: