Click to See Complete Forum and Search --> : GlobalLock() and new..


ramneek.kochhar
July 13th, 2009, 04:19 AM
hi,
i have a basic question about the usage of GlobalLock() API.

As far as i know i have used it along with GlobalAlloc() and GlobalUnlock() in the following manner.

{.
.
HGLOBAL m_hDevMode= GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
DEVMODE *pDevMode = (DEVMODE *)GlobalLock(m_hDevMode);
memset(pDevMode, 0, sizeof(DEVMODE));
GlobalUnlock(m_hDevMode);
.
.}


but what if i use it with compiler memory management operators i.e. malloc and new.
i have the following code snippet.

{.
.
m_pDlgPageSetup = new PAGESETUPDLG; //standard PAGESETUPDLG structure of windows
ZeroMemory(m_pDlgPageSetup, sizeof(*m_pDlgPageSetup));
.
.}


{.
.
m_pDlgPageSetup->hDevMode = (HANDLE)NULL;
LPDEVMODE lpOutpmode = (LPDEVMODE)GlobalLock(m_pDlgPageSetup->hDevMode);
.
.}



is the above implementation fine??
if not then how shold i go around to get a pointer to a memory block allocated with new?? or can i access the HANDLE directly as a pointer since its not private to the process.

<all the above functions are in a single file which manages the printer for my application>

Thanks.

hoxsiew
July 13th, 2009, 08:30 AM
malloc and new are defined by the framework (C and C++ respectively). GlobalAlloc() and related functions are part of the Win32 API. As such they have no firm relationship to each other and cannot be assumed to share anything in common with each other. If you need to send data allocated with new or malloc to a function requiring a handle from GlobalAlloc, you'll need to create one of each and copy the relevant data between them.