BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Importing function pointers from a DLL

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Andrue Cope [TeamB]
Guest





PostPosted: Tue Dec 12, 2006 8:11 pm    Post subject: Importing function pointers from a DLL Reply with quote



Hiya,

This was a new one on me. I've used GetProcAddress() many times but
this is the first time I've run across someone exporting a function
pointer instead of a function. My fix for the moment is the slightly
scary:

typedef int WINAPI (*TFn)( void * );
....
TFn Fn=TFn( GetProcAddress( HInstance, "FN" ) );

memcpy( &Fn,
reinterpret_cast<void *>( Fn ),
sizeof( Fn ) );

I had a quick play with the typedef to see if I could work out how to
import the function pointer properly but couldn't get my head around.
Since I have a solution that works for me I'm moving on to other
problems but can some kind soul suggest a way to get rid of the
memcpy() operation?
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Roddy Pratt
Guest





PostPosted: Tue Dec 12, 2006 8:11 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote



Andrue Cope [TeamB] wrote:

Quote:
typedef int WINAPI (*TFn)( void * );
...
TFn Fn=TFn( GetProcAddress( HInstance, "FN" ) );


Maybe I've misunderstood, but how about this?

typedef int WINAPI (*TFn)( void * );
typedef TFn* PTFn;

int test(void *param)
{
TFn Fn=*PTFn( GetProcAddress( HInstance, "test" ) );
return Fn(param);
}

My motto: Never do in one typedef what you can do in two!

- Roddy
Back to top
Alan Bellingham
Guest





PostPosted: Tue Dec 12, 2006 9:00 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote



"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> wrote:

Quote:
This was a new one on me. I've used GetProcAddress() many times but
this is the first time I've run across someone exporting a function
pointer instead of a function. My fix for the moment is the slightly
scary:

typedef int WINAPI (*TFn)( void * );

Defining a function pointer type. So far, so good.

Quote:
...
TFn Fn=TFn( GetProcAddress( HInstance, "FN" ) );

OK, you've successfully assigned to a function pointer. Now onto stage 2
....

Quote:
memcpy( &Fn,
reinterpret_cast<void *>( Fn ),
sizeof( Fn ) );

You're trying to cast a function pointer to a normal pointer. You're not
allowed to do that.

Having attempted to cast your function pointer to a normal pointer,
you're then attempting to copy the result back to the function pointer.

Do you see something wrong with this scenario?

Umm, what does the export itself look like? I'm confused as to how it
could look any different from the normal export, but I suspect that what
you are missing is probably an extra level of indirection. So, maybe
something like

typedef int WINAPI (*TFn)( void * );
typedef TFn* TfnIndirect;

TfnIndirect FnInd = TfnIndirect(GetProcAddress(0, "FN"));
TFn Fn = *FnInd;

(Compiles, but definitely not tried!)

Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue Dec 12, 2006 10:21 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

Alan Bellingham wrote:

Quote:
memcpy( &Fn,
reinterpret_cast<void *>( Fn ),
sizeof( Fn ) );

You're trying to cast a function pointer to a normal pointer. You're
not allowed to do that.


I am - the compiler is happy (probably only because I used
reinterpret_cast) but it's legal C++ :)

Quote:
Having attempted to cast your function pointer to a normal pointer,
you're then attempting to copy the result back to the function
pointer.

Which works fine.

The problem I'm trying to resolve is that the address returned by
GetProcAddress() is not the address of the function being exported.
Instead it's the address of the address.

