C language program boot from the start

  • 2020-05-07 20:12:47
  • OfStack

The program is relatively simple, using C language to obtain its own path and system path, modify the registry key, some functions do not understand can see MSDN


#include<stdio.h>
#include<windows.h>

char *GetFilename(char *p) // get 1 The path of the pure file name 
{
  int x=strlen(p);
  char ch='\\';
  char *q=strrchr(p,ch);
  return q;
}

int main()
{
  char *filepath;
  char modlepath[256];
  char syspath[256];

  // Open the cool dog app 
  filepath="C:\\Program^ Files\\KuGou\\KGMusic\\KuGou.exe"; 
  system(filepath);

  // Move the program to the system directory 

  GetModuleFileName(0,modlepath,256); // Get its own path 
  GetSystemDirectory(syspath,256); // Get the system path 

  int ret=CopyFile(modlepath,strcat(syspath,GetFilename(modlepath)),1);// To copy, CopyFile The first 2 Three parameters are the target file name 
  if(ret)
  {
    printf("%s has been copyed to sys dir %s\n",modlepath,syspath);
  }
  else
  {
    printf("%s is exists",modlepath);
  }

  // Program add boot from start 
  char regname[]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
  HKEY hKey;
  ret=RegOpenKey(HKEY_LOCAL_MACHINE,regname,&hKey);    // Open the registry key 
  ret=RegSetValueEx(hKey,"MyProm",0,REG_EXPAND_SZ,(unsigned char*)strcat(syspath,GetFilename(modlepath)),25); // Set the key value 

  if(ret==0)
  {
    printf("succes to write run key.\n");
    RegCloseKey(hKey);
  }
  else
  {
    printf("failed to open regedit.%d\n",ret);
    return 0;
  }
  
  return 0;
}


Related articles: