Solved

Hibernate PC

Posted on 2004-08-06
5
658 Views
Last Modified: 2008-01-09
I have been asked at work to develop something that will take computer off the network if a virus is detected. Good idea I though. I suggested the idea of just taking the machine off the network by disabling the network card (s) in the machine but they don’t want me to do that. They want the machine to be shutdown.

After much argument I have managed to get them to let me hibernate the machine, so that people don’t loose what they are working on (i.e. Developers loosing code, testers loosing test runs - that sort of thing). My question is this - How the hell do I hibernate a machine from C++, I can quite easily shut the bugger down but I have no idea how to hibernate it. Any ideas?

You help is much appreciated
0
Comment
Question by:ice_nin
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11733735
Which OS???

if it is windows, you shall use the SetSuspendState API available in powrprof.dll (Declared in Powrprof.h). Also make sure that, Hiberation is enabled in Windows (Control-Panel->Power Options->hibernate tab).

The prototype of SetSuspendState would be like,

BOOLEAN SetSuspendState(
  BOOL Hibernate,
  BOOL ForceCritical,
  BOOL DisableWakeEvent
);


You shall refer to MSDN for more info about each parameters (althou obvious).
0
 

Author Comment

by:ice_nin
ID: 11751229
I have included
#include <Powrprof.h> and added Powrprof.lib to the project and when i try and compile the application i get the following.
I have read the MSDN docs and they only mention the 2 files i have referenced.
Any ideas?

 error LNK2019: unresolved external symbol "unsigned char __stdcall SetSuspendState(unsigned char,unsigned char,unsigned char)" (?SetSuspendState@@YGEEEE@Z) referenced in function "void __cdecl HibernatePC(void)" (?HibernatePC@@YAXXZ)
0
 
LVL 1

Accepted Solution

by:
ageraldnaveen earned 125 total points
ID: 11751365
the linker error says "SetSuspendState" takes unsigned char as arg??? i doubt if you are using wrong header file..!! which is different from the import lib "powrprof.lib"

use shall try something like this..

typedef BOOL (*HibernateFn)(BOOL,BOOL,BOOL);

void myfunc()
{
      HMODULE hDLL = LoadLibrary("powrprof.dll");
      HibernateFn myfunc = (HibernateFn)GetProcAddress(hDLL,"SetSuspendState");
      myfunc(TRUE,FALSE,FALSE);
}

hope that helps..
0
 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11751386
correction in my prev. comment:

it shoud be

typedef BOOLEAN (*HibernateFn)(BOOLEAN,BOOLEAN,BOOLEAN);

sorry.
0
 

Author Comment

by:ice_nin
ID: 11751723
I have just looking into this error in more detail, i have found documents released by microsoft saying that they know about this and its an issue when compiling a C++ source file as opposed to a C source file.

They have no fix date for this but have given a temp fix for this

You need to edit the powrprof.h and add the following

//To the top of the File
#ifdef __cplusplus
extern "C" {
#endif

//To the bottom of the file
#ifdef __cplusplus
}  // extern "C"
#endif

Thanks for the help... I will assign you the points because the information was helpful
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SendMessage fails while PostMessage works 2 114
Lambda for random numbers problem 7 113
How can i compile this github project?? 2 85
Issues with C++ Class 19 92
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question