File Types

This is a discussion on File Types within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I have two questions about file types. [1]I was wondering how to edit the registry and associate file types ...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    File Types

    Hello, I have two questions about file types.

    [1]I was wondering how to edit the registry and associate file types in C++. I've googled it, and found some info but I don't really understand, and I don't really want to go messing with it since it can get you into trouble...

    [2]Along with that, how would you get the program to know what file you clicked on? Like with text files, you double click on the file and it opens the file in the editor. Is there a way to do that in C++? (Of course there is, how would everyone else do it?) If #1 can't be done, then of course forget #2 because they go together. Thanks in advance.

    If anyone just knows of any links or tutorials for editing the registry in C++, that would be fine also.
    Last edited by mikeman118; 12-21-2007 at 06:41 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thank you codeplug, that helps. But how is that done in C++?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    The first link describes what registry entries that needed to be modified/added.

    The second link is the reference for Win32 Registry functions. If you search these forums for registry functions, you should find plenty of examples.

    RegOpenKey
    RegCreateKey
    RegQueryValue
    RegSetValue

    gg

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok, thank you, I found the answer to my second question (for all who care to know, it's in the lpCmdLine parameter of the WinMain function). I'll look into that codeplug.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Well, for your second question, it depends on how you do things. The usual way is to just send the filename of the file you're trying to open to the program, but there are other ways. If you do the first one I mentioned, then it's stored lpCmdLine (contains the arguments passed to your program).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok, I just tried to do as codeplug said, however I don't really think it worked. Here's what I tried:
    Code:
    RegCreateKeyEx(HKEY_CLASSES_ROOT, ".cboard", 0, NULL, REG_OPTION_NON_VOLATILE,
    	KEY_WRITE, NULL, NULL, (LPDWORD)REG_CREATED_NEW_KEY);
    Does anyone know what's wrong? Thanks.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Second to last parameter is not optional. You probably need more than just KEY_WRITE access - check the docs.

    >>I don't really think it worked
    A better posting would have been: "RegCreateKeyEx() is returning error xxx"
    xxx was probably invalid parameter - which would have given you a clue of where to look for the problem.

    gg

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    See the thing was it didn't give me any errors or anything, it just seemed that nothing changed. That's why I wasn't more specific - I don't really think I could have been! But ok, I'll try changing the second to last parameter, thanks.

  10. #10
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok, so MSDN says that the second to last parameter that I have set as NULL is supposed to be a PHKEY value. So I tried using
    Code:
    PHKEY phKey;
    but it gives me a warning that it is uninitialized, and then when running it gives me an error about being uninitialized. Is it wanting me to allocate memory for it?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    No, PHKEY is another stupid name for HKEY*. In other words, create a HKEY and pass it along with &. This variable is set to the handle of your newly created key AFAIK.
    Again, tell us what the function returns and if not ERROR_SUCCESS, then call GetLastError and post that error here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    I just tried putting "&phKey", and it gives me an error finally. It's not in compiling, but running:
    Code:
    Unhandled exception at 0x77dd7b6a in Program.exe: 0xC0000005: Access violation writing location 0x00000001.
    It appears to be on the line (I have it sperated over two lines) where I just put the "&phKey", in case that helps. Here's what the line actually says:
    Code:
    /*...*/KEY_WRITE, NULL, &phKey, (LPDWORD)REG_CREATED_NEW_KEY);
    What else should I look for?

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Do you understand what LP and the P prefix is for? It's telling you it's a pointer.
    Code:
    (LPDWORD)REG_CREATED_NEW_KEY
    This is wrong. It's expecting a POINTER to a DWORD that will be set to REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY.
    Never cast variables like that! When something takes a pointer, you shouldn't pass a non-pointer by casting it. No wonder you get a crash.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    The registry functions don't use Set/GetLastError() (for whatever reason) they just return the error straight up.

    We need to see all related code. For example:
    Code:
        LONG res = RegCreateKeyExA(HKEY_CLASSES_ROOT, ".cboard", 0, NULL, 
                                   REG_OPTION_NON_VOLATILE,	KEY_ALL_ACCESS, NULL, 
                                   NULL, NULL);
        if (res != ERROR_SUCCESS)
    Last parameter is an output parameter BTW.

    [edit]second to last param in my example is wrong [/edit]

    gg
    Last edited by Codeplug; 12-21-2007 at 01:09 PM.

  15. #15
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thank you everyone, it finally worked ! Now I have to ask what to do next. So let's say it created a key called ".cboard". I can find it just fine in the Registry Editor. However, it doesn't associate it with anything. What keys do I add to it to make it associated with my program? Thanks.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21