Page 1 of 1

Visual C++ Function Address

Posted: Fri Apr 24, 2009 6:26 pm
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. ]

Re: Visual C++ Function Address

Posted: Fri Apr 24, 2009 6:29 pm
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);

Re: Visual C++ Function Address

Posted: Fri Apr 24, 2009 6:32 pm
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);
}

Re: Visual C++ Function Address

Posted: Fri Apr 24, 2009 6:33 pm
by InsightSoft
The result is:

error C2440: '=' : cannot convert from 'void *' to 'void (__cdecl *)(tpRegistersStructure *)'

Re: Visual C++ Function Address

Posted: Fri Apr 24, 2009 6:36 pm
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*.

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 5:32 am
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

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 5:51 am
by InsightSoft
Yap...
...remember: that error is at compilation stage...

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 6:15 am
by pcmattman
And I told you how to fix the compilation error. Read my post for the answer :)

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 6:20 am
by InsightSoft
Ya... I know... its what I am trying to do

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 8:17 am
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?

Re: Visual C++ Function Address

Posted: Sat Apr 25, 2009 1:02 pm
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.

Re: Visual C++ Function Address

Posted: Sun Apr 26, 2009 2:49 am
by JamesM

Re: Visual C++ Function Address

Posted: Sun Apr 26, 2009 3:21 am
by DeletedAccount
Hi,
This a a general 'C' question and has nothing to do with VC++ compiler . Bad topic


Regards
Shrek