Click here to Skip to main content
15,915,600 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Seems like simple step, but can't my head around it.
In my C# app, I have a third party API function returning buffer as byte[]. I have to pass this buffer to C++ DLL (which I interop using DLLImport) function which takes pointer to this buffer as unsigned short*.
How do I marshal byte[] to unsigned short* in C#, so that the C++ function can process the contents of this buffer?

Thanks much in advance.
Posted

1 solution

The problem with this example is that the size of the array on unmanaged side (C++) is unknown. I would say, your Question does not fully define your problem. You need this information. It this is passed to the C++ function (so it could work with any length) it is not a problem.

This code sample will give you an idea:
http://stackoverflow.com/questions/289076/how-can-i-pass-a-pointer-to-an-array-using-p-invoke-in-c[^]. In this sample the assumed size of the array is some immediate constant. If this value is known before the call to the unmanaged function, it can be variable. It this is not the case, please explain how this size is defined and passed.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-11 2:39am    
Thank you for accepting this Answer.
Did you get the result already?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900