1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
BOOL UpdateRegKey()
{
//si tu ajoutes ce code a l'app elle meme utilise ce bloc(detection automatique)
////////////////////////////////////////////////////////////////////////
//Get File name
char *csAppPath = new char[MAX_PATH];
memset(csAppPath,0,MAX_PATH);
if(!::GetModuleFileName(0, csAppPath, MAX_PATH))
{
delete csAppPath;
DWORD err = GetLastError();
char toto[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,0,toto,255,NULL);
AfxMessageBox(toto);
return FALSE;
}
if(!csAppPath)
return FALSE;
/////////////////////////////////////////////////////////////////////////////////
//sinon tu dois passer a la fonction le chemin de l'application
////////////////////////////////////////////////////////////////////////////////
char *parent = ParentDirectory(csAppPath);//fonction ke j'ai faite pour trouver le parent d'un fichier ou dossier
if(!parent)
{
MyDelete(csAppPath);
return FALSE;
}
strAppPath = _T(parent);
delete parent;
SetCurrentDirectory(csAppPath);
//test if the app has been already registred
HKEY hRegKey = NULL;
BYTE *value=new BYTE[500];
DWORD lenght = 0;
DWORD error = 0;
TCHAR szKey[256];
DWORD flag=(DWORD)REG_SZ;
_tcscpy(szKey, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"));
if ((error=RegOpenKeyEx(HKEY_LOCAL_MACHINE,szKey,0,KEY_QUERY_VALUE,&hRegKey))!= ERROR_SUCCESS)
{
char *toto = new char[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,error,0,toto,255,NULL);
delete toto;
delete value;
}
else
{
if((error=RegQueryValueEx(hRegKey,APP_NAME,0,&flag,value,&lenght))!=ERROR_SUCCESS)
{
char *toto = new char[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,error,0,toto,255,NULL);
delete toto;
delete value;
}
}
if(value && !strcmp((char*)value,csAppPath))
{
delete csAppPath;
delete value;
return TRUE;
}
delete value;
//register app
if (RegOpenKey(HKEY_LOCAL_MACHINE, szKey, &hRegKey) != ERROR_SUCCESS)
{
DWORD err = GetLastError();
char toto[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,0,toto,255,NULL);
delete csAppPath;
return FALSE;
}
else
{
if(::RegSetValueEx(hRegKey,APP_NAME,0,(DWORD)REG_SZ,(BYTE*)csAppPath,strlen(csAppPath))!=ERROR_SUCCESS)
{
DWORD err = GetLastError();
char toto[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,err,0,toto,255,NULL);
delete csAppPath;
return FALSE;
}
}
delete csAppPath;
return TRUE;
}
char* ParentDirectory(char *pathname)
{
if (!pathname)
return NULL;
char *ret = NULL;
unsigned int i = strlen(pathname) - 1;
if ( pathname[strlen(pathname) - 1] == '\\' )
i--;
for ( ; i != 0; i--)
{
if (pathname[i] == '\\')
break;
}
ret = new char[strlen(pathname) +1];
if ( i != 0)
{
unsigned int j;
for ( j = 0; j < i ; j++ )
ret[j] = pathname[j];
ret[j] = '\0';
}
return ret;
} |
Partager