Declaring a D pointer to a C function
Andrej Mitrovic
andrej.mitrovich at gmail.com
Tue Jul 12 14:33:49 PDT 2011
I just had a bug where a D function is implicitly converted to an
extern(C) function pointer.
I've had this definition:
alias extern (C) void function(void* userData) PaStreamFinishedCallback;
And this extern(C) function:
PaError Pa_SetStreamFinishedCallback(PaStream* stream,
PaStreamFinishedCallback streamFinishedCallback);
And I used this callback:
void StreamFinished(void* userData)
{
auto localData = cast(TestData*)userData;
writefln("Stream Completed: %s", localData.message);
}
// this is where the binding takes place
void main() { ...; SetStreamFinishedCallback(..., &StreamFinished); }
That code is invalid. The fix is to add extern(C) to my callback:
extern(C) void StreamFinished(void* userData)
Is this going to be fixed any time soon? Allowing callbacks with D
calling convention where a C callback is expected should be an error,
and this is like the 10th time I've ran into this bug.
More information about the Digitalmars-d-learn
mailing list