休眠与开机自动运行等VC代码

//根据文件句柄,获取文件名

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

#include <string.h>

#include <psapi.h>

 

#define BUFSIZE 512

 

BOOL GetFileNameFromHandle(HANDLE hFile) 

{

  BOOL bSuccess = FALSE;

  TCHAR pszFilename[MAX_PATH+1];

  HANDLE hFileMap;

 

  // Get the file size.

  DWORD dwFileSizeHi = 0;

  DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi); 

 

  if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )

  {

     printf("Cannot map a file with a length of zero.\n");

     return FALSE;

  }

 

  // Create a file mapping object.

  hFileMap = CreateFileMapping(hFile, 

                    NULL, 

                    PAGE_READONLY,

                    0, 

                    1,

                    NULL);

 

  if (hFileMap) 

  {

    // Create a file mapping to get the file name.

    void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);

 

    if (pMem) 

    {

      if (GetMappedFileName (GetCurrentProcess(), 

                             pMem, 

                             pszFilename,

                             MAX_PATH)) 

      {

 

        // Translate path with device name to drive letters.

        TCHAR szTemp[BUFSIZE];

        szTemp[0] = '\0';

 

        if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) 

        {

          TCHAR szName[MAX_PATH];

          TCHAR szDrive[3] = TEXT(" :");

          BOOL bFound = FALSE;

          TCHAR* p = szTemp;

 

          do 

          {

            // Copy the drive letter to the template string

            *szDrive = *p;

 

            // Look up each device name

            if (QueryDosDevice(szDrive, szName, BUFSIZE))

            {

              UINT uNameLen = _tcslen(szName);

 

              if (uNameLen < MAX_PATH) 

              {

                bFound = _tcsnicmp(pszFilename, szName, 

                    uNameLen) == 0;

 

                if (bFound) 

                {

                  // Reconstruct pszFilename using szTemp

                  // Replace device path with DOS path

                  TCHAR szTempFile[MAX_PATH];

                  _stprintf(szTempFile,

                            TEXT("%s%s"),

                            szDrive,

                            pszFilename+uNameLen);

                  _tcsncpy(pszFilename, szTempFile, MAX_PATH);

                }

              }

            }

 

            // Go to the next NULL character.

            while (*p++);

          } while (!bFound && *p); // end of string

        }

      }

      bSuccess = TRUE;

      UnmapViewOfFile(pMem);

    } 

 

    CloseHandle(hFileMap);

  }

  printf("File name is %s\n", pszFilename);

  return(bSuccess);

}

开机自动运行

  其中strPath参数表示要设置为自运行的程序的绝对路径。当设置成功时返回true,否则返回false

BOOL SetAutoRun(CString strPath)//开机自动运行

{

   CString str;

   HKEY hRegKey;

   BOOL bResult;

   str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");

   if(RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS) 

       bResult=FALSE;

   else

   {

       _splitpath(strPath.GetBuffer(0),NULL,NULL,str.GetBufferSetLength(MAX_PATH+1),NULL);

       strPath.ReleaseBuffer();

       str.ReleaseBuffer();

       if(::RegSetValueEx( hRegKey,

                           str,

                           0,

                           REG_SZ,

                           (CONST BYTE *)strPath.GetBuffer(0),

                           strPath.GetLength() ) != ERROR_SUCCESS)

          bResult=FALSE;

       else

          bResult=TRUE;

       strPath.ReleaseBuffer();

   }

   return bResult;

}        

 

使计算机休眠

void XiuMian() 

{

 if(MessageBox("确实要休眠吗?","关机程序",MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES)

 {

  HANDLE hToken;

  TOKEN_PRIVILEGES tp;

  LUID luid;

  if(::OpenProcessToken(GetCurrentProcess(),

         TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,

         &hToken))

  {

   ::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);

   tp.PrivilegeCount=1;

   tp.Privileges[0].Luid =luid;

   tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;

   ::AdjustTokenPrivileges(hToken,false,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);

  }

  ::SetSystemPowerState(false,true); 

 }

}

 

posted @ 2015-01-23 11:32  廖先生  阅读(504)  评论(0编辑  收藏  举报