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