问题描述:

i'm struggling in my head for what's the best way of copying large data that comes from DLL as ref byte to a file.

since it's a video stream (or hundreds) then i don't feel so comfortable using Marhal.Copy/ WriteByte in a loop, and etc.. but something more direct.

i'm pretty stucked with that ref byte and unfortunately don't really have so much to do about it.

anyways, i was thinking about P/Invoke WriteFile but it gets IntPtr where the closest i could get from ref byte is of course byte* (using fixed).

now, i'm going to have few hundreds of this kind of stream, and each one can get to ~2Mbps.

so at the bottom line - i'm searching for an efficient way of copying ref byte chunks to a file (with a given array size of course).

网友答案:

well in case someone would be interested, and thanks to Mr. Smith, i first changed the given DllImport from ref byte declarations to IntPtr.
then i imported WriteFile:

[DllImport("kernel32.dll")]
    private static extern bool WriteFile(IntPtr hFile, IntPtr lpBuffer, int NumberOfBytesToWrite, out int lpNumberOfBytesWritten, IntPtr lpOverlapped);

and used it as follows:

//somewhere in class:
tmpFileStream = new FileStream("tmp.bin", FileMode.Create, FileAccess.Write);

public void AddBuffer(IntPtr buffer, int size)
{
    int written;
    WriteFile(tmpFileStream.SafeFileHandle.DangerousGetHandle(), buffer, size, out written, IntPtr.Zero);
}
相关阅读:
Top