Visual C++ Function Address

Programming, for all ages and all languages.
Post Reply
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Visual C++ Function Address

Post by InsightSoft »

Hi,

I have this problem:

1--- the declaration

Code: Select all

void *irq_routines[16];
2--- the values

Code: Select all

void _cls_irq::InstallHandler(int irq, void (*handler)(tpRegistersStructure *)) 
{
	 irq_routines[irq] = (void *)handler;
}
and

Code: Select all

void handler_Timer(tpRegistersStructure *r)
{
    blablabla
}
then

Code: Select all

IRQ.InstallHandler(0, handler_Timer);            //get handler_timer function address and put on irq_routines[0]
3----- the problem (for every irq)

Code: Select all

void handler_IRQ(tpRegistersStructure *r)
{
    void (*handler)(tpRegistersStructure *r);            //pointer for incoming function with this format -> return (void *) and 
                                                                                       //get (tpRegistersStructure *r) as parameters
    
      handler = irq_routines[(r->int_no - 32)];       //with this index, get its correspondent function address

      (this does not run!!!!!!!!!!!!!!!!!! -> Can you tel me why???

this simple example just works fine:

Code: Select all

void (*p)(int i); 
p = myfunction;  
(*p)(23); // call myfunction with 23 as parameter
[ JamesM: Added code tags. ]
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Visual C++ Function Address

Post by pcmattman »

Firstly, please use code tags - they make it far easier to read your post!

Code: Select all

handler = irq_routines[(r->int_no - 32)]; //with this index, get its correspondent function address
Do you actually ever call the function?

Code: Select all

handler(r);
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Visual C++ Function Address

Post by InsightSoft »

Yap...

Code: Select all

void handler_IRQ(tpRegistersStructure *r)
{
    void (*handler)(tpRegistersStructure *r);
	handler = irq_routines[(r->int_no - 32)];						//to discover in matrix that start from 0 index
    if(handler)
    {
        handler(r);												//execute this address
    }
    if(r->int_no >= 40)
    {
		Ports.SendByte(0xA0, 0x20);
    }
    Ports.SendByte(0x20, 0x20);
}
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Visual C++ Function Address

Post by InsightSoft »

The result is:

error C2440: '=' : cannot convert from 'void *' to 'void (__cdecl *)(tpRegistersStructure *)'
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Visual C++ Function Address

Post by pcmattman »

Okay, that's because you've defined irq_handlers as:

Code: Select all

void *irq_routines[16];
Yet you want to use it as a:

Code: Select all

void (*handler)(tpRegistersStructure *)
You either need to use a cast when obtaining the IRQ handler from the array, or you need to typedef the handler function pointer type and use that instead of void*.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Visual C++ Function Address

Post by JamesM »

Hi,

Firstly, you didn't mention if the IRQ handler gets called at all - you only mentioned the IRQ dispatch afterwards. Does the function IRQ_Handler get called when an IRQ comes in?

If not, which is what I suspect, have you remapped the PIC? Have you enabled interrupts?

Cheers,

James
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Visual C++ Function Address

Post by InsightSoft »

Yap...
...remember: that error is at compilation stage...
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Visual C++ Function Address

Post by pcmattman »

And I told you how to fix the compilation error. Read my post for the answer :)
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Visual C++ Function Address

Post by InsightSoft »

Ya... I know... its what I am trying to do
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Visual C++ Function Address

Post by InsightSoft »

pcmattman wrote:Okay, that's because you've defined irq_handlers as:

Code: Select all

void *irq_routines[16];
Yet you want to use it as a:

Code: Select all

void (*handler)(tpRegistersStructure *)
You either need to use a cast when obtaining the IRQ handler from the array, or you need to typedef the handler function pointer type and use that instead of void*.

And how to do that? can you help me?
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Visual C++ Function Address

Post by Creature »

Gah.. Please know your function pointers before you use them:

Code: Select all

typedef void (*FunctionPointerType)(tpRegistersStructure *);

FunctionPointerType ArrayOfFunctionPointers[16];
Now you (should) have an array of function pointers which point to a function with 'void' as return type and 1 argument, namely a 'tpRegistersStructure' pointer.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Visual C++ Function Address

Post by DeletedAccount »

Hi,
This a a general 'C' question and has nothing to do with VC++ compiler . Bad topic


Regards
Shrek
Post Reply