Jump to content
  • Advertisement

Archived

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

Clash Rocker

[Win32 API]Copying file HANDLEs

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

How would I go about copying a file HANDLE foo; to HANDLE bar And then CloseHandle(bar); If I do this, foo becomes destroyed Edited by - Clash Rocker on July 17, 2001 9:56:16 AM

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Check this out...
HANDLE CopyHandle(HANDLE hSrc)
{
// Return a handle to a copy of the data
// that the passed handle was for.
if (!hSrv) return NULL;
DWORD dwLen = GlobalSize(hSrc);
HANDLE hCopy = GlobalAlloc(GHND, dwLen);
if(hCopy)
{
BYTE* lpCopy = (BYTE*)GlobalLock(hCopy);
BYTE* lp = (BYTE*)GlobalLock(hSrc);
CopyMemory(lpCopy,lp,dwLen);
GlobalUnlock(hCopy);
GlobalUnlock(hSrv);
}
return hCopy;
}

Share this post


Link to post
Share on other sites
Uh, no. What that code does is copy a HGLOBAL not a HANDLE.

I assume Clash wants to copy an actual HANDLE because CloseHandle doesn''t work on HGLOBAL''s.

You can copy handles by calling DuplicateHandle.

-Mike

Share this post


Link to post
Share on other sites
Thanks Anon.
I hadn''t tried out AP''s code yet. Opon a glance it looked alright to me...and still does. But I''ll take your word for it. DuplicateHandle() is so much more nicer

Share this post


Link to post
Share on other sites
Well, as I recall from MSDN Documentation,
DuplicateHandle just make an exact copy of the input
handle:

"The DuplicateHandle function duplicates an object handle. The duplicate handle refers to the same object as the original handle. Therefore, any changes to the object are reflected through both handles. For example, the current file mark for a file handle is always the same for both handles"

Share this post


Link to post
Share on other sites

  • 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!