Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I query the OS before using LocalFree/HeapFree, from FormatMessage, with Windows 7/8.1 and Windows 10?

Tags:

c

windows

winapi

From MSDN (FormatMessage function):

Windows 10:

LocalFree is not in the modern SDK, so it cannot be used to free the result buffer. Instead, use HeapFree (GetProcessHeap(), allocatedMessage). In this case, this is the same as calling LocalFree on memory.

I don't know how to use HeapFree, but I wanna run the app in Windows 7, Windows 8/8.1 and Windows 10, but I am currently using just LocalFree and It is working.

This is my code:

VOID ShowErrorMsg(DWORD messageId)
{
    DWORD flags=FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS;
    DWORD languageID = LANG_USER_DEFAULT;
    LPWSTR errorMsg;
    DWORD size = 0;

    FormatMessageW(flags, NULL, messageId, languageID, (LPTSTR)&errorMsg, size, NULL);

    wprintf(L"\n%s", errorMsg);

    LocalFree(errorMsg);

}

My question is, should I use functions like IsWindows7SP1OrGreater, IsWindows8Point1OrGreater and IsWindows10OrGreater, from Version Helper functions, before calling LocalFree or HeapFree?

If I have to call HeapFree, can anyone give me an example on how to use it?

Thanks!

like image 544
Sergio Calderon Avatar asked Dec 08 '25 22:12

Sergio Calderon


1 Answers

"Modern" here means WinRT/Metro/Modern/Store application. LocalFree exists on every version of Windows and can be used by Classic/Desktop applications on all Windows versions.

like image 185
Anders Avatar answered Dec 10 '25 11:12

Anders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!