-
I've just begun to learn C++. I'm going along fine in the book but I'd like to try something in the registry.
From VB I usually use an API in the fashion:
RegCreateKey(stuff)
RegSetValueEx(stuff)
RegCloseKey(stuff)
But I've been pulling hairs out trying to get that to work in C++.
I've seen many bits and pieces about how to work with the registry but haven't seen a complete example. If someone could post any example that actually writes anything to the registry I'd love to see it. And if I could just cut and paste it into my program it would be great and then if it works I can compare it to what I've done and see what's up.
I'll tell you it's so frustrating to try to do something in C++ that I can do in VB in about 3 seconds. But I'm going to stick with it and see what comes of it. Because I used to be in love with Access and then started using VB and thought it was hard as well. But then I got more and more comfortable with it and all worked out great.
-
See this thread
http://www.vbforums.com/showthread.p...threadid=66943
Parksie posted some files with code for manipulating the windows registry.
-
I've seen those files, and even downloaded them. They look wonderful sitting on my desktop.
Only problem is that I have no idea how to call them. I'll to fumble around. All I know how to do is the MFC wizard and then a "Hello World" app. Oh, and I know how to draw on the screen with my mouse. But I'll try to add those files to my project and see if I can manage to call them.
C++ help file is much different than VB. I used the examples in VB sooooooo much. When I can actually see something in action, that works, is when it sticks to my brain. It seems the C++ help on MSDN explain many things about something but doesn't use it in an example.
-
Code:
Registry theReg;
theReg.SaveLong(HKEY_CURRENT_USER, "Software\My Product\Options", "A Value", 56);
theReg.SaveString(HKEY_CURRENT_USER, "Software\My Product\Options", "Other Value", "Hello!");
-
Thanks very much Parksie, but when I put that code under a button, I get:
'Registry' : undeclared identifier
on compile.
-
You need to add the two files to your project and add:
Code:
#include "Registry.h"
at the top of your source file. If you're using MFC then put it in stdafx.h
-
I'll give that a shot.
While I was fumbling around I also tried your .h and .cpp files and got the error:
fatal error C1010: unexpected end of file while looking for precompiled header directive
I just added the files to my project and tried to compile.
-
That'll be because of the way your project's set up. I think you may have to go to project settings, and disable precompiled headers for Registry.cpp :)
-
So I guess this is as good a time to ask as any.
Is MFC not the way to go when you are learning? I've seen a few people make comments on MFC that seems to lead that it's not the best way to go.
And while I'm at it I'll tell you what I'm up to. I have a VB project that when a user downloads it and tries to install, it needs a reboot if some DLLs are older. Then they have to find the file again and try to reinstall.
What I'm trying to do is make a very simple exe in C++ that writes my setup.exe to the "Run" area of the registry, and then runs my setup.exe. That way if the installation program requires a restart it will finish installing after it's done updating the initial files. Then when the program installs it will remove that setting from Run.
I'm thinking this will give me a better install ratio. I really do want to learn all about C++, and I realize I'm putting the cart before the horse here trying to create this simple exe, but I think it will all go well.
-
And it did end up working after I disabled precompiled headers.
Thank You so much parksie.
-
MFC is not the way to learn Windows programming. I'm sure others would disagree with me, but if you want to do something like that, use C++ Builder -- it's a lot better suited for it.
In this bar we pour raw API :)
For your project, since you don't need an interface, why bother using windows and dialogues at all? Why not just a simple program:
Code:
#include <windows.h>
#include "Registry.h"
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
Registry theReg;
theReg.SaveString(HKEY_LOCAL_MACHINE, "...\\Run", "MyApp", "c:\\...\\setup.exe");
}
...or something similar. Obviously you'd need to parse out lpCmdLine to get the path unless it's hard-coded.
-
And is C++ Builder a program on its own or is it part of Visual C++?
-
Totally separate, produced by Borland.
-
Thanks for all your help.
You're the biggest Nerd in my book.
One last question to wrap up this thread, is there an app.path equivalent in C++?
-
If you're using a console application:
Code:
int main(int argc, char **argv) { ... }
App.Path is equivalent to argv[0].
If you're using a windows application:
Code:
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ... }
Use this code:
Code:
TCHAR pcBuf[MAX_PATH];
long lCount = GetModuleFileName(hInstance, pcBuf, MAX_PATH);
lCount contains the length of the file name, which is stored in pcBuf.
-
I've been playing with the console program since you mentioned that and I like how it starts at ground 0.
But, I can't seem to get the Registry writing function to work in the console app. It did work in the MFC.
Here's the code:
Code:
#include <windows.h>
#include "Registry.h"
int main(int argc, char **argv)
{
Registry theReg;
theReg.SaveString(HKEY_LOCAL_MACHINE, "...\\Run", "MyApp", "c:\\...\\setup.exe");
return 0;
}
-
I got it...
Silly mistake, I forgot to add the Registry.cpp in.
-
Check out this page on the Code Project.
There is plenty of example code and complte class wrappers here under the System\Registry section to keep you going for a while.
http://www.codeproject.com/system/
Hope it helps.