how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

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


Re: how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

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?


Re: how call a c function with stdcall?

2015-03-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 25 March 2015 at 00:09:33 UTC, mzf 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...


Re: how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

On Wednesday, 25 March 2015 at 00:26:21 UTC, Adam D. Ruppe wrote:

On Wednesday, 25 March 2015 at 00:09:33 UTC, mzf 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!


Re: how call a c function with stdcall?

2015-03-24 Thread Adam D. Ruppe via Digitalmars-d-learn

try extern(Windows) isntead of extern(C).