home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
pserv.cpl
/
pserv-2.4.exe
/
source
/
CListViewTools.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-01-05
|
8KB
|
287 lines
#include "stdafx.h"
#include "CListViewTools.h"
#include "CConfiguration.h"
#include "WriteOptimizedFile.h"
CString GetUsernameFromSid( PSID sid )
{
CString result;
TCHAR szUserName[256], szDomainName[256];
DWORD dwName = sizeof(szUserName), dwDomain = sizeof(szDomainName);
SID_NAME_USE snu;
if( LookupAccountSid(NULL, sid, szUserName, &dwName, szDomainName, &dwDomain, &snu ) )
{
result.Format(_T("%s\\%s"), szDomainName, szUserName);
}
return result;
}
CString GetUserNameFromProcess(HANDLE hProcess)
{
CString result;
HANDLE hToken;
if( OpenProcessToken(hProcess, TOKEN_QUERY, &hToken) )
{
DWORD dwReturnLength = 0;
if( !GetTokenInformation(hToken, TokenUser, NULL, 0, &dwReturnLength ) )
{
DWORD dwLastError = GetLastError();
if( dwLastError == ERROR_INSUFFICIENT_BUFFER )
{
BYTE* memory = new BYTE[dwReturnLength];
if( memory )
{
ZeroMemory( memory, dwReturnLength );
if( GetTokenInformation(hToken, TokenUser, memory, dwReturnLength, &dwReturnLength ) )
{
result = GetUsernameFromSid(((TOKEN_USER*) memory)->User.Sid);
}
delete memory;
}
}
}
CloseHandle(hToken);
}
return result;
}
void PrintToFile( CFile* pFile, LPCTSTR lpszMessage, ... )
{
va_list args;
va_start( args, lpszMessage );
CString result;
result.FormatV( lpszMessage, args );
pFile->Write( (LPCTSTR) result, result.GetLength() * sizeof(TCHAR) );
}
void DisplayErrorMessage(LPCTSTR lpszCaption, ...)
{
DWORD dwLastError = GetLastError();
va_list args;
va_start( args, lpszCaption);
CString caption;
caption.FormatV(lpszCaption, args );
if( dwLastError != S_OK )
{
LPVOID lpMsgBuf;
if( FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ) )
{
CString copyOfMessage( (LPCTSTR) lpMsgBuf );
copyOfMessage.TrimRight( _T("\r\n \t") );
AfxMessageBox( FormattedString(_T("%s. %s"),
(LPCTSTR) caption, (LPCTSTR)copyOfMessage) , MB_OK|MB_ICONSTOP );
LocalFree( lpMsgBuf );
return;
}
}
AfxMessageBox(caption, MB_OK|MB_ICONSTOP );
}
void DeleteObjects(CObArray& array)
{
while( array.GetSize() )
{
CObject* element = array.GetAt(0);
if( element )
{
array.RemoveAt(0);
delete element;
}
else break;
}
}
CString FormattedString(LPCTSTR expression, ... )
{
va_list args;
va_start( args, expression);
CString result;
result.FormatV(expression, args );
return result;
}
CListViewEntries::CListViewEntries( GENERICCOMPAREFN* pSortMethods, LPCTSTR lpszName )
: m_SortMethods( pSortMethods ),
m_bSortAscending( TRUE ),
m_dwSortMethod( 0 ),
m_strName( lpszName ),
m_dwNumberOfDisplayColumns( 0 )
{
}
CListViewEntries::~CListViewEntries()
{
DeleteObjects(m_Entries);
DeleteObjects(m_Columns);
}
void CListViewEntries::DoConfigExchange(CRegistry& key)
{
// if this is the first time, create column widths array
ASSERT( m_Columns.GetSize() != 0 );
CString buffer;
for( DWORD dwIndex = 0, dwMax = m_Columns.GetSize(); dwIndex < dwMax; dwIndex++ )
{
CListViewColumn* c = (CListViewColumn*) m_Columns.GetAt(dwIndex);
ASSERT_VALID(c);
buffer.Format(_T("%s_Width"), (LPCTSTR) c->m_strCaption );
key.RegExchange(buffer, &c->m_dwWidth );
buffer.Format(_T("%s_DisplayOrder"), (LPCTSTR) c->m_strCaption );
key.RegExchange(buffer, &c->m_dwDisplayOrder );
}
key.RegExchange( _T("SortAscending"), &m_bSortAscending );
key.RegExchange( _T("SortMethod"), &m_dwSortMethod );
if( !key.m_bSave )
RefreshDisplayColumns();
}
CString CListViewEntries::GetDisplayString(CListViewEntry* item, DWORD dwColumn)
{
if( item && (dwColumn < m_dwNumberOfDisplayColumns) )
return item->GetDisplayString(m_dwDisplayColumn[dwColumn]);
return CString();
}
void CListViewEntries::Sort()
{
CListViewEntry** p = (CListViewEntry**) m_Entries.GetData();
int numberOfEntries = m_Entries.GetSize();
if( m_dwSortMethod >= m_dwNumberOfDisplayColumns )
m_dwSortMethod = 0;
qsort(p,numberOfEntries,sizeof(CListViewEntry*),m_SortMethods[m_dwDisplayColumn[m_dwSortMethod]]);
if( !m_bSortAscending )
{
CListViewEntry* temp;
for( int i = 0, imax = numberOfEntries/2, in = numberOfEntries-1; i < imax; i++, in-- )
{
temp = p[i];
p[i] = p[in];
p[in] = temp;
}
}
}
CString XmlEscape(LPCTSTR s)
{
CString temp(s);
temp.Replace(TEXT("&"),TEXT("&"));
temp.Replace(TEXT("<"),TEXT("<"));
temp.Replace(TEXT(">"),TEXT(">"));
return temp;
}
BOOL CListViewEntries::ExportAsXml( LPCTSTR lpszFilename )
{
CWriteOptimizedFile file;
if( file.Open( lpszFilename, CFile::modeCreate | CFile::modeWrite ) )
{
ExportXmlToFile(&file);
file.Close();
return TRUE;
}
return FALSE;
}
CListViewColumn* CListViewEntries::GetColumnByName( LPCTSTR lpszName )
{
for( int i = 0, imax = m_Columns.GetSize(); i < imax; i++ )
{
CListViewColumn* c = (CListViewColumn*) m_Columns.GetAt(i);
if( c->m_strCaption.Compare(lpszName) == 0 )
{
return c;
}
}
return NULL;
}
DWORD CListViewEntries::GetNumberOfColumns()
{
return m_dwNumberOfDisplayColumns;
}
CListViewColumn* CListViewEntries::GetColumn( DWORD dwIndex )
{
if( dwIndex < m_dwNumberOfDisplayColumns )
return (CListViewColumn*) m_Columns.GetAt(m_dwDisplayColumn[dwIndex]);
return NULL;
}
void CListViewEntries::RefreshDisplayColumns()
{
DWORD nColumns = DWORD(m_Columns.GetSize());
for( m_dwNumberOfDisplayColumns = 0; m_dwNumberOfDisplayColumns < nColumns; m_dwNumberOfDisplayColumns++ )
{
BOOL found = FALSE;
for( DWORD iCurrentIndex = 0; iCurrentIndex < nColumns; iCurrentIndex++ )
{
CListViewColumn* c = (CListViewColumn*) m_Columns.GetAt( iCurrentIndex );
if( c->m_dwDisplayOrder == m_dwNumberOfDisplayColumns )
{
m_dwDisplayColumn[m_dwNumberOfDisplayColumns] = iCurrentIndex;
found = TRUE;
break;
}
}
if( !found )
break;
}
}
BOOL CListViewEntries::Matches(CListViewEntry* item, LPFNFindSubstring lpfnFindSubstring, LPCTSTR lpszFind)
{
if( !item )
return FALSE;
for( DWORD dwIndex = 0; dwIndex < m_dwNumberOfDisplayColumns; dwIndex++ )
{
if( lpfnFindSubstring( item->GetDisplayString(m_dwDisplayColumn[dwIndex]), lpszFind ) )
{
return TRUE;
}
}
return FALSE;
}
void CListViewEntries::CreateColumns( LPCTSTR lpszColumn, ... )
{
va_list args;
va_start( args, lpszColumn );
int index = 0;
while( lpszColumn )
{
m_Columns.Add( new CListViewColumn(lpszColumn, index++ ) );
lpszColumn = va_arg( args, LPCTSTR );
}
}
CListViewColumn::CListViewColumn( LPCTSTR lpszCaption, DWORD dwDisplayOrder, DWORD dwWidth )
: m_strCaption(lpszCaption),
m_dwDisplayOrder( dwDisplayOrder ),
m_dwWidth( dwWidth )
{
}