Click here to Skip to main content
15,891,248 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMFC, My Picture Control has problem. [Solved] Pin
yu-jian28-Sep-12 4:29
yu-jian28-Sep-12 4:29 
QuestionRe: MFC, My Picture Control has problem. Pin
David Crow28-Sep-12 7:29
David Crow28-Sep-12 7:29 
AnswerRe: MFC, My Picture Control has problem. Pin
jeron128-Sep-12 8:33
jeron128-Sep-12 8:33 
GeneralRe: MFC, My Picture Control has problem. Pin
yu-jian29-Sep-12 4:44
yu-jian29-Sep-12 4:44 
QuestionFind ADS Files/Streams? Pin
bosfan28-Sep-12 4:07
bosfan28-Sep-12 4:07 
AnswerRe: Find ADS Files/Streams? Pin
Richard MacCutchan28-Sep-12 6:11
mveRichard MacCutchan28-Sep-12 6:11 
GeneralMy Vote is 1 Pin
Smart Arab1-Oct-12 5:36
Smart Arab1-Oct-12 5:36 
AnswerRe: Find ADS Files/Streams? Pin
Smart Arab1-Oct-12 5:31
Smart Arab1-Oct-12 5:31 
Use this Code :
Streams.h
C#
//
// return code type
//
typedef INT NTSTATUS;

//
// Check for success
//
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)

//
// The NT return codes we care about
//
#define STATUS_BUFFER_OVERFLOW           ((NTSTATUS)0x80000005L)

//--------------------------------------------------------------------
//     N T F S C O N T R O L F I L E   D E F I N I T I O N S
//--------------------------------------------------------------------

//
// Prototype for NtFsControlFile and data structures
// used in its definition
//

//
// Io Status block (see NTDDK.H)
//
typedef struct _IO_STATUS_BLOCK {
    NTSTATUS Status;
    ULONG Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;


//
// Apc Routine (see NTDDK.H)
//
typedef VOID (*PIO_APC_ROUTINE) (
				PVOID ApcContext,
				PIO_STATUS_BLOCK IoStatusBlock,
				ULONG Reserved
			);

//
// File information classes (see NTDDK.H)
//
typedef enum _FILE_INFORMATION_CLASS {
// end_wdm
    FileDirectoryInformation       = 1,
    FileFullDirectoryInformation, // 2
    FileBothDirectoryInformation, // 3
    FileBasicInformation,         // 4  wdm
    FileStandardInformation,      // 5  wdm
    FileInternalInformation,      // 6
    FileEaInformation,            // 7
    FileAccessInformation,        // 8
    FileNameInformation,          // 9
    FileRenameInformation,        // 10
    FileLinkInformation,          // 11
    FileNamesInformation,         // 12
    FileDispositionInformation,   // 13
    FilePositionInformation,      // 14 wdm
    FileFullEaInformation,        // 15
    FileModeInformation,          // 16
    FileAlignmentInformation,     // 17
    FileAllInformation,           // 18
    FileAllocationInformation,    // 19
    FileEndOfFileInformation,     // 20 wdm
    FileAlternateNameInformation, // 21
    FileStreamInformation,        // 22
    FilePipeInformation,          // 23
    FilePipeLocalInformation,     // 24
    FilePipeRemoteInformation,    // 25
    FileMailslotQueryInformation, // 26
    FileMailslotSetInformation,   // 27
    FileCompressionInformation,   // 28
    FileObjectIdInformation,      // 29
    FileCompletionInformation,    // 30
    FileMoveClusterInformation,   // 31
    FileQuotaInformation,         // 32
    FileReparsePointInformation,  // 33
    FileNetworkOpenInformation,   // 34
    FileAttributeTagInformation,  // 35
    FileTrackingInformation,      // 36
    FileMaximumInformation
// begin_wdm
} FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS;


//
// Streams information
//
#pragma pack(4)
typedef struct {
	ULONG    	        NextEntry;
	ULONG    	        NameLength;
	LARGE_INTEGER    	Size;
	LARGE_INTEGER    	AllocationSize;
	USHORT    	        Name[1];
} FILE_STREAM_INFORMATION, *PFILE_STREAM_INFORMATION;
#pragma pack()


Streams.c
C#
#define UNICODE 1
#include <windows.h>
#include <stdio.h>
#include "streams.h"


//
// Native functions we use
//
NTSTATUS (__stdcall *NtQueryInformationFile)( 
    IN HANDLE FileHandle,
    OUT PIO_STATUS_BLOCK IoStatusBlock,
    OUT PVOID FileInformation,
    IN ULONG Length,
    IN FILE_INFORMATION_CLASS FileInformationClass
    );

ULONG (__stdcall *RtlNtStatusToDosError) (
		IN NTSTATUS Status
		);

//
// Globals
//
ULONG FilesWithStreams = 0;
ULONG FilesProcessed = 0;
ULONG DotsPrinted = 0;
BOOLEAN PrintDirectoryOpenErrors = FALSE;

//----------------------------------------------------------------------
//
// PrintNtError
//
// Formats an error message for the last native error.
//
//----------------------------------------------------------------------
void PrintNtError( NTSTATUS status )
{
	WCHAR *errMsg;

	FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
			NULL, RtlNtStatusToDosError( status ), 
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
			(LPTSTR) &errMsg, 0, NULL );
	wprintf(L"%s\n", errMsg );
	LocalFree( errMsg );
}

