www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how call a c function with stdcall?

reply "mzfhhhh" <mzfhhhh foxmail.com> writes:
for example:use vc compile on x86
func.c:
__declspec(dllexport) int _stdcall sub(int a,int b)
{
	return a-b;
}

func.def:

LIBRARY
EXPORTS
sub

-----------------
i use "implib.exe" to create a omf format lib.
----------------
D code:
import std.exception;

//this is a cdecl call
extern(C) int sub(int a,int b);

void main()
{
     auto c1 = sub(2,1);

     //when this line run,the c1 value is broken
     auto c2 = enforceEx!Exception(sub(1,2));
}
------------------
my question is how write D code to call the c function
Mar 24 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
try extern(Windows) isntead of extern(C).
Mar 24 2015
parent reply "mzfhhhh" <mzfhhhh foxmail.com> writes:
On Tuesday, 24 March 2015 at 15:26:22 UTC, Adam D. Ruppe wrote:
 try extern(Windows) isntead of extern(C).
use extern(Windows) or extern(System) the compile show the link error: Error 42: Symbol Undefined _sub 8 ---------------- dll export func name is "sub", and the dll is 3rd party,i can't change the dll code. i use "implib.exe /system func.lib func.dll" to create a func.lib. how can i link correctly?
Mar 24 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 25 March 2015 at 00:09:33 UTC, mzfhhhh wrote:
 i use "implib.exe /system func.lib func.dll" to create a 
 func.lib.
You might also need to specify the .def file as the final argument to that. http://digitalmars.com/ctg/implib.html The def file can list aliases for the functions. Maybe try it without the /system flag too. I know there's a way but I don't know exactly what, I just tend to guess and check a little...
Mar 24 2015
parent "mzfhhhh" <mzfhhhh foxmail.com> writes:
On Wednesday, 25 March 2015 at 00:26:21 UTC, Adam D. Ruppe wrote:
 On Wednesday, 25 March 2015 at 00:09:33 UTC, mzfhhhh wrote:
 i use "implib.exe /system func.lib func.dll" to create a 
 func.lib.
You might also need to specify the .def file as the final argument to that. http://digitalmars.com/ctg/implib.html The def file can list aliases for the functions. Maybe try it without the /system flag too. I know there's a way but I don't know exactly what, I just tend to guess and check a little...
1.def file: LIBRARY "func" EXPORTS _sub 8 = sub 2.create lib implib.exe func.lib func.def ------------------------- now it works fine,thanks!
Mar 24 2015