www.digitalmars.com         C & C++   DMDScript  

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

reply 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
parent "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