#include <windows.h>
#include <strsafe.h>
typedef void( WINAPI *SYSINF )( LPSYSTEM_INFO );
#define BUFFERSIZE 260
int main()
{
SYSTEM_INFO sysInfo;
WIN32_FIND_DATA ffd;
HANDLE hFind;
HINSTANCE hInstance;
SYSINF sysINF;
TCHAR myPath[MAX_PATH];
TCHAR EXE[MAX_PATH] = L"setup.exe";
TCHAR MSI[MAX_PATH] = L"setup.msi";
TCHAR *cpuArch, *subDir, *MsgError;
#ifdef _DEBUG
wprintf( L"Checking System Architecture... : " );
#endif
hInstance = LoadLibrary( L"kernel32.dll" );
if ( hInstance != NULL ) // loading DLL failed
sysINF = ( SYSINF ) GetProcAddress( hInstance , "GetNativeSystemInfo" );
if ( NULL != sysINF )
sysINF( &sysInfo ); // found, use this DLL function
else
GetSystemInfo( &sysInfo );
switch ( sysInfo.wProcessorArchitecture ) {
case PROCESSOR_ARCHITECTURE_INTEL:
{ cpuArch = L"32-Bit"; subDir = L"x86"; };
break;
case PROCESSOR_ARCHITECTURE_AMD64:
{ cpuArch = L"64-Bit"; subDir = L"x64"; };
break;
}
#ifdef _DEBUG
wprintf( L"%s System\n", cpuArch );
#endif
GetCurrentDirectory( MAX_PATH, myPath );
StringCchCat( myPath, BUFFERSIZE, L"\\" );
StringCchCat( myPath, BUFFERSIZE, subDir );
hFind = FindFirstFile( myPath, &ffd );
if ( hFind != INVALID_HANDLE_VALUE ) {
FindClose( hFind );
SetCurrentDirectory( myPath );
#ifdef _DEBUG
wprintf( L"Looking for installation program in \"%s\" sub-directory...\n", subDir );
#endif
}
else {
wprintf( L"ERROR (%d): Installation sub-directory not found!..\n", GetLastError() );
exit( EXIT_FAILURE );
}
GetCurrentDirectory( MAX_PATH, myPath );
hFind = FindFirstFile( EXE, &ffd );
if ( hFind == INVALID_HANDLE_VALUE )
hFind = FindFirstFile( MSI, &ffd );
if ( hFind == INVALID_HANDLE_VALUE ) {
wprintf( L"ERROR (%d) : Installation program not found!..\n", GetLastError() );
return 3;
}
#ifdef _DEBUG
wprintf( L"Found \"%s\". Starting installation...\n", ffd.cFileName );
#endif
hInstance = ShellExecute( NULL, L"open", ffd.cFileName, NULL, myPath, SW_HIDE );
if ( ( int ) hInstance < 32 ) {
switch ( ( int ) hInstance ) {
case 0 :
MsgError = L"Insufficient Memory/Resources";
break;
case ERROR_FILE_NOT_FOUND :
MsgError = L"File not found";
break;
case ERROR_PATH_NOT_FOUND :
MsgError = L"Path not found";
break;
case ERROR_BAD_FORMAT :
MsgError = L"Invalid file format";
break;
case SE_ERR_ACCESSDENIED :
MsgError = L"Access denied";
break;
case SE_ERR_ASSOCINCOMPLETE :
MsgError = L"Invalid file association";
break;
case SE_ERR_DDEBUSY :
MsgError = L"DDE transaction not completed";
break;
case SE_ERR_DDEFAIL :
MsgError = L"DDE transaction failed";
break;
case SE_ERR_DDETIMEOUT :
MsgError = L"DDE transaction timed out";
break;
case SE_ERR_DLLNOTFOUND :
MsgError = L"Specified DLL not found";
break;
case SE_ERR_NOASSOC :
MsgError = L"No application associated";
break;
case SE_ERR_OOM :
MsgError = L"Not enough memory";
break;
case SE_ERR_SHARE :
MsgError = L"Sharing violation";
break;
}
wprintf( L"ERROR (%d): %s!..\n", GetLastError(), MsgError );
}
else
wprintf( L"Succeeded. Exiting!..." );
return 0;
}
Hiç yorum yok:
Yorum Gönder