4 Ocak 2015 Pazar

FarProc

/****************************************************************************
 * Program Name : Gokmen Setup Bootstrapper
 * Program Ver. : v1.2.0.0
 * Module Name  : Setup.exe
 * Last Edition : 08.04.2013
 * Optimization : No code analysis issues were detected.
 ****************************************************************************/
#include <windows.h>
#include <strsafe.h>

typedef void( WINAPI *SYSINF )( LPSYSTEM_INFO );
#define BUFFERSIZE 260

int main()
{

   
    //ek bilbi : bilgisayar adını Load library ile almak
HMODULE szMod = LoadLibraryA("kernel32.dll");     // dll için adres
    FARPROC szAdd = GetProcAddress(szMod, "GetComputerNameA");  // fnk adresi al



    char szTmp[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwSize = sizeof(szTmp);
    ((BOOL(WINAPI*)(LPSTR, LPDWORD))szAdd)(szTmp, &dwSize); //szadd GetComputerName şekline getir. (aldıgı parametreler dönüş tipi v.s. )
    MessageBoxA(0, szTmp, 0, 0); //
 //   return ERROR_SUCCESS;







SYSTEM_INFO sysInfo; //system bilgilerini tut
WIN32_FIND_DATA ffd;     //dosya bilgileri.. unicode derlenirse arkada WIN32_FIND_DATAW olarak tanımlanır.
HANDLE hFind;   //dosya için handle
HINSTANCE hInstance;  //bir modüle yapılan handle... örnek bir exe yüklenınce onu tanımak içindir
SYSINF sysINF=NULL;
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" ); //yüklenen dll ye artık hInstance kullanarak eriş...
FARPROC fp;
if ( hInstance != NULL )                  // loading DLL failed
fp=GetProcAddress( hInstance , //module adı (handle (hinstance))
                                   "GetNativeSystemInfo" // buraya fnk ya da değişken ismi gelebilir...
 );

((void( WINAPI *)( LPSYSTEM_INFO ))fp)(&sysInfo);


//eğer GetProcAdress basarılı olursa, geriye çıkarılan fnk ya da değişkenın adresini döndürür
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 ); //dosya bulunamadı ise...
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 );   // EXE yani("setup.exe") ara ve bilgilerini ffd içine at

if ( hFind == INVALID_HANDLE_VALUE )
hFind = FindFirstFile( MSI, &ffd ); // "setup.exe" yoksa "setup.msi" yi ara

if ( hFind == INVALID_HANDLE_VALUE ) { //"setup.msi" de yoksa hatayı ver ve çık...
wprintf( L"ERROR (%d) : Installation program not found!..\n", GetLastError() );
return 3;
}

#ifdef _DEBUG
wprintf( L"Found \"%s\". Starting installation...\n", ffd.cFileName ); //işlem basarılı. bulunan dosyayı ekrana bas.
#endif




hInstance = ShellExecute( NULL, L"open", ffd.cFileName, NULL, myPath, SW_MAXIMIZE );     //bulunan setup.msi yada exe yi aç
                                                                                      //ekran modu SW_NORMAL olsun
                                                                                     //SW_MAXIMIZE desek tam ekran açılır

if ( ( int ) hInstance < 32 ) {
switch ( ( int ) hInstance ) {//shellexecute hata mesajı varsa ekrana bas ve çık ...
case 0                      :
MsgError = L"Insufficient Memory/Resources";
break;

case ERROR_FILE_NOT_FOUND   : //dosya bulunamad
MsgError = L"File not found";
break;

case ERROR_PATH_NOT_FOUND   : //yol bulunamadı
MsgError = L"Path not found";
break;

case ERROR_BAD_FORMAT :      //format hatası.: örneğin x86 için x64 program...
MsgError = L"Invalid file format";
break;

case SE_ERR_ACCESSDENIED    : //erişim engellendi OS tarafından
MsgError = L"Access denied";
break;

case SE_ERR_ASSOCINCOMPLETE : //dosya adı eksik veya gecersiz
MsgError = L"Invalid file association";
break;

case SE_ERR_DDEBUSY         :  //DDE = paylaşılan hafızanın uygulamalar arası veri paylaşımında kullanılması
MsgError = L"DDE transaction not completed"; //DDE tamamlanamadı
break;

case SE_ERR_DDEFAIL         :
MsgError = L"DDE transaction failed"; //DDE
break;

case SE_ERR_DDETIMEOUT      :  //DDE için yapılan request time out aldı. tamamlanamadı
MsgError = L"DDE transaction timed out";
break;

case SE_ERR_DLLNOTFOUND     : //sözkonusu dll bulunamadı
MsgError = L"Specified DLL not found";
break;

case SE_ERR_NOASSOC         :
MsgError = L"No application associated";
break;

case SE_ERR_OOM             : //hafıza yetersiz...
MsgError = L"Not enough memory";
break;

case SE_ERR_SHARE           : //paylaşım ihlali oluştu.
MsgError = L"Sharing violation";
break;
}

wprintf( L"ERROR (%d): %s!..\n", GetLastError(), MsgError ); //bulunan hatayı ekrana bas.
}
else
wprintf( L"Succeeded. Exiting!..." ); // hata yok. setup.exe veya msi çalıştır....

return 0;
}

Hiç yorum yok:

Yorum Gönder