Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Creating File Extension / File Type

I'm writing an application in Visual Basic and I'm wondering what should I do to create my own file type/extension (eg. I want my application to save files with a .gx extension). Also, is the process the same if I write an application in another language, say, C++? Lastly, how do I "customize" a file extension that I create? ("customize" meaning determine the file size and such).
0
wyckedcrow
Asked:
wyckedcrow
  • 2
  • 2
1 Solution
 
fl0ydCommented:
There are no filetypes on windows systems. A file is a file is a file ... The extension is merely a hint to what it is being used for, but unlike other OS's like EPOC there is no way to know what type a file is other than looking inside. That is, files don't have an external signature on windows systems.

To get around this files on windows systems usually have header information inside the, e.g. the first two bytes of a .exe/.dll are 'MZ', a .bmp starts with 'BM', .zip files start with 'PK'. So it's the responsibility of an application developer to create and check for header information to see if it can read and process a file.

What you need to do is set up your own file format and do all the validation, creation, and loading procedures yourself. You can define default applications for file extensions in the system registry, but that doesn't mean that your application cannot be called with a 'fake' file that has your extension but doesn't comply with your file format.

.f
0
 
wyckedcrowAuthor Commented:
Hi fl0yd,

Thanks for the reply... I have a question about it, however... Could you please clarify the header information part please? thanks!

~Bill
0
 
fl0ydCommented:
A header is merely a conceptual thing -- it doesn't have to obay any rules. You can make it anything you like, as long as your application is able to identify files created by your application. It doesn't matter if you read or write your file in a specfic language, as long as the same logic for checking the header information is used. I'm not really good with VB so I will post a bit of c++ code instead. I hope you can relate this to VB:

// pragma's used to force alignment of data members to byte boundaries
#pragma pack( push, 1 )
// structure used as a header
struct GX_HEADER {
    WORD HeaderMagic;    // so-called magic code to identify file type
    DWORD VersionMajor;    // version information will make your life a lot easier in the future
    DWORD VersionMinor;
};
#pragma pack( pop )

// let's define a header magic for .gx files
// it can be anything, but let's choose 'GX' here
#define GX_HEADER_MAGIC ( 'G' | (WORD)( 'X' << 8 ) )

// version information should be changed every time you decide
// to make changes to the file format you use
// let's make it 1.0 for now
#define GX_VERSION_MAJOR 1
#define GX_VERSION_MINOR 0


// file format validation
bool IsValid( LPCSTR lpszPathName ) {

    GX_HEADER header = { 0 };
    // open file in binary mode
    std::ifstream ifs( lpszPathName, ios_base::in | ios_base::binary );
    // read header information
    ifs >> header;
    // close file
    ifs.close();
    // check header in file against what you expect
    if( header.HeaderMagic == GX_HEADER_MAGIC )
        if( header.VersionMajor == GX_VERSION_MAJOR )
            if( header.VersionMinor == GX_VERSION_MINOR )
                return true;
    // at least one check failed
    return false;
}

I hope this illustrates well enough one of many possibilities for header information. An alternative for more complex files would be to use a chunk-based format, like windows .avi files. To sum up: The basic concept is that you put information of any form you like in a file at a specific location. You then check for the presence of this information to make decisions on whether your application will be able to handle this file or not.

.f
0
 
wyckedcrowAuthor Commented:
Thanks fl0yd!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now