Thread: FindFirstStreamW() and FindNextStreamW() to get all file streams

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    31

    FindFirstStreamW() and FindNextStreamW() to get all file streams

    Hello All,

    I am using FindFirstStreamW() and FindNextStreamW() to iterate over all file streams and get the name and size.
    However, it returns on the data stream and alternate data streams and skips all other.

    BackupRead() used on the same file returns the above along with object identifiers and security data.
    Why is this and how can I get all streams without using BackupRead() ?

    Below is the code :
    Code:
    #include<stdio.h>
    #include<windows.h>
    #include<winbase.h>
    #include<stdlib.h>
    
    void FindAllStreams(HANDLE file)
    {
    	void *buffer;
    	int err;
    
    	WIN32_FIND_STREAM_DATA stream;
    	while(1)
    	{	
    		err = FindNextStreamW(file, &stream);
    		if(err == 0)
    		{
    			if (GetLastError() == ERROR_HANDLE_EOF)
    				printf("\nNo more streams available\n");
    			else
    				printf("error = 0x%x\n", GetLastError());
    			
    			break;
    		}
    		else
    		{
    			printf("StreamSize   = 0x%x\n", stream.StreamSize.QuadPart);
    			wprintf(L"StreamName = %s\n",   stream.cStreamName);
    		}
    	}
    }
    
    
    
    int wmain(int argc, wchar_t *argv[])
    {
        HANDLE file;
    	WIN32_FIND_STREAM_DATA stream;
    
        {    
            file = FindFirstStreamW(argv[1], FindStreamInfoStandard, &stream, 0);
            if (file == INVALID_HANDLE_VALUE)
            {
                printf("Could not crerate file handle\n");
                printf("error = 0x%x\n", GetLastError());
            }
            else
            {   
    			printf("\nStreamSize   = 0x%x\n", stream.StreamSize.QuadPart);
    			wprintf(L"StreamName = %s\n\n",   stream.cStreamName);
    
    			FindAllStreams(file);			
                FindClose(file);
            }        
        }
    
        return 0;
    }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > err = FindNextStreamW(file, &stream);
    How does this even compile, when stream is a local variable in another function?

    Or better yet, why aren't FindFirst / FindNext in the same function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    31
    Code:
    err = FindNextStreamW(file, &stream);
    here stream is declared in the same function
    Code:
    void FindAllStreams(HANDLE file)
    
    WIN32_FIND_STREAM_DATA stream;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    was it initialised by findfirst?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed