예제 #1
0
파일: Process.cpp 프로젝트: caidongyun/libs
/** 
 * 
 * Returns owner of a process i.e. name of user who created this process
 * 
 * @param       hProcess_i - Process handle
 * @param       csOwner_o  - Owner of process
 * @return      bool       - Returns execution status
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool Process::ExtractProcessOwner( HANDLE hProcess_i, 
                                   CString& csOwner_o )
{
    // Get process token
    HANDLE hProcessToken = NULL;
    if ( !::OpenProcessToken( hProcess_i, TOKEN_READ, &hProcessToken ) || !hProcessToken )
    {
        return false;
    }

    // Auto closes token on destruction
    Utils::AutoHandleMgr ahmTokenHandleMgr( hProcessToken );
    
    // Obtain the size of the user information in the token.
    DWORD dwProcessTokenInfoAllocSize = 0;
    ::GetTokenInformation(hProcessToken, TokenUser, NULL, 0, &dwProcessTokenInfoAllocSize);
    
    // Call should have failed due to zero-length buffer.
    if( ::GetLastError() == ERROR_INSUFFICIENT_BUFFER )
    {
        // Allocate buffer for user information in the token.
        PTOKEN_USER pUserToken = RCAST( PTOKEN_USER, new BYTE[dwProcessTokenInfoAllocSize] );
        if (pUserToken != NULL)
        {
            // Delete ptr
            Utils::AutoDeleter<BYTE, true> adTokenBuffDeletor( RCAST( LPBYTE*, &pUserToken ));

            // Retrieve the user information from the token.
            if (::GetTokenInformation( hProcessToken, TokenUser, pUserToken, dwProcessTokenInfoAllocSize, &dwProcessTokenInfoAllocSize ))
            {
                SID_NAME_USE   snuSIDNameUse;
                TCHAR          szUser[MAX_PATH] = { 0 };
                DWORD          dwUserNameLength = MAX_PATH;
                TCHAR          szDomain[MAX_PATH] = { 0 };
                DWORD          dwDomainNameLength = MAX_PATH;

                // Retrieve user name and domain name based on user's SID.
                if ( ::LookupAccountSid( NULL, 
                                         pUserToken->User.Sid, 
                                         szUser, 
                                         &dwUserNameLength, 
                                         szDomain, 
                                         &dwDomainNameLength, 
                                         &snuSIDNameUse ))
                {
                    // User name string
                    csOwner_o = _T("\\\\");
                    csOwner_o += szDomain;
                    csOwner_o += _T("\\");
                    csOwner_o += szUser;

                    // We succeeded
                    return true;
                }//End if
            }// End if
        }// End if
예제 #2
0
파일: Settings.cpp 프로젝트: Mateuus/devsrc
void Settings::SaveBinary( LPCTSTR lpctszSection_i, 
                           LPCTSTR lpctszEntry_i, 
                           int* pColumnOrder_i, 
                           const unsigned int uCount_i )
{
    WriteBinary( lpctszSection_i, 
                 lpctszEntry_i, 
                 RCAST( LPBYTE, pColumnOrder_i ), 
                 uCount_i * sizeof( int ));
}
예제 #3
0
//************************************
// Method:    GetRegKeyStringValue
// FullName:  CAutoStartupInfo::GetRegKeyStringValue
// Access:    private
// Returns:   bool
// Qualifier: const
// Parameter: const HKEY hKey_i
// Parameter: LPCTSTR lpctszSubKeyName_i
// Parameter: CString & csValue_o
//************************************
bool CAutoStartupInfo::GetRegKeyStringValue( const HKEY hKey_i, LPCTSTR lpctszSubKeyName_i, CString& csValue_o ) const
{
    if( !hKey_i || !lpctszSubKeyName_i )
    {
        return false;
    }

    DWORD dwKeyType = 0;

    DWORD dwKeyValueBuffByteLen = MAX_PATH * sizeof( TCHAR );
    TCHAR szKeyValue[MAX_PATH] = { 0 };

    // Get key value
    const long lRes = RegQueryValueEx( hKey_i,
                                       lpctszSubKeyName_i,
                                       0,
                                       &dwKeyType,
                                       RCAST( LPBYTE, szKeyValue ),
                                       &dwKeyValueBuffByteLen );


    // Check return value
    if( lRes != ERROR_SUCCESS )
    {
        return false;
    }

    // Check key type
    if( dwKeyType != REG_SZ && dwKeyType != REG_EXPAND_SZ )
    {
        return false;
    }

    if( dwKeyValueBuffByteLen != 0 )
    {
        if(( dwKeyValueBuffByteLen % sizeof( TCHAR ) != 0 ) ||
                ( szKeyValue[dwKeyValueBuffByteLen / sizeof(TCHAR) - 1] != 0 ))
        {
            return false;
        }
        else
        {
            csValue_o = szKeyValue;
        }// End if
    }// End if

    return true;
}//End GetRegKeyStringValue
예제 #4
0
파일: Window.cpp 프로젝트: caidongyun/libs
/** 
 * 
 * Extracts details pertaining to a window
 * 
 * @param       WindowObj - Window data
 * @return      void
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
void Window::ExtractWindowDetails( const HWND hWnd_i )
{
    // Set window handle
    SetHandle( hWnd_i );

    // Set thread id of window
    SetThreadId( ::GetWindowThreadProcessId( GetHandle(), 0 ));

    // Get class name of window
    TCHAR szBuffer[MAX_PATH] = { 0 };
    ::GetClassName( GetHandle(), szBuffer, MAX_PATH );
    GetClassName() = szBuffer;

    GetClass().cbSize = sizeof( GetClass() );
    GetClassInfoEx( AfxGetInstanceHandle(), szBuffer, &GetClass() );

    // Get window text if any
    InternalGetWindowText( GetHandle(), GetTitle().GetBufferSetLength( MAX_PATH ), MAX_PATH );
    GetTitle().ReleaseBuffer();

    // Get normal style
    SetStyle( GetWindowLong( GetHandle(), GWL_STYLE ));

    // Get extended style
    SetStyleEx( GetWindowLong( GetHandle(), GWL_EXSTYLE ));

    // Get window id
    SetId( GetWindowLong( GetHandle(), GWL_ID ));

    // Get parent window
    SetParentHandle( RCAST( HWND, GetWindowLong( GetHandle(), GWL_HWNDPARENT )));

    // Window state i.e. window is maximized, minimized or restored
    GetStateAsString( GetState() );

    // For style parsing
    RetrieveWndStyles();

    // Window bounds
    CRect crBounds;
    GetWindowRect( GetHandle(), &crBounds );
    if( crBounds.Width() || crBounds.Height() )
    {
        GetBounds().Format( _T( "L:%d T:%d R:%d B:%d" ), crBounds.left, 
                                                         crBounds.top, 
                                                         crBounds.right, 
                                                         crBounds.bottom );
    }// End if

    // Retrieves unicode support status for windows
    SetUnicode( IsWindowUnicode( GetHandle() ));

    // Get window icon
    DWORD dwResult = 0;
    Utils::SndMsgTimeOutHelper( GetHandle(), 
                                WM_GETICON, 
                                ICON_SMALL, 
                                0,
                                dwResult );
    // Get window icon
    SetIcon( RCAST( HICON, dwResult ));

    // Get enabled status of window
    SetEnabled( IsWindowEnabled( GetHandle() ));
}// End ExtractWindowDetails