Creating File Extension / File Type

Posted on 2003-03-30
Medium Priority
Last Modified: 2010-04-17
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).
Question by:wyckedcrow
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

fl0yd earned 300 total points
ID: 8236918
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.


Author Comment

ID: 8258169
Hi fl0yd,

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


Expert Comment

ID: 8259701
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

// 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
    // 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.


Author Comment

ID: 8276663
Thanks fl0yd!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question