Author Archive

Checking Windows registry to see if a program is installed

Let’s say you want to check the existence of a program that is installed on a Windows computer. Let’s also assume you have the program installed on your machine. Open the “Run…” command (Win+R for those keyboard masters) and type “regedit” to open the registry editor.

Now this is the vague part: you’ll need to find a registry item that can only exist if the program you’re looking for is installed. In the screenshot below I’m looking to see if a program called “TinyCAD” is installed. TinyCAD uses a *.dsn extension so I look for the registry entry for it and bingo!

Navigate to the registry value you want to find

Navigate to the registry value you want to find

Now leave the the registry window open for reference.

Use the following includes in the header (string and iostream are basically there if you want to use strings, which are a lot easier to manipulate than char arrays):

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

Below is the code for the search algorithm. Referencing the screenshot above the type has to match the type of the registry entry (“REG_SZ” in this case). Size can be arbitrary as it defines how big your buffer is.

unsigned long type=REG_SZ, size=1024;
char res[1024];
HKEY key;

string path;
bool found = false;

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes\\dsn\\shell\\open\\command"),
        NULL, KEY_READ, &key)==ERROR_SUCCESS){

        RegQueryValueExA(key,
                         NULL,// YOUR value
                         NULL,
                         &type,
                         (LPBYTE)res,
                         &size);
        RegCloseKey(key);

        // get the string, remove the parameter "/dde"
        string temp_path = string(res);
        int end = (int)temp_path.find('/');

        // change to string
        path = temp_path.substr(0,end-1);
    }

// check to see if the files exist by opening files that are suppose to be there
if( fopen(path.c_str(), "r") != NULL ){
    found = true;
}

This line of code tells the program where to look so be specific. Basically the first parameter (HKEY_LOCAL_MACHINE) is the main folder in the registry editor and the second parameter is the path in that folder to the registry entry you’re looking for. &key is the pointer to variable you’re looking for (which will be used later). The full explanation of how this works can be found at MSDN.

RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes\\dsn\\shell\\open\\command"),
        NULL, KEY_READ, &key)

This code is to get the value. Where the comment says “your value” is the name of the data you’re looking for. If this is NULL, it will grab the “(Default)” data. Looking at the screenshot above, if the value is “abc” then the data is “sdsdsdsdsds”. The data will be stored in the variable res as a char array (note it needs to be converted to a LPBYTE first). Then we close the registry finder. Now the char array can be converted to a string or you can leave it and use it as-is. Read the full explanation of RegQueryValueEx at MSDN.

    RegQueryValueExA(key,
                     NULL,// YOUR value
                     NULL,
                     &type,
                     (LPBYTE)res,
                     &size);
    RegCloseKey(key);

Note: RegQueryValueExA stores an ASCII value in res. Using RegQueryValueEx without the A at the end will yield a char array where every other character is a NULL character if you have the character set as Unicode or Multi-Byte. To change the character set in Visual Studio 2008 go to Project > Properties > Configuration Properties > General and change “Character Set” to “Not Set” and rebuild your project.

This code goes to the path it found and just tries to open the file. It will fail if it cannot find the file.

// check to see if the files exist by opening files that are suppose to be there
if( fopen(path.c_str(), "r") != NULL ){
    found = true;
}

That’s it in a nutshell. It really isn’t as scary as it sounds.

C++ Vectors

I really wish I knew about vectors in college instead of making a half-assed stack that we cobbled together from class lectures. Vectors simplifies things so much!

Vectors are basically a quick and easy way to use a stack. I don’t know where the name comes from, but it is pretty easy to use. Start by adding the following headers to your file:

#include <iostream>
#include <vector>
using namespace std;

Then you want to make a vector like so:

vector<dataType> myStack;

dataType is the data type of what you want to store in the stack. It can be a class or struct that you made or it could be an int, float, char, string, etc. The code is similar to an array creation:

dataType myStack[num_items];

Now, just to be safe, we should clear the vector:

myStack.clear();

This will erase everything stored in the vector and reset the size back to zero.

Let’s store something in the vector. Since it acts like a stack it uses a class member named .push_back():

dataType item0, item1, item2;
myStack.push_back(item0);
myStack.push_back(item1);
myStack.push_back(item2);

Now the awesome part about vectors is you can access the items like an array! So let’s say I want item1. I don’t have to pop twice. I can directly access and manipulate it by using an index like so:

dataType foo = myStack[1];
//
dataType bar;
myStack[1] = bar;

It is a little more complicated to pop like a stack, though, because it doesn’t save the last value inserted to something before erasing. To pop, you would have to do the following:

dataType getThis = myStack.back();
myStack.pop_back();

If you noticed above, I used the member function .back(). This quickly accesses the last item that was pushed into the stack. In fact, you can access the front (the oldest/first item pushed onto the stack) by using .front(). No more needing to guess the size to get the last item!

If you do want to know the size or check if it is empty, there are members for that as well:

int blah = (int)myStack.size();
// blah = 3 (since myStack contains item0, item1, and item2)
//
bool flag = myStack.empty();
// flag = false since it's not empty

That’s the basics of a C++ vector! If you want to learn all the members of a vector and do more advance stuff, you should read the documentation at cplusplus.com.

Strings in Visual Studio 2008

If you get this error:

error C2784: ‘std:: _String_iterator <_Elem,_Traits,_Alloc> std::operator + (_String_iterator <_Elem,_Traits,_Alloc>::difference_type , std:: _String_iterator <_Elem,_Traits,_Alloc>) “:template argument for ‘std:: _String_iterator <_Elem,_Traits,_Alloc>”could not be derived from’ const char [5].

Then you have to add #include <string> to the header. Linux doesn’t require it, but Windows needs it (especially Visual Studio).