Converting void* to D array

Daniel Kozák via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 14 21:38:15 PDT 2015


On Wed, 15 Apr 2015 04:24:20 +0000
Craig Dillabaugh via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

> Hi.
> I want to call a C library function that returns a data buffer as 
> a void*.  How do I convert the resulting void* into something I 
> can process in D?
> 
> //I have the following function from the GDAL C library.
> extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* 
> );
> 
> 
> So I have (GByte is defined in the GDAL library):
> 
> void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size 
> );
> 
> I fill the buffer (and ignore any errors :o)
> 
> GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer );
> 
> 
> Now, how can I access the data in buffer?

auto buf = (cast(GByte *)buffer)[0 .. xblock * yblock];


More information about the Digitalmars-d-learn mailing list