I'm a beginner to C++, I use CodeBlocks which I think is related to Ming so please make your answers pointing to C++ but not Visual C++. Thanks! And also, please ecplain things in detail ...

Well, after a long search on the net I found the code that looks through registry, gets information and prints it. What I want to do is to modify that program to do the same except not to print it, but to store gained information into an int. Then, add some extra text to the int and then finally use the system() function to execute that path. The program I've got so far:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HKEY hKey = 0;
    char buf[255] = {0};
    DWORD dwType = 0;
    DWORD dwBufSize = sizeof(buf);
    const char* subkey = "Software\\Unreal Technology\\Installed Apps\\Unreal Gold";

    if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        if( RegQueryValueEx(hKey,"Folder",0, &dwType, (BYTE*)buf, &dwBufSize) == ERROR_SUCCESS)
        {
            cout << "key value is '" << buf << "'\n";
        }
    }
    cin.get();
}

How do I change this program to do the following: 1) Instead of printing the key value, store the information in an int. 2) Add at the end of this information the text: "System\Unreal.exe" so instead of being: "F:\Tolvuleikir\Full\FPS\Unreal Anthology\UnrealGold\" (which BTW is the value of the key), it would be "F:\Tolvuleikir\Full\FPS\Unreal Anthology\UnrealGold\System\Unreal.exe" && change the format to the C++ friendly executable format, "F:\\Tolvuleikir\\Full\\FPS\"\\Unreal Anthology\"\\UnrealGold\\System\\Unreal.exe" so I would then be able to do: system("Unreal") and it would know that I wanted to execute the path contained by the int "Unreal".

This might sound a bit confusing but I hope you understand me. Does anybody know how to do this? Any ideas very much appriciated even if they are not total answers!!

Thanks a lot!

Recommended Answers

All 6 Replies

1. Add #include <string>
2. Add

string path;
...// get dir name into buf then
path = buf;
path += "System\\Unreal.exe";
// Now you have string path with
// "F:\Tolvuleikir\Full\FPS\Unreal Anthology\UnrealGold\System\Unreal.exe"
system(path.c_str()); // Start the application

3. Don't muddle double backslashes in source text literals with "friendly executable format".
4. I don't understand why you insert quotes (after Anthology, for example) in the result string example:

F:\\Tolvuleikir\\Full\\FPS\"\\Unreal Anthology\"\\UnrealGold\\System\\Unreal.exe" so I would then be able to do: system("Unreal") and it would know that I wanted to execute the path contained by the int "Unreal".

And you? Do you understand why?..

3. Don't muddle double backslashes in source text literals with "friendly executable format".
4. I don't understand why you insert quotes (after Anthology, for example) in the result string example:

And you? Do you understand why?..

I've learned with my little experience with C++ that you are required when you want the program to execute a path to include double slashes. like so: "F:\\Tolvuleikir\\Full\\" ... and when you want to open directories that include spaces I know you need to do this: ("FPS\"\\Unreal Anthology\"\\") or else the program thinks you want it to execute "FPS\\Unreal\\". When you do not include \"\\Example with spaces\"\\ the program sees the string as \\Example\\.

And BTW, I call it "Friendly C++ executable" format because you need to do double slashes and \"\\Example with spaces\"\\ when you want ot open a directory with spaces. Any idea how to let the program know that I want it to open a directory with spaces? I've heard you could do f.x. "C:\\Docume~\\Administrator\\Diablo~\\" and then I'd do ~ where I'd tell the program to open the directory that starts with that name. Am I correct? How does that rule work? Thanks!

P.s. Also, did you know that some games like to put flags at the end of the execute path f.x. : "C:\Program Files\Pumpkin Studios\Warzone2100\wz2100 -640" where the "-640" would stand for, run the game at 640*480, and another shortcut could say "-800" which would tell the exe to execute the game in 800*600. However, I can't do system("C:\\Program Files\\Pumpkin Studios\\Warzone2100\\wz2100 -640") because it woudl tell me that the string was invalid and if I'd put the flag after the qoute: Warzone2100\\wx2100" -640, codeblocks would call up an error. Any idea how to tell the program this is a command for the exe, not part of a string and not part of a command for the program?

Modify the same approach:

std::string cmd;
...// get dir name into buf then
cmd = "\"";
cmd += buf;
cmd += "System\\Unreal.exe\"";
// Now you have quoted exe path with
// "F:\Tolvuleikir\Full\FPS\Unreal Anthology\UnrealGold\System\Unreal.exe"
// Append cmd parameter(s):
cmd += " -800";
system(cmd.c_str()); // Start the application

It works fine with VC++ RTL system() call.

Also you may use non-standard spawn family functions from <process.h>.

Modify the same approach:

std::string cmd;
...// get dir name into buf then
cmd = "\"";
cmd += buf;
cmd += "System\\Unreal.exe\"";
// Now you have quoted exe path with
// "F:\Tolvuleikir\Full\FPS\Unreal Anthology\UnrealGold\System\Unreal.exe"
// Append cmd parameter(s):
cmd += " -800";
system(cmd.c_str()); // Start the application

It works fine with VC++ RTL system() call.

Also you may use non-standard spawn family functions from <process.h>.

Ok now I got this completely messed up :( My program:

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    string unrealpath;
    HKEY hKey = 0;
    char unreal[255] = {0};
    DWORD dwType = 0;
    DWORD dwUnrealSize = sizeof(unreal);
    const char* subkey = "Software\\Unreal Technology\\Installed Apps\\Unreal Gold";

    if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        if( RegQueryValueEx(hKey,"Folder",0, &dwType, (BYTE*)unreal, &dwUnrealSize) == ERROR_SUCCESS)
        {
            unrealpath = unreal;
            unrealpath += "\\System\\Unreal.exe";
            system(unrealpath);
        }
    }
}

When it compiles, it says: " Cannot convert 'std::string' to 'const char*' for argument '1' to 'int system(const char*)' " And ohh, where do I put those new commands you speak of,

std::string cmd;
cmd = "\"";
cmd += unreal
cmd += "System\\Unreal.exe\"";

Ohh, read my snippet again.
Look at cmd.c_str()! It's std::string to const char* conversion!

I know you are a beginner to C++ (but I think true C++ beginners do not starting from RegOpenKey stuff). They are starting from the C++ language basics.

Well, continue your C++ studies with copy/paste training:

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main()
{
    string unrealpath;
    HKEY hKey = 0;
    char unreal[255] = {0};
    DWORD dwType = 0;
    DWORD dwUnrealSize = sizeof(unreal);
    const char* subkey = "Software\\Unreal Technology\\Installed Apps\\Unreal Gold";

    if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        if( RegQueryValueEx(hKey,"Folder",0, &dwType, (BYTE*)unreal, &dwUnrealSize) == ERROR_SUCCESS)
        {
            unrealpath = "\""; // Starting quote
            unrealpath += unreal; // Append dir path
            unrealpath += "\\System\\Unreal.exe\""; // Append the last part with quote
            // Can you insert debugging print of unrealpath here?..
            system(unrealpath.c_str());
        }
    }
}

hank you very much for that. I know I should just be going ove basic things but I wanted suddenly to know something complicated :D

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.