Implementation of the program startup from Windows by writing the registry

Source: Internet
Author: User

The program can read the configuration in the Config.ini file in the same directory to enable boot.

The Config.ini file format instance is as follows:

[Main]
KeyName = Test
Processpath = D:\bin\test.exe


Where the KeyName field is the table key name written to the registry (can be set to the program name)

Processpath the full path to the program


The complete source code for the program is as follows.

The Regedit function implements the registry write, and the autopen implements the boot from boot.

The compiled program is downloaded here: Program Download

#include <stdio.h> #include <windows.h>int regedit (HKEY key, const char* reg_name, const char* key_name, const  char* key_value) {HKEY hkresult; int Ret=regopenkeyex (key, Reg_name, 0, Key_all_access, &hkresult), if (ret! = 0) return Ret;ret=regsetvalueex (  Hkresult, Key_name, 0, REG_EXPAND_SZ, (CONST byte*) Key_value, + if (ret==0) {RegCloseKey (hkresult); return 0;} else{return ret;}} int autopen (const char* key_name, const char* process_path) {char reg_name[] = "software\\microsoft\\windows\\ Currentversion\\run "; return regedit (HKEY_LOCAL_MACHINE, Reg_name, Key_name, Process_path);} int main (void) {char Key_name[100];char process_path[1000]; Getprivateprofilestringa ("Main", "KeyName", "Test", Key_name, sizeof (Key_name), ". \\config.ini"); Getprivateprofilestringa ("Main", "Processpath", "Test", Process_path, sizeof (Process_path), ". \\config.ini"); int ret = Autopen (Key_name, Process_path), if (ret = = 0) printf ("Write to register succeed\n"); elseprintf ("Write to register failed \ n ");System ("pause");  return 0;  }



Related Article

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.