Modify the registry and run the program automatically

Source: Internet
Author: User
Create a string-type sub-key under the Registry branch HKEY_LOCAL_MACHINE // software // Microsoft // windows // CurrentVersion // run. Do not repeat the name.

The full path of the program you want to run at startup. This will automatically run at startup.
Hkey;
Cstring keytree =/"software // Microsoft /// windows // CurrentVersion /// run/"; // master Region
Cstring mainkey =/"killwindow/"; // subkey
Cstring newdata =/"name of the file to be started/"; // New Value of the subkey
Long ret0 = (: regopenkeyex (HKEY_LOCAL_MACHINE, keytree, 0, key_write, & hkey ));
If (ret0! = Error_success)
{
Afxmessagebox (/"error /");
Return;
}
Lpbyte DATA = cstringtolpbyte (newdata );
DWORD datasize = newdata. getlength () + 1;
DWORD type = REG_SZ;
Long ret1 =: regsetvalueex (hkey, mainkey, null, type, Data, datasize );
If (ret1! = Error_success)
{
Afxmessagebox (/"Set automatic running error! /");
Return;
}
Delete data;
: Regclosekey (hkey );

Why do I use C language to write the registry? The C language does not have such a function!
Int createrun (void) // modify the registry and automatically run the program
{
Hkey;
Char winexe [256]; // path of the execution File
Char xepath [256];
Int I;
Char * pbuf;
Pbuf = getcommandline ();

Strcpy (winexe, pbuf + 1 );
For (I = (INT) strlen (winexe); I> = 0; I --)
{
If (winexe= /'/"/')
{
Winexe= 0;
I =-10000;
}
}

// Get running path
Strcpy (exepath, winexe );
For (I = (INT) strlen (winexe); I> = 0; I --)
{
If (winexe= /'/////')
{
Exepath [I + 1] = 0;
I =-10000;
}
}

If (regopenkeyex (HKEY_LOCAL_MACHINE,/"software // Microsoft // windows /// CurrentVersion /// run /"
, 0, key_write, & hkey )! = Error_success)
Return-1;
If (regsetvalueex (hkey,/"five32/", 0, REG_SZ, (const unsigned char *) winexe, strlen (pval) + 1 )! = Error_success)
{
Regclosekey (hkey );
Return-1;
}
Regclosekey (hkey );
Return 0;
}
 

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.