SetupAtlas.c

Go to the documentation of this file.
00001 /*************************************************************************************************/
00002 /* Allan CORNET */
00003 /* INRIA 2006   */
00004 /*************************************************************************************************/
00005 
00006 #include <windows.h>
00007 #include <stdio.h>
00008 
00009 #define KeyCpuIdentifer "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"
00010 #define LenLine 255
00011 #define LogName "SetupAtlas.log"
00012 #define LineMaxSize 512
00013 #define MaxCPU 64
00014 #define DefaultDllName "ATLAS_PIII.DLL"
00015 
00016 char ArrayLinesCPU[MaxCPU][LineMaxSize]; /* 64 cpus max in atlas.spec*/
00017 int NbrLinesCPU=0;
00018 
00019 char *GetWhereIsThisExe(void);
00020 /*************************************************************************************************/
00021 BOOL AppendMessageToLog(char *Message)
00022 {
00023         BOOL bOK=FALSE;
00024         FILE *pFile;
00025         char *Path=NULL;
00026         char PathAndFilename[MAX_PATH];
00027 
00028         Path=GetWhereIsThisExe();
00029         wsprintf(PathAndFilename,"%s%s",Path,LogName);
00030         free(Path);
00031         Path=NULL;
00032 
00033         pFile=fopen(PathAndFilename,"at");
00034         if (pFile)
00035         {
00036                 fprintf(pFile,"%s\n",Message);
00037                 fclose(pFile);
00038                 bOK=TRUE;
00039         }
00040 
00041         return bOK;
00042 }
00043 /*************************************************************************************************/
00044 char * GetRegKeyIdentifier(void)
00045 {
00046         HKEY key;
00047         DWORD result;
00048         char *LineIdentifier;
00049         ULONG length = LenLine,Type;
00050         
00051         result=RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyCpuIdentifer, 0, KEY_QUERY_VALUE , &key);
00052 
00053         LineIdentifier=(char*)malloc(sizeof(char)*length);
00054 
00055         if ( RegQueryValueEx(key, "Identifier", 0, &Type, (LPBYTE)LineIdentifier, &length) !=  ERROR_SUCCESS )
00056         {
00057                 wsprintf(LineIdentifier,"ERROR");
00058         }
00059 
00060         if( Type != REG_SZ )
00061     {
00062        wsprintf(LineIdentifier,"ERROR");
00063     }
00064 
00065         if ( result == ERROR_SUCCESS ) RegCloseKey(key);
00066         
00067         return (char *)LineIdentifier;
00068 }
00069 /*************************************************************************************************/
00070 char *GetRegKeyVendorIdentifier(void)
00071 {
00072         HKEY key;
00073         DWORD result;
00074         ULONG length = LenLine,Type;
00075 
00076         char *LineVendorIdentifier;
00077 
00078         result=RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyCpuIdentifer, 0, KEY_QUERY_VALUE , &key);
00079 
00080         LineVendorIdentifier=(char*)malloc(sizeof(char)*length);
00081 
00082         if ( RegQueryValueEx(key, "VendorIdentifier", 0, &Type, (LPBYTE)LineVendorIdentifier, &length) !=  ERROR_SUCCESS )
00083         {
00084                 wsprintf(LineVendorIdentifier,"ERROR");
00085         }
00086 
00087         if( Type != REG_SZ )
00088     {
00089        wsprintf(LineVendorIdentifier,"ERROR");
00090     }
00091 
00092         if ( result == ERROR_SUCCESS ) RegCloseKey(key);
00093         
00094         return (char *)LineVendorIdentifier;
00095 }
00096 /*************************************************************************************************/
00097 char * GetWhereIsThisExe(void)
00098 {
00099         LPSTR tail;
00100         char *fullfilename=NULL;
00101         fullfilename=(char*)malloc(sizeof(char)*MAX_PATH);
00102 
00103         GetModuleFileName(GetModuleHandle(NULL),fullfilename,MAX_PATH);
00104 
00105         if ((tail = strrchr (fullfilename, '\\')) != (LPSTR) NULL)
00106     {
00107       tail++;
00108       *tail = '\0';
00109     }
00110         
00111         return (char*)fullfilename;
00112 }
00113 /*************************************************************************************************/
00114 BOOL FileExist(char *filename)
00115 {
00116      BOOL retour=FALSE; 
00117         
00118      WIN32_FIND_DATA FindFileData;
00119      HANDLE handle = FindFirstFile (filename, &FindFileData);
00120      if (handle != INVALID_HANDLE_VALUE)
00121      {
00122          FindClose (handle);
00123          retour=TRUE;
00124      }
00125      else retour=FALSE;
00126 
00127      return retour;
00128 }
00129 /*************************************************************************************************/
00130 BOOL LoadArrayCPUFromFile(char *path)
00131 {
00132         #define ATLASSPEC "atlas.spec"
00133         BOOL vOK=FALSE;
00134         char *fullfilename=NULL;
00135     FILE *pFile;
00136 
00137     fullfilename=(char*)malloc(sizeof(char)*(strlen(path)+strlen(ATLASSPEC)+2));
00138         wsprintf(fullfilename,"%s\\%s",path,ATLASSPEC);
00139         if (FileExist(fullfilename))
00140         {
00141                 char Line[LineMaxSize];
00142                 int nbrLines=0;
00143                 pFile=fopen(fullfilename,"rt");
00144                 while (fgets(Line, LineMaxSize,pFile))
00145                 {
00146                         if (nbrLines >= MaxCPU)
00147                         {
00148                                 AppendMessageToLog("See LoadArrayCPUFromFile function in SetupAtlas.c");
00149                                 return FALSE;
00150                         }
00151                         Line[strlen(Line)-1]='\0';
00152                         wsprintf(ArrayLinesCPU[nbrLines],"%s",Line);
00153                         nbrLines++;
00154                         NbrLinesCPU=nbrLines;
00155                 }
00156                 fclose(pFile);
00157                 vOK=TRUE;
00158         }
00159 
00160         free(fullfilename);
00161         fullfilename=NULL;
00162         
00163         return vOK;
00164 
00165 }
00166 /*************************************************************************************************/
00167 char *GetLocalCPU(void)
00168 {
00169         char *CPUString=NULL;
00170         char *Vendor=NULL;
00171         char *Identifier=NULL;
00172 
00173         Identifier=GetRegKeyIdentifier();
00174         Vendor=GetRegKeyVendorIdentifier();
00175         CPUString=(char *)malloc(sizeof(char)*LineMaxSize);
00176 
00177         if ( (strcmp(Identifier,"ERROR")!=0) && (strcmp(Vendor,"ERROR")!=0) )
00178         {
00179                 wsprintf(CPUString,"%s %s",Vendor,Identifier);
00180         }
00181         else wsprintf(CPUString,"ERROR");
00182         
00183         return (char*)CPUString;
00184 }
00185 /*************************************************************************************************/
00186 BOOL GetInfoCPUfromLine(char * line,BOOL FromFile,char *Vendor,int *Family,int *Model,int *Step,char *DLLName)
00187 {
00188         BOOL vOK=FALSE;
00189         
00190         char Architecture[32];
00191         char wFamily[32];
00192         char wModel[32];
00193         char wStep[32];
00194         char wRemark[32];
00195 
00196         if (FromFile)
00197         {
00198                 char F[32],M[32];
00199                 
00200                 sscanf(line,"%s %s %s %s %s %s %s",Vendor,wFamily,F,wModel,M,DLLName,wRemark);
00201 
00202                 if (strcmp(F,"*")==0)
00203                 {
00204                         *Family=-1;
00205                 }
00206                 else *Family=(int)strtol(F, (char **)NULL, 10);
00207 
00208                 if (strcmp(M,"*")==0)
00209                 {
00210                         *Model=-1;
00211                 }
00212                 else *Model=(int)strtol(M, (char **)NULL, 10);
00213                 
00214                 *Step=-1;
00215                 
00216                 if ( (strcmp(wFamily,"Family")==0) &&
00217                          (strcmp(wModel,"Model")==0) )
00218                 {
00219                         vOK=TRUE;
00220                 }
00221         }
00222         else
00223         {
00224                 char F[32],M[32],S[32];
00225 
00226                 sscanf(line,"%s %s %s %s %s %s %s %s",
00227                                  Vendor,Architecture,wFamily,F,wModel,M,wStep,S);
00228 
00229                 *Family=(int)strtol(F, (char **)NULL, 10);
00230                 *Model=(int)strtol(M, (char **)NULL, 10);
00231                 *Step=(int)strtol(S, (char **)NULL, 10);
00232 
00233                 if ( (strcmp(wFamily,"Family")==0) &&
00234                          (strcmp(wModel,"Model")==0) &&
00235                          (strcmp(wStep,"Stepping")==0) )
00236                 {
00237                         vOK=TRUE;
00238                 }
00239                 wsprintf(DLLName,"EMPTY");
00240         }
00241         
00242         return vOK;
00243 }
00244 /*************************************************************************************************/
00245 char *GetDLLFilenameAtlas(void)
00246 {
00247         #define PathAtlas  "Atlas" 
00248         char *PathOfThisExe=NULL;
00249         char *FilenameAtlasDLL=NULL;
00250         BOOL FindCPU=FALSE;
00251 
00252         FilenameAtlasDLL=malloc(sizeof(char)*MAX_PATH);
00253         wsprintf(FilenameAtlasDLL,"%s",DefaultDllName);
00254 
00255         PathOfThisExe=GetWhereIsThisExe();
00256 
00257         if ( LoadArrayCPUFromFile(PathOfThisExe) )
00258         {
00259                 char *LocalCPU=NULL;
00260                 char LocalVendor[32];
00261                 char LocalFilenameNotUse[32];
00262                 int LocalFamily,LocalModel,LocalStep;
00263                 
00264                 LocalCPU=GetLocalCPU();
00265                 AppendMessageToLog(LocalCPU);
00266                 GetInfoCPUfromLine(LocalCPU,FALSE,LocalVendor,&LocalFamily,&LocalModel,&LocalStep,LocalFilenameNotUse);
00267         if ( strcmp(LocalCPU,"ERROR")!=0 )
00268                 {
00269                         int i=0;
00270                         for (i=0;i<NbrLinesCPU;i++)
00271                         {
00272                                 int F,M,S;
00273                                 char Vendor[32];
00274                                 char Filename[MAX_PATH];
00275 
00276                                 if ( GetInfoCPUfromLine(ArrayLinesCPU[i],TRUE,Vendor,&F,&M,&S,Filename) )
00277                                 {
00278                                         if ( (!FindCPU) && (strcmp(LocalVendor,Vendor)==0) )
00279                                         {
00280                                                 if ( LocalFamily == F )
00281                                                 {
00282                                                         if ( (LocalModel == M) || (M == -1) || (LocalModel == -1) )
00283                                                         {
00284                                                                 FindCPU=TRUE;
00285                                                                 wsprintf(FilenameAtlasDLL,"%s",Filename);
00286                                                                 AppendMessageToLog(ArrayLinesCPU[i]);
00287                                                         }
00288                                                 }
00289                                         }
00290                                 }
00291                                 else
00292                                 {
00293                                         AppendMessageToLog("Error in Atlas.spec");
00294                                 }
00295                         }
00296                 }
00297                 else
00298                 {
00299                         AppendMessageToLog("Error Detection CPU from Registry key.");
00300                 }
00301                 free(LocalCPU);
00302                 LocalCPU=NULL;
00303         }
00304         
00305         if (!FindCPU)
00306         {
00307                 AppendMessageToLog("Use default Atlas library for Pentium");
00308         }
00309 
00310         free(PathOfThisExe);
00311         PathOfThisExe=NULL;
00312 
00313         return (char*)FilenameAtlasDLL;
00314 }
00315 /*************************************************************************************************/
00316 BOOL CopyAtlas(char *AtlasSourceFilename)
00317 {
00318         #define PathAtlas         "Atlas"
00319         #define AtlasFilename "atlas.dll"
00320         BOOL bOK=FALSE;
00321         char *PathOfThisExe=NULL;
00322         char *FullDestinationFilenameAtlas=NULL;
00323         char *FullSourceFilenameAtlas=NULL;
00324 
00325         PathOfThisExe=GetWhereIsThisExe();
00326 
00327         FullSourceFilenameAtlas=malloc( sizeof(char) * (strlen(PathOfThisExe)+strlen(PathAtlas)+strlen(AtlasSourceFilename)+2) );
00328         wsprintf(FullSourceFilenameAtlas,"%s%s\\%s",PathOfThisExe,PathAtlas,AtlasSourceFilename);
00329         AppendMessageToLog(FullSourceFilenameAtlas);
00330 
00331         FullDestinationFilenameAtlas=malloc( sizeof(char) * (strlen(PathOfThisExe)+strlen(AtlasSourceFilename)+2) );
00332         wsprintf(FullDestinationFilenameAtlas,"%s%s",PathOfThisExe,AtlasFilename);
00333         AppendMessageToLog(FullDestinationFilenameAtlas);
00334 
00335         if ( FileExist(FullSourceFilenameAtlas) )
00336         {
00337                 if ( !CopyFile(FullSourceFilenameAtlas,FullDestinationFilenameAtlas,FALSE) )
00338                 {
00339                         char msg[512];
00340 
00341                         wsprintf(msg,"Impossible to copy %s.",FullDestinationFilenameAtlas);
00342                         // MessageBox(NULL,msg,"Error",MB_ICONWARNING|MB_OK);
00343                         AppendMessageToLog(msg);
00344                 }
00345                 else bOK=TRUE;
00346         }
00347         else
00348         {
00349                 char msg[512];
00350                 wsprintf(msg,"File %s not found.",FullSourceFilenameAtlas);
00351                 // MessageBox(NULL,msg,"Error",MB_ICONWARNING|MB_OK);
00352                 AppendMessageToLog(msg);
00353         }
00354 
00355         if (FullDestinationFilenameAtlas)
00356         {
00357                 free(FullDestinationFilenameAtlas);
00358                 FullDestinationFilenameAtlas=NULL;
00359         }
00360 
00361         if (FullSourceFilenameAtlas)
00362         {
00363                 free(FullSourceFilenameAtlas);
00364                 FullSourceFilenameAtlas=NULL;
00365         }
00366 
00367         free(PathOfThisExe);
00368         PathOfThisExe=NULL;
00369 
00370         return bOK;
00371 }
00372 /*************************************************************************************************/
00373 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine, int iCmdShow)
00374 {
00375         char *DLLFilename=NULL;
00376 
00377         AppendMessageToLog("**************************************************");
00378         DLLFilename=GetDLLFilenameAtlas();
00379         AppendMessageToLog(DLLFilename);
00380         if ( CopyAtlas(DLLFilename) ) AppendMessageToLog("Copy file OK");
00381         else AppendMessageToLog("Copy file Not OK");
00382         AppendMessageToLog("**************************************************");
00383 
00384         free(DLLFilename);
00385         DLLFilename=NULL;
00386         return 0;
00387 }
00388 /*************************************************************************************************/

Generated on Sun Mar 4 15:04:01 2007 for Scilab [trunk] by  doxygen 1.5.1