'program crashes on calling RmShutdown()

I'm experiencing a strange issue with the Restart Manager API.

I'm trying to debug a runtime error that is causing my program to crash in the middle of execution.

I have created a logging system that writes a simple log text file during the course of execution. I tried to debug the program, but it doesn't appear in my system, and the program crashes on calling RmShutdown().

This function crashes on specific files, and sometimes it doesn't appear. For example, most times it appears on C:\Users\Administrator\ntuser.dat.LOG1. I know I shouldn't kill a system process, but it's a test when the crash happens. Does killing a process that has a handle of file make my program crash?

What could possibly be wrong here? Why would RmShutdown() fail?

BOOL KillFileOwner(__in LPCWSTR PathName)
{
    BOOL Result = FALSE;
    DWORD dwSession = 0xFFFFFFFF; // Invalid handle value
    DWORD ret = 0;
    WCHAR szSessionKey[CCH_RM_SESSION_KEY + 1] = { 0 };

    if (RmStartSession(&dwSession, 0, szSessionKey) == ERROR_SUCCESS)
    {
        if (RmRegisterResources(dwSession, 1, &PathName, 0, NULL, 0, NULL) == ERROR_SUCCESS)
        {
            DWORD dwReason = 0x0;
            UINT nProcInfoNeeded = 0;
            UINT nProcInfo = 0;
            PRM_PROCESS_INFO ProcessInfo = NULL;
            // RtlSecureZeroMemory(&ProcessInfo, sizeof(ProcessInfo));

            ret = (DWORD)RmGetList(dwSession, &nProcInfoNeeded, &nProcInfo, NULL, &dwReason);
            if (ret != ERROR_MORE_DATA || !nProcInfoNeeded) {

                RmEndSession(dwSession);
                return FALSE;
            }

            ProcessInfo = (PRM_PROCESS_INFO)calloc(nProcInfoNeeded, sizeof(RM_PROCESS_INFO));
            if (!ProcessInfo) {
                RmEndSession(dwSession);
                return FALSE;
            }

            nProcInfo = nProcInfoNeeded;
            ret = (DWORD)RmGetList(dwSession, &nProcInfoNeeded, &nProcInfo, ProcessInfo, &dwReason);
            if (ret != ERROR_SUCCESS || !nProcInfoNeeded) {

                free(ProcessInfo);
                RmEndSession(dwSession);
                return FALSE;
            }

            DWORD ProcessId = (DWORD)GetProcessId(GetCurrentProcess());
            if (!ProcessId) {

                free(ProcessInfo);
                RmEndSession(dwSession);
                return FALSE;
            }

            for (UINT i = 0; i < nProcInfo; i++) {

                if (ProcessInfo[i].Process.dwProcessId == ProcessId) {

                    free(ProcessInfo);
                    RmEndSession(dwSession);
                    return FALSE;
                }
            }

            Result = (RmShutdown(dwSession, RmForceShutdown, NULL) == ERROR_SUCCESS);
            free(ProcessInfo);
        }

        RmEndSession(dwSession);
    }

    return Result;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source