how to pass a ubyte[] to c interface?

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 12 23:49:09 PDT 2015


On 13/03/2015 7:47 p.m., zhmt wrote:
> On Friday, 13 March 2015 at 06:39:31 UTC, Rikki Cattermole wrote:
>> On 13/03/2015 7:35 p.m., zhmt wrote:
>>> ubyte[] arr ;
>>>
>>> I pass the arr.ptr to c program, it fails silently.
>>>
>>> Is there any way to cast a ubyte[] to a clang pointer?
>>
>> Theoretically this should work.
>>
>> D:
>>
>> void func(ubyte[] value) {
>>     func(value.length, value.ptr);
>> }
>>
>> extern(C) void func(size_t length, ubyte*);
>>
>> C:
>> void func(size_t length, ubyte*) {
>>     // ...
>> }
>
> Thank you for your confirm,I will try again.

Just be careful about GC pointers. They could be free'd and the c 
library thinks it is still live.
Either keep a copy of the pointer alive on the heap somewhere or tell 
the GC to ignore it.


More information about the Digitalmars-d-learn mailing list