CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    May 2019
    Posts
    53

    [RESOLVED] EnumServicesStatusEx and Process Walking - How to allocate and free memory?

    Got another question:

    About Memory Management in EnumServicesStatusEx().

    What's the best way to capture the amount of memory needed from the first call to EnumServicesStatusEx() (where the 5th and 6th arguments are NULL and 0 respectively)?

    How should I allocate that memory to get around the if(GetLastError() == ERROR_MORE_DATA) scenario?

    How can I safely and reliably free that allocated memory when I'm done parsing the Process struct and want to discard it?

  2. #2
    Join Date
    May 2019
    Posts
    53

    Re: EnumServicesStatusEx and Process Walking - How to allocate and free memory?

    Found a fairly comprehensive set of functions() to do the requested operations now... especially thanks to your input @VictorN and @IgorVartanov :

    Code:
    if(handleName != INVALID_HANDLE_VALUE && handleName != 0
    {
    CloseHandle(handleName);
    }
    Also, in the special case of the Service Control Manager handle opened with OpenSCManager() function...:

    Code:
    if(scHandle != INVALID_HANDLE_VALUE && scHandle != 0)
    {
    CloseServiceHandle(scHandle);
    }
    We receive runtime Exception errors if the handles are already invalid or if we use CloseHandle()/FindClose() with the Service Control Manager handle...
    Last edited by Arianax; July 19th, 2019 at 10:25 AM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] EnumServicesStatusEx and Process Walking - How to allocate and free me

    You can simplify your code by not checking for INVALID_HANDLE_VALUE or NULL before calling the appropriate close handle api.

    If the value is INVALID_HANDLE_VALUE or NULL, the close handle family of APIs simply perform a no-op.

    You can verify this by calling CloseHandle (or any if the other CloseXXX APIs) with NULL. To check INVALID_HANDLE_VALUE, call CloseHandle on a valid handle twice (as on the 2nd close, the handle will be INVALID_HANDLE_VALUE).

    Not a big deal, but not having to do those checks keeps the code cleaner.

  4. #4
    Join Date
    May 2019
    Posts
    53

    Re: [RESOLVED] EnumServicesStatusEx and Process Walking - How to allocate and free me

    The code is still triggering a range of Exception Errors if it is run more than once during the same execution...

    When I close the window that outputs the data from GetProcessNext() and EnumServicesStatusEx() I'm trying to free ALL structs/memory associated with those functions.

    When I re-open that window it should re-execute the code to populate those structs/heap memory again for a fresh, updated set of Process information and Services information.

    However, the second or subsequent times it is run it is VERY prone to triggering Exceptions all over the place (though not necessarily always).

    Any idea what is causing this? I'm using VS2017 in Debug Mode.

    Also, @Arjay, if I don't check for Invalid or NULL handles then Close Handle returns an Exception Error 'Invalid Handle'. That's also in VS2017 in Debug Mode. That's why I'm checking for Invalid/empty handles...

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] EnumServicesStatusEx and Process Walking - How to allocate and free me

    Set the handle to NULL after closing it.

    If you cant run this code more than once, it probably means something isn't getting cleaned up (as you know). To this end, my approach is to leverage C++ and wrap the needed functionality inside a class and perform cleanup in the class destructor (with all variables initialized in the ctor).

Tags for this Thread

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