• Advertisement

Archived

This topic is now archived and is closed to further replies.

Win32, passing a HANDLE?

This topic is 5496 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

hello! I''m passing a handle around. It''s the window handle. cGraphicsCore must create a window for it. Then audio and keyboard can access it. cGraphicsCore::Initiate(m_hWnd, m_hInst) ); cAudioCore::Initiate(m_hWnd) ); cInputCore::Initiate() ); cICKeyboard::Initiate(m_hWnd) ); But inside cGraphicsCore I''m doing this: cGC_hWnd = parameter_hWnd; do stuff to cGC_hWnd. but I want to parameter_hWnd to be affected to! I thought HANDLES worked like pointers? "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Seriema
I thought HANDLES worked like pointers?


they do.

Share this post


Link to post
Share on other sites
Why don''t you pass a pointer to the handle?

''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking

Share this post


Link to post
Share on other sites
Because that would be retarded. Handles are structs containing a single 32-bit value, so passing them by address has no benefit.

Seriema: You''ll have to be a little more specific about exactly what you want to do with the window handle within the function.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
Handles are structs containing a single 32-bit value, so passing them by address has no benefit.


actually, handles are pointers to such structs.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by niyaw
[quote]Original post by ZealousElixir
Handles are structs containing a single 32-bit value, so passing them by address has no benefit.


actually, handles are pointers to such structs.
A HANDLE is only a number in a usermode process, that identifies a kernel object once a thread in the application does a transition to kernel mode.

I believe handle is defined as "void*", but that doesn''t make it a pointer.

Share this post


Link to post
Share on other sites
oki I''ll try again.

I have a cApplication, it contains a HWND as a member. Another class, cGraphicsCore, sets up the actual window. Other classes like cAudioCore and cInputCore require the window to be up and running. That''s why I call cGraphicsCore first, passing the cApplication::HWND as a parameter in cGraphicsCore::Initiate(). Although the window sets up alright and all, the cApplication::HWND doesn''t seem to "HANDLE" it

I tried taking in a HWND*, and it worked. But somehow it doesn''t feel right...

Did this not work because HWND didn''t HANDLE a window from start? If cApplication created the window, then passing the HWND would do the same as sending a pointer. Right?.. :/

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
A HANDLE is only a number in a usermode process, that identifies a kernel object once a thread in the application does a transition to kernel mode.


a handle can be an opaque pointer, and they don''t have to originate in kernel.
quote:

I believe handle is defined as "void*", but that doesn''t make it a pointer.

handle is often defined as a pointer to a struct containing a single dummy integer value, to prevent assignments such as

HWND hwnd = ...;
HDC hdc = hwnd;

which is what i said earlier. the "generic" handle is a void pointer, and while this definition is easy to use it also leaves a chance of passing a particular handle to functions that don''t work with that handle''s type.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This thread is painful to read . Everyone repeat after me: A HANDLE IS NOT A POINTER. I repeat, A HANDLE IS NOT A POINTER.

The exact meaning of "handle" seems to vary greatly from one platform to another so some confusion has arisen. In the Windows world, a handle is simply a value that is used as an identifier. As an application programmer, you should not assume that a handle is anything beyond some number used internally by the system.

For example, if you view the value of kernel handles (such as the handle returned by CreateFile), you''ll see that these are low numbers -- definitely not valid pointers.

Some handles are pointers. For example, if you call LoadLibrary, the resulting HMODULE is a pointer to the location in the application''s memory space that the DLL has been loaded. However, you should not rely on this. As far as I know, it''s undocumented and subject to change in future versions.

That being said, HANDLE is declared as a void* in the Windows headers. Under 32-bit Windows, handles could be represented in the API as unsigned longs without any ill effect. I''m assuming void* is used either for historical reasons or to allow HANDLE to be polymorphic with the stricter handle types or to allow handle size to be equivalent to pointer size.

Seriema, the practical benefit of this to you is that you should not view handles as being just like pointers. View them simply as some value that means something to Windows. Treat them as you would an int, not as you would (normally) treat a pointer.

Where are you creating the HWND? In cGraphicsCore Initiate or before?

Share this post


Link to post
Share on other sites
How about creating a structure that has reference to the handle. This is one solution I use to cut down on global objects or numerous calls to get a HANDLE.

//declared at top with other global identifiers
struct tagWIN_OBJ
{
HANDLE myHandle;
//can include more identifiers if needed ie:
HWND myHwnd;
};

tagWIN_OBJ Win_obj;

//in WINMAIN do something like this:
Win_obj.myHandle = handle;

Now you not need HWND in your function paramater just use
Win_obj.myHandle directly in your function. Hope this helps



-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
quote:
I tried taking in a HWND*, and it worked. But somehow it doesn''t feel right...


I was told, by a moderator no less, that this was retarded.
I think an apology is in order ZealousElixir...


''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:

I''m passing a handle around. It''s the window handle. cGraphicsCore must create a window for it.

cGraphicsCore::Initiate(m_hWnd, m_hInst) );
//...



Indeed, if it is not a pointer passed into CGraphicCore, how will the actual handle value ever "get out".

I think there was an assumption that cApplication would create the window - that''s what I expected anyway.

Share this post


Link to post
Share on other sites
quote:
Original post by Sailorstick

I was told, by a moderator no less, that this was retarded.
I think an apology is in order ZealousElixir...



EDIT I was wrong above in saying "handle" generically. I meant HWND.
I was also wrong in thinking that the value of the HWND would not be modified. It shouldn't, and this indicates piss-poor class structure, but I do retract the statement that it is "unnecessary" to pass a pointer to the HWND (though I'm still not convinced that passing a vanilla HWND wouldn't have the same effect)

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on February 4, 2003 1:04:20 AM]

Share this post


Link to post
Share on other sites

  • Advertisement