www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - Calling C functions with D function pointers as operands

↑ ↓ ← Zarathustra <adam.chrapkowski gmail.com> writes:
How can I call C functions with D function pointers as operands?

I tried this:
	// gl2psUserWritePNG & gl2psUserFlushPNG are static
	void function(png_struct*, ubyte*, uint) gl2psUserWritePNG_ptr
= &gl2psUserWritePNG;
	void function(png_struct*)
gl2psUserFlushPNG_ptr = &gl2psUserFlushPNG;

	png_set_write_fn(
		png_ptr,
		cast(void*)png,
		gl2psUserFlushPNG_ptr,
		gl2psUserFlushPNG_ptr
	);

but there are some errors:
 Error: cannot implicitly convert expression (gl2psUserWritePNG_ptr)
of type void function(png_struct*, ubyte*, uint) to voidC  function
(png_struct*, ubyte*, uint)

 Error: cannot implicitly convert expression (gl2psUserFlushPNG_ptr)
of type void function(png_struct*) to voidC  function(png_struct*)
Apr 26 2008
↑ ↓ → "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Zarathustra" <adam.chrapkowski gmail.com> wrote in message 
news:fuvj7c$1hf7$1 digitalmars.com...
 How can I call C functions with D function pointers as operands?

 I tried this:
 // gl2psUserWritePNG & gl2psUserFlushPNG are static
 void function(png_struct*, ubyte*, uint) gl2psUserWritePNG_ptr
 = &gl2psUserWritePNG;
 void function(png_struct*)
 gl2psUserFlushPNG_ptr = &gl2psUserFlushPNG;

 png_set_write_fn(
 png_ptr,
 cast(void*)png,
 gl2psUserFlushPNG_ptr,
 gl2psUserFlushPNG_ptr
 );

 but there are some errors:
 Error: cannot implicitly convert expression (gl2psUserWritePNG_ptr)
 of type void function(png_struct*, ubyte*, uint) to voidC  function
 (png_struct*, ubyte*, uint)

 Error: cannot implicitly convert expression (gl2psUserFlushPNG_ptr)
 of type void function(png_struct*) to voidC  function(png_struct*)

You have to declare your callback functions as extern(C), like so: extern(C) void gl2psUserFlushPNG(png_struct* s) { ... } Same with the other one.
Apr 26 2008