[comp.lang.c] Casting to far proc ptr

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (10/05/90)

  I need to pass to address of a procedure to a called function. The
procedure is near static, and I need to cast to far, or assign the
address to a far ptr.

  Is there a sytax which will tell the compiler what I want? The obvious
cast compiles fine but doesn't generate the requisite code.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
    VMS is a text-only adventure game. If you win you can use unix.

dwatney@pine.circa.ufl.edu (SUTHERLAND) (10/06/90)

In article <2734@crdos1.crd.ge.COM>, davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes...
>  I need to pass to address of a procedure to a called function. The
>procedure is near static, and I need to cast to far, or assign the
>address to a far ptr.

The differences between near and far functions exist beyond the calling
routine.  the called function knows whether it is near or far and uses
the appropriate return.  so, in effect, the code you have now is probably
using a far call to a routine which in turn is using a near return.
A definite candidate for "undefined behavior", I would say, :)

But, you say, this doesn't solve my problem!  Well, how about this:
make a far function in the same segment as the function you want
which calls (near call) that function and then pass the pointer to
this new far funtion to whatever needs it?

client_function() far calls bridge_function() near calls desired_function()

-----
Marshall Sutherland, Partner/Consultant, Digital Magic Computer Consulting
dwatney@pine.circa.ufl.edu, dwatney@ufpine.bitnet
Sysop, Farthinghale Arms BBS, (904) 378-4861

kaiser@ananke.stgt.sub.org (Andreas Kaiser) (10/06/90)

In a message of <Oct 05 13:58>, Wm E Davidsen Jr (davidsen@crdos1.crd.ge.COM ) writes: 

 WEDJ>   I need to pass to address of a procedure to a called function. The
 WEDJ> procedure is near static, and I need to cast to far, or assign the
 WEDJ> address to a far ptr.

You cannot call a near function via a far pointer. If you find way to cast the 
pointer, the called functions parameters will be addressed wrong and 
a return from the called funtion crashes you program. Near functions in a large 
model should never be used with function pointers. If you need the pointer, you 
should strip off the "near" attribute.

                Gruss, Andreas

 

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) (10/07/90)

(Intel- and Turbo C-specific stuff follows.)

In <2734@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen
Jr) writes:

>  I need to pass to address of a procedure to a called function. The
>procedure is near static, and I need to cast to far, or assign the
>address to a far ptr.

Even if you can call the function, I think you will have problems,
because it will have been compiled to do a near return.  It will pop 16
bits off the stack and jump there.  (Had Intel reversed the order in
which segment and offset are pushed on the stack in a far call, it
would have been possible to define a far function that could be called
from both near and far.)

Just finding the address of the function shouldn't be difficult,
though.

     far_address = MK_FP(_CS, func);   /* not tested */
--
Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi