The program of recording the time of the switch machine with vc.net

Source: Internet
Author: User
Tags bool implement

Although computers are now slashing prices, many people are still in the same computer, especially students. Sometimes I have to know when someone is using the computer because of some kind of need. To find one on the Internet? But not necessarily meet their own requirements, as well as carry forward the DIY spirit, do one yourself. Then use Vc.net to DIY a bar.

Programming idea: Record boot time is easier, just want to let the program with the system start, record a boot time, shutdown record shutdown time on it.

Before we begin, we need to understand the process of writing this little program, and let's take it one step at a time:

Realize power-on self-running

Implement Runtime Auto Hide

Implementation can be hot key exhale

Implement record time

Record time when shutdown is implemented

1, to achieve the power-on self-operation

I often see on the CSDN site that someone asked such a question, in fact, to realize when the boot automatically run their own program is not difficult. You can see a series of key values under the registry's HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key, which are the path of the software that is automatically running on the boot. So what we have to do is to programmatically implement the path of our program also added to this key value, it's done. The famous "Windows Optimization Master" is to remove the unnecessary boot running software This method to achieve the boot speed optimization.

Now that you want to read and write to the registry, you need two important functions to manipulate the registry: RegOpenKey () and RegSetValueEx (). The former is used to open the key of the registry, the latter sets the key value for the open key, and for the specific parameters of these two API functions, see MSDN. For code reuse, I encapsulate a special function for this, as follows:

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;
}

Where the strpath parameter represents the absolute path of the program to be set to run from. Returns True when the setting succeeds, otherwise returns false.

Here's another question: How do you get it if you need an absolute path to this program? Can not specify a value bar, then when the path changes in this program will have to modify the program, too much trouble. You can use this encapsulated function to implement:

Get the path to the program file itself (including the filename)

CString GetMyPath()
{
  CString strPath;
  GetModuleFileName(NULL,strPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
  strPath.ReleaseBuffer();
  return strPath;
}

where GetModuleFileName () is an API function that gets the path. This function encapsulates this API function in order to simplify the purpose of the invocation.

When you execute this function, return to the absolute path of this program, including the file name of this program.

OK, get the path to this program and load it into the registry, and the next time the system starts, our program will automatically start.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.