What my hack is doing (and yes it's a hack, legal, but a hack) is to
take the four bytes pointed to by GetProcAddress() and copy them into
the function pointer so that the function pointer is useable. In effect
I'm doing a one-time-only dereference of what GetProcAddress() returns.

In pseudo code:

Function_Pointer=*( GetProcAddress() );

And this works fine. My only problem is that I don't like having to use
memcpy() and I'd like to know how to import it properly.

Quote:
Umm, what does the export itself look like?

_declspec(dllexport) int (*Fn)(void);

Quote:
I'm confused as to how it
could look any different from the normal export, but I suspect that
what you are missing is probably an extra level of indirection. So,
maybe something like

typedef int WINAPI (*TFn)( void * );
typedef TFn* TfnIndirect;

TfnIndirect FnInd = TfnIndirect(GetProcAddress(0, "FN"));
TFn Fn = *FnInd;

(Compiles, but definitely not tried!)

I'll give that a try tomorrow. As is usually the case with these things
this was supposed to be a minor background task to fit into a quiet
moment away from my primary C# project. I had a couple of goes at
fixing the declaration then gave up.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue Dec 12, 2006 10:23 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

Yah, that's what Alan has suggested. I'll let you know how I get on
tomorrow.

My motto:Nothing is ever as simple as it appears.

The code that uses the DLL (the stuff that does all the work) turned
out to be simple, elegant and worked like a charm. The bit that I've
done dozens of times before and is hardly rocket science (loading a
DLL) had me tearing my hair out. Ho hum.

I'm hoping to find out /why/ it was done this way but atm I can only
assume it's deliberate obfustication.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Alan Bellingham
Guest





PostPosted: Tue Dec 12, 2006 10:57 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> wrote:

Quote:
Yah, that's what Alan has suggested. I'll let you know how I get on
tomorrow.

But Roddy has his clock set so it looks like he got in before me. In
fact, it even looks like he answered before you asked!

Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
Back to top
Alan Bellingham
Guest





PostPosted: Tue Dec 12, 2006 11:00 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> wrote:

Quote:
I am - the compiler is happy (probably only because I used
reinterpret_cast) but it's legal C++ Smile

Ah, OK. I had thought that was going across a boundary it wasn't allowed
across. Maybe I was thinking normal pointer <=> member pointer.

I can only think that the reason the writer exports the pointer is so
that the same export can provide different functions behind it,
depending on circumstances. I can sort of image a scenario in which it
makes perfect sense, but it is definitely unusual.

Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
Back to top
Roddy Pratt
Guest





PostPosted: Wed Dec 13, 2006 1:26 am    Post subject: Re: Importing function pointers from a DLL Reply with quote

Alan Bellingham wrote:

Quote:
But Roddy has his clock set so it looks like he got in before me. In
fact, it even looks like he answered before you asked!

I had Xananews "Generate Date Headers" disabled, so the time (with
offset) seems to actually come from the newsgroup server! My PC *is*
correct.

I will accept you actually beat me by a whole 56 seconds, although the
15:00:00 time looks definitely dodgy ;-)

- Roddy
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Dec 13, 2006 3:23 am    Post subject: Re: Importing function pointers from a DLL Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> wrote in message
news:457ed73b (AT) newsgroups (DOT) borland.com...

Quote:
I'm hoping to find out /why/ it was done this way but atm I
can only assume it's deliberate obfustication.

Maybe the DLL wants to export a single entry point that maps to different
internal functions under different conditions?


Gambit
Back to top
Alisdair Meredith[TeamB]
Guest





PostPosted: Wed Dec 13, 2006 3:29 am    Post subject: Re: Importing function pointers from a DLL Reply with quote

Andrue Cope [TeamB] wrote:

Quote:
I am - the compiler is happy (probably only because I used
reinterpret_cast) but it's legal C++ Smile

Technically, it is undefined behaviour that has the desired effect on
the platform you use <g>

Data pointers and function pointers are not interchangable, and indeed
there are platforms where they are different sizes. In 32 bit Windows
world, which is all we are usually worried about in these groups, then
you will probably get away with this for some considerable time to
come. I might be happier with some static assertions that your
assumptions hold though (sizeof(void*) == sizeof(TFn)) in case 64 bit
windows or some future platform holds an interesting surprise...

--
AlisdairM(TeamB)
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed Dec 13, 2006 3:59 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

Remy Lebeau (TeamB) wrote:

Quote:
I'm hoping to find out why it was done this way but atm I
can only assume it's deliberate obfustication.

Maybe the DLL wants to export a single entry point that maps to
different internal functions under different conditions?

Could be I suppose. OTOH for this DLL it may be an attempt at
obfustication as well. It sure confused me.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Wed Dec 13, 2006 4:01 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

Alisdair Meredith[TeamB] wrote:

Quote:
Technically, it is undefined behaviour that has the desired effect on
the platform you use <g

Yeah, which is why I wanted a better solution :)

Thankfully Roddy's and Alan's solution works so I have removed the
memcpy.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Alan Bellingham
Guest





PostPosted: Wed Dec 13, 2006 5:38 pm    Post subject: Re: Importing function pointers from a DLL Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> wrote:

Quote:
Yeah, which is why I wanted a better solution :)

Thankfully Roddy's and Alan's solution works so I have removed the
memcpy.

Thanks.

It did appear closer to the description of what was going on.

Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.