从托管(C#)调用不安全的代码.读字节数组

Tom*_*ski 1 .net c# unsafe unmanaged

我有这个方法,我需要在我的应用程序中调用和使用,但我不知道真的知道如何完成它.

这是我需要调用的功能.

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(int Position, int Length, char* Buffer, int* DataRead);
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我有这个功能,我错过了它的实现.

internal static void GetDataToBuffer(int position, int length, out byte[] data, out int dataRead)
    {
        unsafe
        {
             // the code I need
        }
    }
Run Code Online (Sandbox Code Playgroud)

我认为大部分都是非常自我规划的.我需要实现后一个函数,这样我才能将数据读入缓冲区和读取的数据量(实际上应与data.Length相同,但制造商将此作为单独的选项,所以我需要它).有人可以帮忙吗?这够清楚了吗?

谢谢

编辑:这是.h文件中的非托管声明.希望能帮助到你.

 extern NAG_DLL_EXPIMP int DTS_GetDataToBuffer(int Position, 
                               int Length, 
                               unsigned char  *Buffer, 
                               int *DataRead );
Run Code Online (Sandbox Code Playgroud)

编辑#2:位置 - 星形读取数据的位置.长度 - 要读取的数据量(这将是缓冲区大小).DataRead - 读取的实际数据大小.

max*_*max 7

我认为你真的不需要在这里使用不安全的指针.声明函数为

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(
    int     position,
    int     length,
    byte[]  buffer,
    ref int dataRead);
Run Code Online (Sandbox Code Playgroud)

这个函数的合理C#包装器:

internal static byte[] GetDataToBuffer()
{
    // set BufferSize to your most common data length
    const int BufferSize = 1024 * 8;
    // list of data blocks
    var chunks = new List<byte[]>();
    int dataRead = 1;
    int position = 0;
    int totalBytes = 0;
    while(true)
    {
        var chunk = new byte[BufferSize];
        // get new block of data
        DTS_GetDataToBuffer(position, BufferSize, chunk, ref dataRead);
        position += BufferSize;
        if(dataRead != 0)
        {
            totalBytes += dataRead;
            // append data block
            chunks.Add(chunk);
            if(dataRead < BufferSize)
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
    switch(chunks.Count)
    {
        case 0: // no data blocks read - return empty array
            return new byte[0];
        case 1: // single data block
            if(totalBytes < BufferSize)
            {
                // truncate data block to actual data size
                var data = new byte[totalBytes];
                Array.Copy(chunks[0], data, totalBytes);
                return data;
            }
            else // single data block with size of Exactly BufferSize
            {
                return chunks[0];
            }
        default: // multiple data blocks
            {
                // construct new array and copy all data blocks to it
                var data = new byte[totalBytes];
                position = 0;
                for(int i = 0; i < chunks.Count; ++i)
                {
                    // copy data block
                    Array.Copy(chunks[i], 0, data, position, Math.Min(totalBytes, BufferSize));
                    position += BufferSize;
                    // we need to handle last data block correctly,
                    // it might be shorted than BufferSize
                    totalBytes -= BufferSize;
                }
                return data;
            }
    }
}
Run Code Online (Sandbox Code Playgroud)