View mode: basic / threaded / horizontal-split · Log in · Help
May 04, 2004
Re: How to call a dll function from D?
Thanks again for the help, you're the only one who has given me sensible
responses. Tonight I had an idea (don't know why I didn't think of it before), I
searched the dmd directory for GetProcAddress, and to my surprise I found a
sample file which gave an example. You were actually very close, the only thing
that was off was the alias declaration, it should have been:

extern (C) alias void (*ptr)(/* args */);

This is not particularly clear from the documentation.

Herbert

In article <c76bn6$at5$1@digitaldaemon.com>, Eric Anderton says...
>
>>Thanks Eric, the new syntax works for me. The only issue now I have is
>>specifying the calling convention, I need to specify cdecl. I'm currently
>>getting an access violation which I assume is due to a bad calling mechanism.
>
>This is just a shot in the dark, but have you tried prefixing the handle
>definition with "extern(C)"?
>
>extern(C) function(int foo) bar; // maybe this will create the right call type?
>
>After looking over Walter's D documentation again, I think the "extern"
>statement gets double-duty when compared to C/C++.  Where in C we use "extern"
>to mean "declared later/elsewhere", D uses it for that as well as the calling
>convention.  Add to that the fact that DMD makes multiple passes on any given
>source file, and it really shifts the meaning more towards being a call-type
>declaration.
>
>Again, no D compiler in sight until I get home... so here goes nothing, right?
>
>- Eric
>
>from: http://www.digitalmars.com/d/htomodule.html
>
>__cdecl, __pascal, __stdcall
>
>int __cdecl x;
>int __cdecl foo(int a);
>int __pascal bar(int b);
>int __stdcall abc(int c);
>
>
>become:
>
>extern (C) int x;
>extern (C) int foo(int a);
>extern (Pascal) int bar(int b);
>extern (Windows) int abc(int c);
>
>
>
May 04, 2004
Re: How to call a dll function from D?
In article <c74emg$fe2$1@digitaldaemon.com>, snowflake says...
>
>I've read through the docs but I can't seem to work out how to declare a
>function that can to be loaded from a specific dll. The calling convention is
>cdecl. Is there a sort of equivalent loadlibrary() machanism? I tried to link
>the function via a lib file but the linker says "Error 43: Not a Valid Library
>File", even though the file is used by other apps on the computer. 
>
>HMS
>
>

It seems that you want to link with a specific DLL in compile time, instead of
run-time, right ? Ok, here it is:

DigitalMars has a tool named 'implib.exe' which is part of the DigitalMars C/C++
compiler package which takes a DLL as an argument and produces a .lib file which
can be used with the DM linker. 

For example, let's say you have 

foo.dll 

which contains the function 

int foo(int)

You do the following steps:

1) execute: 

implib foo.dll foo.lib

2) Link with foo.lib.

3) in your program, you must have: 

extern(C) int foo(int);

4) use function 'foo' normally.

This is how I used the Allegro DLL with D.
May 04, 2004
Re: How to call a dll function from D?
>Thanks again for the help, you're the only one who has given me sensible
>responses. 

You're quite welcome.  Anything to help a fellow developer.  Be sure to let us
(the NG) know how the project pans out.

>Tonight I had an idea (don't know why I didn't think of it before), I
>searched the dmd directory for GetProcAddress, and to my surprise I found a
>sample file which gave an example. 

So I'm not the only one sifting through Walter's code! ::chuckle::  Lately, I've
taken to pulling apart Phobos to learn more about the platform.  IMO, this is
the best way (at least for now) to learn how to do "undocumented" things with
the D platform.

And as far as source goes, its a pretty good read. ;

- Eric
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home