Method for XP or earlier is slightly different from Vista/Win7 from my experience.
For XP or earlier, return 0 for WM_QUERYENDSESSION and this should be enough.
For Vista/Win7, I haven't found a way to entirely stop logging off. But temporarily blocking logging off is possible. I would return 1 for WM_QUERYENDSESSION and register a blocking reason during WM_ENDSESSION, by calling ShutdownBlockReasonCreate. Then call ShutdownBlockReasonDestroy when your application is finished running.