Thread overview
Can I typedef pointer to extern (C) function?
Jan 27, 2007
Rick Mann
Jan 27, 2007
Rick Mann
Jan 27, 2007
Rick Mann
Jan 27, 2007
Mike Parker
Jan 27, 2007
Mike Parker
Jan 27, 2007
Rick Mann
January 27, 2007
I'm having trouble posting. If this succeeds, I'll post the rest in a follow-on.
January 27, 2007
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(*)()
	$

*/
January 27, 2007
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(*)()
> 	$
> 
> */

January 27, 2007
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.
January 27, 2007
Oh, and just to be pedantic, typedef and alias are not the same. The subject line says one and the code uses the other.
January 27, 2007
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!