Solved

Hibernate PC

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

708 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

15 Experts available now in Live!

Get 1:1 Help Now