开发者

Calling unsafe code from managed (C#). Reading byte array

开发者 https://www.devze.com 2023-01-19 22:59 出处:网络
I have this method that I need to call and use in my application, but I don\'t know really know how to do it exactly.

I have this method that I need to call and use in my application, but I don't know really know how to do it exactly.

This is the function that I need to call.

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

In my code, I have this function and I'm missing its implementation.

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

I think most of this is very selfexplanatory. I need to implement the latter function so I can be able to read the data into the buffer and the amount of data read (which should actually be the same as data.Length, but the manufacturer has this as separate option, so I need it). Can anyone help? Is this clear enough?

Thank you

Edit: Here is the unmanaged declaration from the .h file. Hope it helps.

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

Edit #2: Positon - the position from which to star reading the data. Length - The amount of data to read (this would be the buffer siz开发者_如何学编程e). DataRead - the actual data size that was read.


I don't think you really need to use unsafe pointers here. Declare function as

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

Reasonable C# wrapper for this function:

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;
            }
    }
}


I can't test this but I think you should let the Marshaler do you conversion(s):

[DllImport(dll_Path)]
public static extern int DTS_GetDataToBuffer(out byte[] data, out int dataRead);


i agree you don't need to use unsafe block. you are using pinvoke, i hope below links might be useful : http://msdn.microsoft.com/en-us/magazine/cc164123.aspx http://www.pinvoke.net/

and there are post on stackoverflow too

0

精彩评论

暂无评论...
验证码 换一张
取 消