www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can I typedef pointer to extern (C) function?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
I'm having trouble posting. If this succeeds, I'll post the rest in a follow-on.
Jan 26 2007
parent reply Rick Mann <rmann-d-lang latencyzero.com> writes:
Rick Mann Wrote:

 I'm having trouble posting. If this succeeds, I'll post the rest in a
follow-on.
So, I have the following example (gdc on Mac OS X, DMD 1.00-based): alias extern (C) uint function() EventHandlerProcPtr; extern (C) uint func1() { return 0; } uint func2() { return 0; } void main() { EventHandlerProcPtr p = &func1; EventHandlerProcPtr q = &func2; } /* Compiling with gdc generates the following errors: $ gdc callbacktest.d callbacktest.d:23: Error: cannot implicitly convert expression (& func1) of type uint(C *)() to uint(*)() $ */
Jan 26 2007
parent reply Rick Mann <rmann-d-lang latencyzero.com> writes:
Rick Mann Wrote:

 Rick Mann Wrote:
 
 I'm having trouble posting. If this succeeds, I'll post the rest in a
follow-on.
So, I have the following example (gdc on Mac OS X, DMD 1.00-based):
Sorry, forgot the rest of the question. It seems to dislike the "extern (C)" in my target function declaration, but I get bus errors if I omit it (as one might expect). Any suggestions would be most welcome.
 
 
 alias extern (C) uint function()		EventHandlerProcPtr;
 
 extern (C)
 uint
 func1()
 {
 	return 0;
 }
 
 uint
 func2()
 {
 	return 0;
 }
 
 void
 main()
 {
 	EventHandlerProcPtr p = &func1;
 	EventHandlerProcPtr q = &func2;
 }
 
 
 /*
 	Compiling with gdc generates the following errors:
 	
 	$ gdc callbacktest.d 
 	callbacktest.d:23: Error: cannot implicitly convert expression (& func1) of
type uint(C *)() to uint(*)()
 	$ 
 
 */
Jan 26 2007
parent reply Mike Parker <aldacron71 yahoo.com> writes:
Rick Mann wrote:
 Rick Mann Wrote:
 
 Rick Mann Wrote:

 I'm having trouble posting. If this succeeds, I'll post the rest in a
follow-on.
So, I have the following example (gdc on Mac OS X, DMD 1.00-based):
Sorry, forgot the rest of the question. It seems to dislike the "extern (C)" in my target function declaration, but I get bus errors if I omit it (as one might expect). Any suggestions would be most welcome.
Switch up the 'alias' and extern(C) statements: extern(C) alias uint function() EventHandlerProcPtr;
 extern (C)
 uint
 func1()
 {
 	return 0;
 }

 uint
 func2()
 {
 	return 0;
 }

 void
 main()
 {
 	EventHandlerProcPtr p = &func1;
 	EventHandlerProcPtr q = &func2;
 }
Also, as written, func2 is not extern(C), only func1 is. That's likely why you only saw the error for func1.
Jan 27 2007
parent reply Mike Parker <aldacron71 yahoo.com> writes:
Oh, and just to be pedantic, typedef and alias are not the same. The 
subject line says one and the code uses the other.
Jan 27 2007
parent Rick Mann <rmann-d-lang latencyzero.com> writes:
Mike Parker Wrote:

 Oh, and just to be pedantic, typedef and alias are not the same. The 
 subject line says one and the code uses the other.
Yeah; sorry about that. I had trouble posting it the first time (kept getting "Fail to post message"), and when I tried again later I had been experimenting, so I think my example got changed.
 Switch up the 'alias' and extern(C) statements:
 extern(C) alias uint function() EventHandlerProcPtr;
I did eventually discover that, and now I'm just getting Bus errors when my callback happens. My actual code is using Mac OS X's Carbon API, where it calls you back for events. I'm not sure what's causing the problem at this point, but I'll keep poking at it. Thanks!
Jan 27 2007