5

I've been reading the D Cookbook and near the beginning there's the following sentence:

D is binary compatible with C, but not source compatible.

SAS allows users to define and call C functions from within SAS. But I'm wondering, would it'd also be possible to do this from D?

I found Adam Ruppe's answer to create a DLL here, and I tried using that to create the DLL example from the SAS documentation; however, whenever I go to call it, the dll gets loaded, and then SAS proceeds to crash (without any crash log that I can find).

4
  • could be that you declared the functions wrong, the default is not appropriate for this Commented Sep 16, 2014 at 12:24
  • That causing SAS to crash wouldn't surprise me; however, I have very limited experience with DLLs, and essentially none within D. I know within SAS they have to be using the __stdcall Gz calling convention, but I'm not sure how to verify this is being done within D.
    – charles
    Commented Sep 16, 2014 at 12:27
  • 1
    The crash is probably missing extern(Windows) like CyberShadow said. Just to clarify that sentence though, what I meant there was D can call or make functions callable from C, but it can't read C source code directly. Now, a lot of C code will also compile as D, and will mostly do the same thing, but there are some important details to check. extern is one of them. Matching types and layouts are important too. With int that is easy, a C int and a D int are the same. But passing strings between C and D is a bit trickier so if you try to do that, it is likely to crash too. Commented Sep 16, 2014 at 12:34
  • @AdamD.Ruppe: Thanks for the clarification, and also thanks for the book :)
    – charles
    Commented Sep 16, 2014 at 12:38

1 Answer 1

7

Yes, you can write DLLs in D which use or implement a C API.

You have to make sure that the function signatures and calling conventions match. In the page you linked, the calling convention is indicated as stdcall, so your D functions need to be annotated with extern(Windows) or extern(System).

1
  • Thanks! I found this page regarding extern after verifying this answer fixed the problem.
    – charles
    Commented Sep 16, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.