Windows Programing
#include "stdafx.h"
#include "pwrapi.h"
#include "PwrDrv.h"
// power down and power up flags
BOOL g_bPoweredDown = FALSE;
BOOL g_bPoweredUp = FALSE;
// messaging related variables
HANDLE g_hMessageThread = NULL; // messaging thread handle
DWORD g_dwMessageThreadId = 0; // messaging thread id
HWND g_hMsgWnd = NULL; // messaging HWND
BOOL g_bMsgSent = FALSE; // flags that message was sent
CRITICAL_SECTION g_csMsg; // critical section used for changing the
messaging HWND
// messaging working thread function
DWORD WINAPI MessageThreadProc(LPVOID lpParameter);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////
//
// Driver Interface Functions
//
/////////////////////////////////////////////////////////////////
//----------------------------------------------------------
//
// XXX_Init
// This function is called by the Device Manager to
// initialize a device.
//
// Notes:
// Normally this function returns a handle to the device
// context created. For our purposes, this function doesn't
// need to do anything, so I just return TRUE for a device
// handle.
//
PWRDRV_API DWORD PWR_Init(DWORD dwContext)
{
// initialize the critical section (used for changing the messaging
HWND)
InitializeCriticalSection(&g_csMsg);
// create messaging working thread
if (g_hMessageThread==NULL)
{
g_hMessageThread = CreateThread(NULL, 0, MessageThreadProc, NULL, 0,
&g_dwMessageThreadId);
}
return TRUE; // return non-zero value
}
//----------------------------------------------------------
//
// XXX_Deinit
//
// This function is called by the Device Manager to
// de-initialize a device.
//
// Notes:
// For our purposes, this function doesn't need to
// do anything, so just return TRUE.
//
PWRDRV_API BOOL PWR_Deinit(DWORD hDeviceContext)
{
// delete message working thread
if (g_hMessageThread!=NULL) {
TerminateThread(g_hMessageThread, 0);
}
// delete the critical section (used for changing the messaging HWND)
DeleteCriticalSection(&g_csMsg);
return TRUE;
}
//----------------------------------------------------------
//
// XXX_Open
//
// This function opens a device for reading and/or writing.
// An application indirectly invokes this function when it
// calls CreateFile to open special device file names.
//
// Notes:
// Normally, this function returns a handle that identifies
// the open context of the device to the calling application.
// Since we are running for a single application and we don't
// need to keep any information with this handle, we just
// an arbitrary value.
//
PWRDRV_API DWORD PWR_Open(DWORD hDeviceConext, DWORD dwAccessCode,
DWORD dwShareMode)
{
return 1; // return non-zero value
}
//----------------------------------------------------------
//
// XXX_Close
//
// This function closes the device context.
//
// Notes:
// For our purposes, this function doesn't need to
// do anything, so just return TRUE.
//
PWRDRV_API BOOL PWR_Close(DWORD hOpenContext)
{
return TRUE;
}
//----------------------------------------------------------
//
// XXX_IOControl
//
// This function sends a command to a device.
//
// This is where commands from the application are handled.
// These commands include:
// * IOCTL_PWR_GET_POWERUP_STATE
// Returns the current power up notification value
// * IOCTL_PWR_RESET_POWERUP_STATE
// Resets the power up notification value
// * IOCTL_PWR_ENABLE_POWERUP_MESSAGE
// Enables the power up notification messaging, by
// receiving an HWND to pass the message to
// * IOCTL_PWR_DISABLE_POWERUP_MESSAGE
// Disables the power up notification messaging
//
// Remarks (from MSDN documentation)
// An application uses the DeviceIOControl function to
// specify an operation to be performed. The operating
// system, in turn, invokes this function. The dwCode
// parameter contains the input or output operation to
// be performed. I/O control codes usually are specific
// to each device driver and typically are exposed to
// programmers by means of a header file.
//
PWRDRV_API BOOL PWR_IOControl(
DWORD hOpenContext, // open context handle
DWORD dwCode, // I/O control code
PBYTE pBufIn, // input buffer
DWORD dwLenIn, // input buffer size
PBYTE pBufOut, // output buffer
DWORD dwLenOut, // output buffer size
PDWORD pdwActualOut) // actual output bytes returned
{
BOOL bRet = FALSE;
PDWORD pDatIn = (PDWORD) pBufIn;
PDWORD pDatOut = (PDWORD) pBufOut;
switch (dwCode) {
case IOCTL_PWR_GET_POWERUP_STATE:
// return power up notification value in the output buffer
*pDatOut = g_bPoweredUp;
*pdwActualOut = sizeof(g_bPoweredUp);
bRet = TRUE;
break;
case IOCTL_PWR_RESET_POWERUP_STATE:
// reset the power up notification value
g_bPoweredUp = FALSE;
bRet = TRUE;
break;
case IOCTL_PWR_ENABLE_POWERUP_MESSAGE:
// enable the power up notification messaging by getting the
messaging HWND
// in the input buffer
g_hMsgWnd = (HWND) *pDatIn;
bRet = TRUE;
break;
case IOCTL_PWR_DISABLE_POWERUP_MESSAGE:
// disable the power up notification messaging
EnterCriticalSection(&g_csMsg);
{
g_hMsgWnd = NULL;
}
LeaveCriticalSection(&g_csMsg);
bRet = TRUE;
break;
}
return bRet;
}
//----------------------------------------------------------
//
// XXX_PowerUp
//
// This function normally restores power to a device. In
// our case, we just flag the notification so that the
// messaging working thread can handle sending notification
// to the application window.
//
// Remarks (from MSDN documentation)
// The operating system invokes this function to restore
// power to a device. The operating system might call this
// function as it is leaving its power-save mode. This
// function should not call any functions that may cause
// it to block, and it should return as quickly as possible.
// This function should set a global variable to indicate
// that power was restored and perform any necessary processing
// later.
//
PWRDRV_API void PWR_PowerUp(DWORD hDeviceContext)
{
if (g_bPoweredDown)
g_bPoweredUp = TRUE; // set notification
}
//----------------------------------------------------------
//
// XXX_PowerDown
//
// This function normally suspends power to the device. In
// our case, we just flag the notification so that when we
// get the XXX_PowerUp call, we have a pair of Down/Up calls
// and know that a valid power up occurred.
//
// Remarks (from MSDN documentation)
// The operating system invokes this function to suspend
// power to the device. The operating system might call
// this function when it is about to enter the power-save
// mode. This function should not call any functions that
// might cause it to block, and it should return as quickly
// as possible. One strategy for returning quickly is to
// have this function set a global variable to indicate
// that a power loss occurred and to defer any complicated
// processing until later, when the driver=F2s XXX_PowerUp
// function is called.
//
PWRDRV_API void PWR_PowerDown(DWORD hDeviceContext)
{
// reset notification flags
g_bPoweredDown = TRUE;
g_bPoweredUp = FALSE;
g_bMsgSent = FALSE;
}
//----------------------------------------------------------
//
// XXX_Read
//
// This function reads data from the device identified by
// the open context.
//
// Notes:
// This functionality is not supported, so return error.
//
PWRDRV_API DWORD PWR_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD
dwCount)
{
// Unused function, return error
return -1;
}
//----------------------------------------------------------
//
// XXX_Write
//
// This function writes data to the device.
//
// Notes:
// This functionality is not supported, so return error.
//
PWRDRV_API DWORD PWR_Write(DWORD hOpenContext, LPCVOID pSourceBytes,
DWORD dwNumberOfBytes)
{
// Unused function, return error
return -1;
}
//----------------------------------------------------------
//
// XXX_Seek
//
// This function moves the data pointer in the device.
//
// Notes:
// This functionality is not supported, so return error.
//
PWRDRV_API DWORD PWR_Seek(DWORD hOpenContext, long lAmount, WORD
wType)
{
// Unused function, return error
return -1;
}
//----------------------------------------------------------
//
// Messaging working thread function
//
// Polls the power up detection flag, and if set, sends a
// power on notification message to the application window.
//
DWORD WINAPI MessageThreadProc(LPVOID lpParameter)
{
DWORD dwSleepTime = 1000; // need in milliseconds
while (1)
{
if (g_bPoweredUp && !g_bMsgSent)
{
g_bMsgSent = TRUE;
EnterCriticalSection(&g_csMsg);
if (g_hMsgWnd!=NULL)
{
PostMessage(g_hMsgWnd, WM_PWR_POWERUP, 0, 0);
}
LeaveCriticalSection(&g_csMsg);
}
// wait some time before polling again
Sleep(dwSleepTime);
}
return 0;
}
this is code for my prj in tnis code i am getting error as
Error 1 error C2491: 'PWR_Init' : definition of dllimport function not
allowed
i looked for msdn but doesnt help me much