//--------------------------------------------------------------------
//
// PrintWin32Error
// 
// Translates a Win32 error into a text equivalent
//
//--------------------------------------------------------------------
void PrintWin32Error( DWORD ErrorCode )
{
	LPVOID lpMsgBuf;
 
	FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
					NULL, ErrorCode, 
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					(LPTSTR) &lpMsgBuf, 0, NULL );
	wprintf(L"%s\n", lpMsgBuf );
	LocalFree( lpMsgBuf );
}

//----------------------------------------------------------------------
//
// EnableTokenPrivilege
//
// Enables the load driver privilege
//
//----------------------------------------------------------------------
BOOL EnableTokenPrivilege( PTCHAR PrivilegeName )
{
    TOKEN_PRIVILEGES tp;
    LUID luid;
	HANDLE	hToken;
    TOKEN_PRIVILEGES tpPrevious;
    DWORD cbPrevious=sizeof(TOKEN_PRIVILEGES);

	//
	// Get debug privilege
	//
	if(!OpenProcessToken( GetCurrentProcess(),
				TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
				&hToken )) {
	
		return FALSE;
	}
	
    if(!LookupPrivilegeValue( NULL, PrivilegeName, &luid )) return FALSE;

    //
    // first pass.  get current privilege setting
    //
    tp.PrivilegeCount           = 1;
    tp.Privileges[0].Luid       = luid;
    tp.Privileges[0].Attributes = 0;

    AdjustTokenPrivileges(
            hToken,
            FALSE,
            &tp,
            sizeof(TOKEN_PRIVILEGES),
            &tpPrevious,
            &cbPrevious
            );

    if (GetLastError() != ERROR_SUCCESS) return FALSE;

    //
    // second pass.  set privilege based on previous setting
    //
    tpPrevious.PrivilegeCount       = 1;
    tpPrevious.Privileges[0].Luid   = luid;
    tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
    AdjustTokenPrivileges(
            hToken,
            FALSE,
            &tpPrevious,
            cbPrevious,
            NULL,
            NULL
            );

    return GetLastError() == ERROR_SUCCESS;
}


