1) Ne znam odakle mi ovaj kod i odakle sam ga skinuo
ali evo ga pa vidi sta ces sa njim:
Code:
#include <windows.h>
#include <stdio.h>
//
// Make up some private access rights.
//
#define ACCESS_READ 1
#define ACCESS_WRITE 2
bool TAntivariOdbcManager::IsAdmin()
{
HANDLE hToken;
DWORD dwStatus;
DWORD dwAccessMask;
DWORD dwAccessDesired;
DWORD dwACLSize;
DWORD dwStructureSize = sizeof ( PRIVILEGE_SET );
PACL pACL = NULL;
PSID psidAdmin = NULL;
BOOL bReturn = FALSE;
PRIVILEGE_SET ps;
GENERIC_MAPPING GenericMapping;
PSECURITY_DESCRIPTOR psdAdmin = NULL;
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
__try
{
// AccessCheck() requires an impersonation token.
ImpersonateSelf( SecurityImpersonation );
if ( !OpenThreadToken ( GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken ) )
{
if ( GetLastError() != ERROR_NO_TOKEN )
throw; // __leave;
// If the thread does not have an access token, we'll
// examine the access token associated with the process.
if ( !OpenProcessToken ( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
throw; // __leave;
}
if ( !AllocateAndInitializeSid ( &SystemSidAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &psidAdmin ) )
throw; // __leave;
psdAdmin = LocalAlloc ( LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH );
if ( psdAdmin == NULL )
throw; // __leave;
if ( !InitializeSecurityDescriptor ( psdAdmin, SECURITY_DESCRIPTOR_REVISION ) )
throw; // __leave;
// Compute size needed for the ACL.
dwACLSize = sizeof ( ACL ) + sizeof ( ACCESS_ALLOWED_ACE ) +
GetLengthSid ( psidAdmin ) - sizeof ( DWORD );
// Allocate memory for ACL.
pACL = (PACL)LocalAlloc( LPTR, dwACLSize );
if (pACL == NULL)
throw; // __leave;
// Initialize the new ACL.
if ( !InitializeAcl( pACL, dwACLSize, ACL_REVISION2 ) )
throw; // __leave;
dwAccessMask = ACCESS_READ | ACCESS_WRITE;
// Add the access-allowed ACE to the DACL.
if ( !AddAccessAllowedAce ( pACL, ACL_REVISION2, dwAccessMask, psidAdmin ) )
throw; // __leave;
// Set our DACL to the SD.
if ( !SetSecurityDescriptorDacl( psdAdmin, TRUE, pACL, FALSE ) )
throw; // __leave;
// AccessCheck is sensitive about what is in the SD; set
// the group and owner.
SetSecurityDescriptorGroup( psdAdmin, psidAdmin, FALSE );
SetSecurityDescriptorOwner( psdAdmin, psidAdmin, FALSE );
if ( !IsValidSecurityDescriptor( psdAdmin ) )
throw; // __leave;
dwAccessDesired = ACCESS_READ;
//
// Initialize GenericMapping structure even though we
// won't be using generic rights.
//
GenericMapping.GenericRead = ACCESS_READ;
GenericMapping.GenericWrite = ACCESS_WRITE;
GenericMapping.GenericExecute = 0;
GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
if ( !AccessCheck( psdAdmin, hToken, dwAccessDesired,
&GenericMapping, &ps, &dwStructureSize, &dwStatus,
&bReturn ) )
{
//printf( "AccessCheck() failed with error %lu\n", GetLastError() );
throw; // __leave;
}
RevertToSelf();
}
__finally
{
// Cleanup
if ( pACL ) LocalFree( pACL );
if ( psdAdmin ) LocalFree ( psdAdmin );
if ( psidAdmin ) FreeSid ( psidAdmin );
}
return bReturn;
}
2) Za ovo ti treba (uglavnom poseban DLL). Najbolje da pogledas na
netu. Imas puno resenja - garantovano. Ovo ja koristim za neke svoje
programe (Borland C++ Builder):
--- DLL.CPP ---
Code:
/************************************************************/
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#define WM_KEYHOOK (WM_USER + 1002)
HHOOK ghhookKB;
HINSTANCE ghInst;
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void *lpReserved)
{
ghInst = hinst;
return ( 1 );
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) __stdcall void SetHook(void);
extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
//---------------------------------------------------------------------------
void __stdcall SetHook( void )
{
HOOKPROC lpfnHookProc = NULL;
lpfnHookProc = GetProcAddress( GetModuleHandle( "altfhook.dll" ),"CheckKey" );
ghhookKB = SetWindowsHookEx( WH_KEYBOARD, lpfnHookProc, ghInst, NULL );
}
void __stdcall RemoveHook( void )
{
UnhookWindowsHookEx( ghhookKB );
}
DWORD __stdcall CheckKey( int nCode, WORD wParam, LONG lParam )
{
HWND ghAppWnd = FindWindow( "TDiMainForm", 0 );
if ( ( nCode < 0 ) || ( nCode == HC_NOREMOVE ) )
return CallNextHookEx( ghhookKB, nCode, wParam, lParam );
// Skip if it's a repeat
if ( lParam & 0x40000000 )
return CallNextHookEx( ghhookKB, nCode, wParam, lParam );
// Check if Alt is pressed
if ( !( lParam & ( 1<<29 ) ) )
return CallNextHookEx( ghhookKB, nCode, wParam, lParam );
// Send key information to the main window
SendMessage( ghAppWnd, WM_KEYHOOK, 0, lParam );
return CallNextHookEx( ghhookKB, nCode, wParam, lParam );
}
/************************************************************/
--- PROGRAM.H ---
Code:
// ...
#define WM_KEYHOOK (WM_USER + 1002)
// ...
MESSAGE void __fastcall KeyHook(TMessage &Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_KEYHOOK, TMessage, KeyHook);
END_MESSAGE_MAP(TForm)
--- PROGRAM.CPP ---
Code:
// ...
extern "C" __declspec(dllexport) __stdcall void SetHook(void);
extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
// ...
__fastcall TDiMainForm::TDiMainForm(TComponent* Owner)
: TForm(Owner)
{
SetHook();
}
__fastcall TDiMainForm::~TDiMainForm()
{
RemoveHook();
}
// ...
void __fastcall TDiMainForm::KeyHook( TMessage &Message )
{
char HKey[80];
GetKeyNameText( Message.LParam, HKey, 80 );
if ( AnsiString( HKey ) == "F1" )
DoSomething(); // Alt + F1
}
Nadam se da nisam nesto zaboravio.