3 Ocak 2015 Cumartesi

VSc++ Load Library



#include <windows.h>
#include <strsafe.h>              // for StringCchPrintf function

/*************************************************************************
 * Global type and variable definitions
 ************************************************************************/
// Define a new type in the same structure of GetSystemInfo API function
typedef VOID ( WINAPI *SYSINF )( LPSYSTEM_INFO );
// Specify the maximum size of buffer
#define BUF_SIZE 256
//----End of Type Definitions --------------------------------------------

/*************************************************************************
 * Error handling function which display error in a MessageBox
 ************************************************************************/
void ShowError( TCHAR *errFunc )
{
TCHAR    Message[BUF_SIZE] = L"";  // Message will be displayed
TCHAR    *msgStatic;               // Static Error string
LPCTSTR  fmtString;                // Error string structure
LPVOID   msgBuffer;                // Temporary buffer for error description
DWORD    errNo;                    // Last System Error Number

errNo    = GetLastError();         // The last error number of this thread.

/**********************************************************************
 * Message string formatting Functions. This will get the error
 * description text defined by GetLastError() in localized language
 * and assign this text to reference of msgBuffer variable.
 *********************************************************************/
msgStatic = L"Program hatalı bir işlem sonucu durduruldu!...";
fmtString = L"%s\n\nYordam : %s\nKod: %d\nAçıklama : %s";

if ( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errNo,
0,
( LPTSTR ) &msgBuffer,
0,
NULL
)
  )
/*****************************************************************
* Prepare a formatted Message to display in the MessageBox.
****************************************************************/
StringCchPrintf( Message,    // Message String
BUF_SIZE,   // Max size of Message string
fmtString,  // Format String of Message
msgStatic,  // Argument 1 |
errFunc,    // Argument 2 |
errNo,      // Argument 3 |-> of Format String
msgBuffer   // Argument 4 |
  );

/**********************************************************************
* Show a message box which display the formatted error description
*********************************************************************/
MessageBox( NULL,
Message,            // Message that will be displayed
L"Hatalı İşlem",        // Caption of Message Box
MB_OK | MB_ICONERROR    // Buttons, Icons and sounds
 );
}
//----End of ShowError Function ------------------------------------------

/*************************************************************************
 * Main program function
 ************************************************************************/
int main()
{
HINSTANCE hInstance;
SYSINF sysINF;
SYSTEM_INFO si;
TCHAR cName[BUF_SIZE];
TCHAR uName[BUF_SIZE];
LPCTSTR txt;
TCHAR Info[BUF_SIZE];
DWORD chCount = BUF_SIZE;
TCHAR *cpuArch;

txt = L"Bilgisayar: %s\nKullanıcı: %s\nİşlemci: %d\nMimari: %s";

chCount = BUF_SIZE;
GetComputerName( cName, &chCount );
chCount = BUF_SIZE;
GetUserName( uName, &chCount );

hInstance = LoadLibrary( L"kernel32.dll" );

if ( hInstance == NULL ) {                 // loading DLL failed
ShowError( L"LoadLibrary() : Loading DLL is failed" );
return 1;                             // Exit with error.
}

/**********************************************************************
* "GetNativeSystemInfo" function in "kernel32.dll" is only Windows 7
* and later Operating systems. If this function call fails we must use
* the "GetSystemInfo" API function.
*********************************************************************/
sysINF = ( SYSINF ) GetProcAddress( hInstance , "GetNativeSystemInfo" );

if ( NULL == sysINF ) {                  // Getting DLL's function address failed
ShowError( L"GetProcAddress() : Getting function address is failed." );
GetSystemInfo( &si );                 // not found, use GetSystemInfo API function
}
else
sysINF( &si );                        // found, use this DLL function

switch ( si.wProcessorArchitecture ) {
case PROCESSOR_ARCHITECTURE_AMD64 :
cpuArch = L"64-Bit";
break;

case PROCESSOR_ARCHITECTURE_IA64  :
cpuArch = L"64-Bit";
break;

case PROCESSOR_ARCHITECTURE_INTEL :
cpuArch = L"32-Bit";
break;
}

StringCchPrintf(  Info,                    // Information String
 BUF_SIZE,                // Max size of Info string
 txt,                     // Format String of Info
 cName,                   // Arg 1 |
 uName,                   // Arg 2 |
 si.dwNumberOfProcessors, // Arg 3 |-> of Format String
 cpuArch                  // Arg 4 |
  );

MessageBox( NULL,
( LPCTSTR ) Info,              // Message that will be displayed
L"Sistem Bilgisi",             // Caption of Message Box
MB_OK | MB_ICONINFORMATION     // Buttons, Icons and Sounds
 );

return 0;
}

Hiç yorum yok:

Yorum Gönder