//--------------------------------------------------------------------
//
// ProcessFile
//
// Queries a file to obtain stream information.
//
//--------------------------------------------------------------------
VOID ProcessFile( WCHAR *FileName, BOOLEAN IsDirectory, BOOLEAN Delete )
{
    PFILE_STREAM_INFORMATION  streamInfo, streamInfoPtr;
    ULONG    streamInfoSize = 0;
    BOOLEAN  printedFile = FALSE;
    NTSTATUS status;
    HANDLE   fileHandle;
    WCHAR    streamName[MAX_PATH];
    WCHAR    fullStreamName[MAX_PATH];
    IO_STATUS_BLOCK ioStatus;

    //
    // Open the file
    //
    fileHandle = CreateFile( FileName, GENERIC_READ,
					FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, 
					OPEN_EXISTING, 
					FILE_FLAG_BACKUP_SEMANTICS, 0 );
    if( fileHandle == INVALID_HANDLE_VALUE ) {

        if( !IsDirectory || PrintDirectoryOpenErrors ) {

            wprintf(L"\rError opening %s:\n", FileName );
            PrintWin32Error( GetLastError() );
        }
        return;
    }

    if( !(++FilesProcessed % 500)) {

        if( DotsPrinted == 3 ) {

            wprintf(L"\r     \r");
            DotsPrinted = 0;

        } else {
            DotsPrinted++;
            wprintf(L".");
        }
        fflush( stdout );
    }
    
    streamInfoSize = 16384;
    streamInfo = malloc( streamInfoSize );
    status = STATUS_BUFFER_OVERFLOW;
    while( status == STATUS_BUFFER_OVERFLOW ) {

        status = NtQueryInformationFile( fileHandle, &ioStatus,
                                         streamInfo, streamInfoSize,
                                         FileStreamInformation );
        if( status == STATUS_BUFFER_OVERFLOW ) {

            free( streamInfo );
            streamInfoSize += 16384;
            streamInfo = malloc( streamInfoSize );

        } else {

            break;
        }
    }

    //
    // If success, dump the contents
    //
    if( NT_SUCCESS( status ) && ioStatus.Information ) {

        streamInfoPtr = streamInfo;
        while( 1 ) {
            
            memcpy( streamName, 
                     streamInfoPtr->Name, 
                     streamInfoPtr->NameLength );
            streamName[ streamInfoPtr->NameLength/2 ] = 0;

            //
            // Skip the standard Data stream
            //
            if( wcsicmp( streamName, L"::$DATA" )) {

                if( !printedFile ) {
                    wprintf(L"\r%s:\n", FileName );
                    printedFile = TRUE;
                }

                if( Delete ) {

                    swprintf( fullStreamName, L"%s%s", FileName, streamName );
                    if( !DeleteFile( fullStreamName )) {

						wprintf(L"   Error deleting %s:\n", streamName );
						PrintWin32Error( GetLastError());
					} else {

						wprintf(L"   Deleted %s\n", streamName );
					}
                } else {

					wprintf(L"   %20s\t%I64d\n", streamName, streamInfoPtr->Size.QuadPart );
				}
            }

            if( !streamInfoPtr->NextEntry ) break;

            FilesWithStreams++;
            streamInfoPtr = (PFILE_STREAM_INFORMATION) ((char *) streamInfoPtr + 
                                                     streamInfoPtr->NextEntry );
        }

    } else if( !NT_SUCCESS( status )) {

        wprintf(L"\rError on %s: ", FileName );
        PrintNtError( status );
    }
    free( streamInfo );
    CloseHandle( fileHandle );
}



//--------------------------------------------------------------------
//
// ProcessDirectory
// 
// Recursive routine that passes files to the stream analyzing 
// function.
//
//--------------------------------------------------------------------
void ProcessDirectory( WCHAR *PathName, WCHAR *SearchPattern, 
                       BOOLEAN Recurse, BOOLEAN Delete )
{
	WCHAR			subName[MAX_PATH], fileSearchName[MAX_PATH], searchName[MAX_PATH];
	HANDLE			dirHandle, patternHandle;
	static BOOLEAN	firstCall = TRUE;
	WIN32_FIND_DATA foundFile;

	//
	// Scan the files and/or directories if this is a directory
	//
	if( firstCall ) {

		if( PathName[ wcslen( PathName ) - 1] == L'\\' ) {

			PathName[ wcslen( PathName ) - 1] = 0;
		}

		if( wcsrchr( PathName, '*' ) ) {
	
            if( wcsrchr( PathName, '\\' ) ) {

                swprintf( SearchPattern, wcsrchr( PathName, '\\' )+1 );
                wcscpy( searchName, PathName );
                wcscpy( wcsrchr( searchName, '\\')+1, L"*.*" );

            } else {
                
                swprintf( SearchPattern, PathName );
                wcscpy( searchName, PathName );
            }
            swprintf( fileSearchName, L"%s", PathName );

		} else {

			swprintf( SearchPattern, L"*.*" );
            if( Recurse ) {

                swprintf( searchName, L"%s\\*.*", PathName );
                swprintf( fileSearchName, L"%s\\*.*", PathName );
            } else {

                swprintf( searchName, L"%s", PathName );
                swprintf( fileSearchName, L"%s", PathName );
            }
		}

	} else {

		swprintf( searchName, L"%s\\*.*", PathName );
		swprintf( fileSearchName, L"%s\\%s", PathName, SearchPattern );
	}

	//
	// Process all the files, according to the search pattern
	//
	if( (patternHandle = FindFirstFile( fileSearchName, &foundFile )) != 
		INVALID_HANDLE_VALUE  ) {

		do {

			if( firstCall || (wcscmp( foundFile.cFileName, L"." ) &&
				wcscmp( foundFile.cFileName, L".." ))) {

				wcscpy( subName, searchName );
				if( wcsrchr( subName, '\\' ) ) 
					wcscpy( wcsrchr( subName, '\\')+1, foundFile.cFileName );
				else
					wcscpy( subName, foundFile.cFileName );

				//
				// Do this file/directory
				//
				ProcessFile( subName, 
                             (BOOLEAN) (foundFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY), 
                             Delete );
			}
		} while( FindNextFile( patternHandle, &foundFile ));
		FindClose( patternHandle );
	}

	//
	// Now recurse if we're supposed to
	//
	if( Recurse ) {

        if( firstCall && !wcsrchr( searchName, L'\\') ) {

            if( wcsrchr( searchName, L'*' )) {

                if( (dirHandle = FindFirstFile( L"*.*", &foundFile )) == 
                    INVALID_HANDLE_VALUE  ) {

                    //
                    // Nothing to process
                    //
                    return;
                }
            } else {

                if( (dirHandle = FindFirstFile( searchName, &foundFile )) == 
                    INVALID_HANDLE_VALUE  ) {

                    //
                    // Nothing to process
                    //
                    return;
                }
            }
        } else {

            if( (dirHandle = FindFirstFile( searchName, &foundFile )) == 
                INVALID_HANDLE_VALUE  ) {

                //
                // Nothing to process
                //
                return;
            }
        }
        firstCall = FALSE;

		do {

			if( (foundFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
				wcscmp( foundFile.cFileName, L"." ) &&
				wcscmp( foundFile.cFileName, L".." )) {

				wcscpy( subName, searchName );
				if( wcsrchr( subName, '\\' ) ) 
					wcscpy( wcsrchr( subName, '\\')+1, foundFile.cFileName );
				else
					wcscpy( subName, foundFile.cFileName );

				//
				// Go into this directory
				//
				ProcessDirectory( subName, SearchPattern, Recurse, Delete );

			}
		} while( FindNextFile( dirHandle, &foundFile ));
	}

	FindClose( dirHandle );
}


int Usage( WCHAR *ProgramName ) 
{
    wprintf(L"usage: %s [-s] [-d] <file or directory>\n", ProgramName );
    wprintf(L"-s     Recurse subdirectories\n");
    wprintf(L"-d     Delete streams\n\n");
    return -1;
}


int wmain( int argc, WCHAR *argv[] ) 
{
    BOOLEAN     recurse = FALSE, delete = FALSE;
    PWCHAR      filePart;
	WCHAR		volume[] = L"C:\\";
	DWORD		fsFlags;
    WCHAR       searchPattern[MAX_PATH];
	WCHAR		searchPath[MAX_PATH];
    int         i;

    

    if( argc > 1 ) {
        
        for( i = 1; i < argc; i++ ) {

            if( argv[i][0] != L'/' && argv[i][0] != L'-' ) {

                if( i != argc-1 ) {

                    wprintf( argv[i] );
                    return Usage( argv[0] );
                }
				continue;
            }

            if( argv[i][1] == L's' || argv[i][1] == L'S' ) {

                recurse = TRUE;

            } else if( argv[i][1] == L'd' || argv[i][1] == 'D' ) {

                delete = TRUE;

            } else return Usage( argv[0] );
        }
    } else {

        return Usage( argv[0] );
    }

    //
    // Enable backup privilege if we can
    //
    if( EnableTokenPrivilege( SE_BACKUP_NAME )) {

        PrintDirectoryOpenErrors = TRUE;
    }

    //
    // Load the NTDLL entry point we need
    //
	if( !(NtQueryInformationFile = (void *) GetProcAddress( GetModuleHandle(L"ntdll.dll"),
			"NtQueryInformationFile" )) ) {

		wprintf(L"\nCould not find NtQueryInformationFile entry point in NTDLL.DLL\n");
		exit(1);
	}
	if( !(RtlNtStatusToDosError = (void *) GetProcAddress( GetModuleHandle(L"ntdll.dll"),
							"RtlNtStatusToDosError" )) ) {

		wprintf(L"\nCould not find RtlNtStatusToDosError entry point in NTDLL.DLL\n");
		exit(1);
	}
	GetFullPathName( argv[argc-1], MAX_PATH, searchPath, &filePart );

	//
	// Make sure that its a FAT volume
	//
	if( searchPath[1] == L':' ) {

		fsFlags = 0;
		volume[0] = searchPath[0];
		GetVolumeInformation( volume, NULL, 0, NULL, NULL, &fsFlags, NULL, 0 );
		if( !(fsFlags & FILE_NAMED_STREAMS )) {

			wprintf(L"\nThe specified volume does not support streams.\n\n");
			exit(2);
		}
	}
    
    //
    // Now go and process directories
    //
    ProcessDirectory( searchPath, searchPattern, recurse, delete );

    if( !FilesWithStreams ) wprintf(L"No files with streams found.\n\n");
    return 0;
}

GeneralRe: Find ADS Files/Streams? Pin
bosfan1-Oct-12 20:27
bosfan1-Oct-12 20:27 
GeneralRe: Find ADS Files/Streams? Pin
bosfan16-Oct-12 20:45
bosfan16-Oct-12 20:45 
QuestionHas anybody tried the new C++ RAD Toolkit SmartDB (http://sdb.net.in) Pin
sumeet12327-Sep-12 22:32
sumeet12327-Sep-12 22:32 
AnswerRe: Has anybody tried the new C++ RAD Toolkit SmartDB (http://sdb.net.in) Pin
Richard MacCutchan28-Sep-12 2:00
mveRichard MacCutchan28-Sep-12 2:00 
QuestionMultiMon/Multi-VideoCards/Multi-Displays With Multiple DirectDraw FullScreen Exclusive Applications. Pin
bowen198127-Sep-12 5:30
bowen198127-Sep-12 5:30 
AnswerRe: MultiMon/Multi-VideoCards/Multi-Displays With Multiple DirectDraw FullScreen Exclusive Applications. Pin
Ingo28-Sep-12 2:00
Ingo28-Sep-12 2:00 
QuestionHighlighting Control based on Selection Pin
Don Guy27-Sep-12 3:52
Don Guy27-Sep-12 3:52 
AnswerRe: Highlighting Control based on Selection Pin
Jochen Arndt27-Sep-12 5:04
professionalJochen Arndt27-Sep-12 5:04 
AnswerRe: Highlighting Control based on Selection Pin
Marius Bancila1-Oct-12 22:12
professionalMarius Bancila1-Oct-12 22:12 
Questionhow to use the outside class Pin
yu-jian27-Sep-12 3:18
yu-jian27-Sep-12 3:18 
AnswerRe: how to use the outside class Pin
David Crow27-Sep-12 3:27
David Crow27-Sep-12 3:27 
QuestionPrinting with CRichEditCtrl control Pin
sdancer7526-Sep-12 23:05
sdancer7526-Sep-12 23:05 
AnswerRe: Printing with CRichEditCtrl control Pin
sdancer755-Oct-12 3:55
sdancer755-Oct-12 3:55 
GeneralRe: Printing with CRichEditCtrl control Pin
chaau7-Oct-12 18:44
chaau7-Oct-12 18:44 
GeneralRe: Printing with CRichEditCtrl control Pin
sdancer757-Oct-12 20:21
sdancer757-Oct-12 20:21 
QuestionQuality based Video encoding in H.264 Codec Pin
Ajesh Kumar.T.T26-Sep-12 21:01
Ajesh Kumar.T.T26-Sep-12 21:01 
QuestionHow to Convert palette buffer to RGB buffer Pin
Ajesh Kumar.T.T26-Sep-12 21:00
Ajesh Kumar.T.T26-Sep-12 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.