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).

Community
  • 1
  • 1
charles
  • 547
  • 1
  • 3
  • 11
  • could be that you declared the functions wrong, the default is not appropriate for this – ratchet freak Sep 16 '14 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 Sep 16 '14 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. – Adam D. Ruppe Sep 16 '14 at 12:34
  • @AdamD.Ruppe: Thanks for the clarification, and also thanks for the book :) – charles Sep 16 '14 at 12:38

1 Answers1

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).

Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
  • Thanks! I found [this page](http://dlang.org/attribute.html#linkage) regarding extern after verifying this answer fixed the problem. – charles Sep 16 '14 at 12:36