CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Posts
    16

    Lightbulb GlobalLock() and new..

    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.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: GlobalLock() and new..

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured