Solved

Hibernate PC

Posted on 2004-08-06
5
650 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now