Hello, I have two questions about file types.
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...
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.
Thank you codeplug, that helps. But how is that done in C++?
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.
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.
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).
Ok, I just tried to do as codeplug said, however I don't really think it worked. Here's what I tried:
Does anyone know what's wrong? Thanks.
RegCreateKeyEx(HKEY_CLASSES_ROOT, ".cboard", 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, NULL, (LPDWORD)REG_CREATED_NEW_KEY);
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.
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.
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
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?
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.
I just tried putting "&phKey", and it gives me an error finally. It's not in compiling, but running:
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:
Unhandled exception at 0x77dd7b6a in Program.exe: 0xC0000005: Access violation writing location 0x00000001.
What else should I look for?
/*...*/KEY_WRITE, NULL, &phKey, (LPDWORD)REG_CREATED_NEW_KEY);
Do you understand what LP and the P prefix is for? It's telling you it's a pointer.
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.
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:
Last parameter is an output parameter BTW.
LONG res = RegCreateKeyExA(HKEY_CLASSES_ROOT, ".cboard", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
if (res != ERROR_SUCCESS)
second to last param in my example is wrong :)[/edit]
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.