0

I am having a unmanaged library . I want to create a C# application using the unmanaged library. I am using 'DllImport' to import the functions from the unmanaged library.

In C++ the structure and the function calling the structure looks as shown below

typedef struct _DATA_BUFFER {

UCHAR *buffer;                // variable length array      
UINT32 length;                 // lenght of the array     
UINT32 transferCount;               

} DATA_BUFFER,*P_DATA_BUFFER;

byte  Write (HANDLE handle, DATA_CONFIG *dataConfig, DATA_BUFFER *writeBuffer, UINT32 Timeout);        

In C# I defined the structure and function as shown below

[StructLayout(LayoutKind.Sequential, Pack = 1)] 

public unsafe struct DATA_BUFFER
{
public byte* buffer;
public UInt32 length;
public UInt32 transfercount;             
};

[DllImport("Library.dll")]

public unsafe static extern byte Write([In] IntPtr hHandle, DATA_CONFIG* dataConfig,   DATA_BUFFER* WriteBuffer, UInt32 timeout);


for (byte i = 0; i < length; i++)
transfer[i] = i;

DATA_BUFFER buffer_user = new DATA_BUFFER();
buffer_user.length = length;
buffer_user.buffer = transfer;

return_status = Write(handle , &dataconfig , &buffer_user , 1000);

I am getting error 'Cannot implicity convert type byte[] to byte .

I tried using fixed and other . Nothing is working .

How do I assign an array (In this case Tranfer[]) to the pointer (public byte* buffer ) in the structure . I need to pass it to above mentioned function?

3
  • Where does that error even occur? I don't see a single byte[] in all that code. Commented Jul 23, 2014 at 18:22
  • I assume its not the complete code and transfer is byte[] Commented Jul 23, 2014 at 18:35
  • did my answer work for you? would be nice if you could markt it as accepted if it did ;) Commented Jul 24, 2014 at 16:19

1 Answer 1

1

Your struct DATA_BUFFER uses a byte* buffer. But I guess you understand the error.

Try

fixed (byte* b = transfer) buffer_user.buffer = b;

inside an unsafe context of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.