Jump to content
  • Advertisement
Sign in to follow this  
SirLuthor

Using handles acceptable?

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good even fine people! A question, if I may be so bold! Anyway, I'm working on an engine as some of you may know, and I've lately been thinking of how I'm going to do client to DLL interaction, that is, how are people going to use my engine. Now, since it is in an explicitly linked DLL, it becomes odious to export classes, a load of extra hassle that I'm sure few people want. However, I was thinking, would it be too slow to do things as the Win32 API does it, that is, returning handles, that are passed to functions, and used as lookup values in a table to determine the object in question? Added benefits of this are of course that it can be used with straight C as well, if done properly, and, it keeps things nice and simple. However, I'm greatly fearing that it will be terribly slow, having to lookup an object in a table that could be large, every time something is used. So I really have no clue which one I should go with. So what are your opinions/experiences? Is it possible with handles, or should I wrap those classes, come hell or high water? It would also help if people could cite examples of commercially accepted engines that use whatever method. Much obliged.

Share this post


Link to post
Share on other sites
Advertisement
You know, you can just use pointers as your handles. Cast the pointers to your handle type and back to a pointer type. That way you don't need to index into a table.

Share this post


Link to post
Share on other sites
It's how many of the Windows handles work. If you want you can make the handles pointers to undefined types (types undefined in the code the client sees that is). However, if users get handled pointers they might think they need to do pointer like things to them like delete.

Share this post


Link to post
Share on other sites
Hehehe, a good point. Thanks for clearing that up, I don't know why I didn't think of that my self, to use the address as the handle [grin]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i have a question.. could someone give me a hint why handles are used at all when they are more or less pointers (depending on the implementation of course) and can be cast to one? is it just to give it a c++ look and hiding the obvious i.e. protecting the pointer from being deleted by someone who shouldn't ?

what's the point in having them?

regards
- simon.

Share this post


Link to post
Share on other sites
Handles are an abstraction. They may be disguised pointers, or the may be indexes into a table, or they may be something else entirely. The point is the client of the API doesn't know what they are and doesn't actually care. This allows the API to be used in languages that are not pointer aware like Visual Basic, and allows the implementation of the API to change representation behind the scenes without bothering the client code. For example some handles that were disguised pointers in Windows 95 were indicies into tables in NT 4.

Share this post


Link to post
Share on other sites
They're also more useful in C than in C++, they hide data and force you to take action through functions, roughly similar to encapsulation provided by C++ classes and structs.

Let's take Win32, you get an HWND, which may actually be a pointer to a struct, but you can't manipulate the data in that struct because you have no information on the struct whatsoever. If you want to do something with the HWND you'll have to use a function. The function probably just casts it to a struct pointer and does whatever it needs to do with it, and all you know is that it got done. Very useful in a C environment.

Share this post


Link to post
Share on other sites


Another reason for handles is so the "server"/engine side can relocate the objects at will and the handle never changes. Handles allow the "server" side to abstract any and all access or location information. Some console games use handles so that they can defrag memory when needed and not effect the client game code. The downside is it can take more memory accesses/clock cycles to use the handle. Whether to use them or not depends on the requirements for your application. Just weigh the gains vs tradeoffs and decide based on that. There is no "better" general solution.

Share this post


Link to post
Share on other sites
Sounds like a way complicated scheme to solve a problem which shouldn't exist.

If you can't pass pointers to objects to / from DLL function calls, you shouldn't be using a DLL - it's obviously not a usable mechanism.

The problem should not exist. Don't make an unwieldy solution for a problem which should not exist.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!