1

我有这个方法需要在我的应用程序中调用和使用,但我真的不知道该怎么做。

这是我需要调用的函数。

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(int Position, int Length, char* Buffer, int* DataRead);

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

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

我认为大部分内容都是不言自明的。我需要实现后一个功能,以便能够将数据读入缓冲区以及读取的数据量(实际上应该与 data.Length 相同,但制造商将此作为单独的选项,所以我需要它)。任何人都可以帮忙吗?这足够清楚吗?

谢谢

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

 extern NAG_DLL_EXPIMP int DTS_GetDataToBuffer(int Position, 
                               int Length, 
                               unsigned char  *Buffer, 
                               int *DataRead );

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

4

3 回答 3

8

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

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(
    int     position,
    int     length,
    byte[]  buffer,
    ref int dataRead);

此函数的合理 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;
            }
    }
}
于 2010-10-15T11:28:03.030 回答
2

我无法对此进行测试,但我认为您应该让 Marshaler 进行转换:

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(out byte[] data, out int dataRead);
于 2010-10-15T11:26:35.103 回答
1

我同意你不需要使用 unsafe 块。您正在使用 pinvoke,我希望以下链接可能有用:http: //msdn.microsoft.com/en-us/magazine/cc164123.aspx http://www.pinvoke.net/

在stackoverflow上也有帖子

于 2010-10-15T11:58:58.820 回答