Hello all. I am new to D and loving the experience so far.

I am trying to make use of a C library from Canon that provides a header and a pre-compiled binary containing implementations of declarations found in the header. I used the excellent guide at https://www.gamedev.net/articles/programming/general-and-gameplay-programming/binding-d-to-c-r3122/ to get started and so far I can initialize the library and embed it within my D application. So far so great!

Where I am running into problems is creating D bindings for this function declaration:

// EDSDK.h
EdsError EDSAPI EdsGetCameraList(EdsCameraListRef* outCameraListRef);
//

If accepts a pointer to EdsCameraListRef that is declared as:

// EDSDKTypes.h
typedef struct __EdsObject* EdsBaseRef;
typedef EdsBaseRef EdsCameraListRef;
//

From my reading I learned that EdsCameraListRef is an alias of an alias for an opaque pointer. It allowed the library writers to declare an object for use that the end-user would not have access to the implementation of. These are the D bindings I created:

// edsdk.d
struct EdsBaseRef;
alias EdsBaseRef EdsCameraListRef;
alias uint EdsUInt32;

extern (System):
EdsError EdsGetCameraList(EdsCameraListRef*);
EdsError EdsGetChildCount(EdsBaseRef*, EdsUInt32*);
EdsUInt32 EdsRelease(EdsBaseRef*);
//

Calling the functions using the bindings and the following code results in invalid pointer errors however (reported from the library):

// camera_list.d
import edsdk;

class CameraList
{
    private EdsCameraListRef* list;
    private EdsUInt32 _count = 0;
    this()
    {
        // Returns EDS_ERR_INVALID_POINTER
        EdsGetCameraList(list);
        // Returns EDS_ERR_INVALID_HANDLE
        EdsGetChildCount(list, &_count);
    }

    ~this()
    {
        EdsRelease(list);
    }

    final uint count()
    {
        return cast(uint) _count;
    }
}
//

What did I do wrong in constructing the bindings? If it helps the library provides a function called EdsRelease for cleaning-up allocated objects. Is the management of pointers between D and C the issue? Please forgive me if I've mangled that concept.

Reply via email to