Results 1 to 18 of 18

Thread: C++ Walk through

  1. #1

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    See this thread
    http://www.vbforums.com/showthread.p...threadid=66943

    Parksie posted some files with code for manipulating the windows registry.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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!");
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    Thanks very much Parksie, but when I put that code under a button, I get:

    'Registry' : undeclared identifier

    on compile.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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.

  10. #10

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    And it did end up working after I disabled precompiled headers.

    Thank You so much parksie.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    And is C++ Builder a program on its own or is it part of Visual C++?

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Totally separate, produced by Borland.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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++?

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    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;
    }

  17. #17

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    I got it...

    Silly mistake, I forgot to add the Registry.cpp in.

  18. #18
    New Member
    Join Date
    Apr 2001
    Posts
    1
    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.
    Giles Forster

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width