Post: C++ System Shutdown
11-06-2011, 05:11 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I'm using Visual Studio 2010 ultimate, and I got this code right off MSDN.
It's supposed to check for permission to shut down the local computer and then shut it down:
    #include "stdafx.h"
#include <Windows.h>

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "advapi32.lib")

BOOL MySystemShutdown( LPTSTR lpMsg )
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure

BOOL fResult; // system shutdown flag

// Get the current process token handle so we can get shutdown
// privilege.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;

// Get the LUID for shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Display the shutdown dialog box and start the countdown.

fResult = InitiateSystemShutdown(
NULL, // shut down local computer
lpMsg, // message for user
30, // time-out period, in seconds
FALSE, // ask user to close apps
TRUE); // reboot after shutdown

if (!fResult)
return FALSE;

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

return TRUE;
}


The code seems fine, but when I build the solution, I get these errors:

Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\Owner\Documents\Projects\Hypster\LIBCMTD.lib(wincrt0.obj) Hypster

Error 2 error LNK1120: 1 unresolved externals C:\Users\Owner\Documents\Projects\Hypster\Debug\Hypster.exe Hypster

The program "Hypster" is supposed to shut down the computer on execution. Any ideas on what I should do?
11-06-2011, 09:01 PM #2
Pichu
RIP PICHU.
Originally posted by s0vietstr1ke View Post
I'm using Visual Studio 2010 ultimate, and I got this code right off MSDN.
It's supposed to check for permission to shut down the local computer and then shut it down:
    #include "stdafx.h"
#include <Windows.h>

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "advapi32.lib")

BOOL MySystemShutdown( LPTSTR lpMsg )
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure

BOOL fResult; // system shutdown flag

// Get the current process token handle so we can get shutdown
// privilege.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;

// Get the LUID for shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Display the shutdown dialog box and start the countdown.

fResult = InitiateSystemShutdown(
NULL, // shut down local computer
lpMsg, // message for user
30, // time-out period, in seconds
FALSE, // ask user to close apps
TRUE); // reboot after shutdown

if (!fResult)
return FALSE;

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

return TRUE;
}


The code seems fine, but when I build the solution, I get these errors:

Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\Owner\Documents\Projects\Hypster\LIBCMTD.lib(wincrt0.obj) Hypster

Error 2 error LNK1120: 1 unresolved externals C:\Users\Owner\Documents\Projects\Hypster\Debug\Hypster.exe Hypster

The program "Hypster" is supposed to shut down the computer on execution. Any ideas on what I should do?


Did you just Copy and Paste the code into your project? MSDN gives you a good idea as to how it would function but never the copy and paste.
11-08-2011, 06:40 AM #3
Originally posted by s0vietstr1ke View Post
I'm using Visual Studio 2010 ultimate, and I got this code right off MSDN.
It's supposed to check for permission to shut down the local computer and then shut it down:
    #include "stdafx.h"
#include <Windows.h>

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "advapi32.lib")

BOOL MySystemShutdown( LPTSTR lpMsg )
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure

BOOL fResult; // system shutdown flag

// Get the current process token handle so we can get shutdown
// privilege.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;

// Get the LUID for shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Display the shutdown dialog box and start the countdown.

fResult = InitiateSystemShutdown(
NULL, // shut down local computer
lpMsg, // message for user
30, // time-out period, in seconds
FALSE, // ask user to close apps
TRUE); // reboot after shutdown

if (!fResult)
return FALSE;

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

return TRUE;
}


The code seems fine, but when I build the solution, I get these errors:

Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\Owner\Documents\Projects\Hypster\LIBCMTD.lib(wincrt0.obj) Hypster

Error 2 error LNK1120: 1 unresolved externals C:\Users\Owner\Documents\Projects\Hypster\Debug\Hypster.exe Hypster

The program "Hypster" is supposed to shut down the computer on execution. Any ideas on what I should do?


may i ask why you didn't just use system("shutdown");?

or are you trying to do something else.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo