Writing and getting variables from Windows registry

MFC makes it easy to use the Windows Registry to store your application’s settings and variables. This allows your application to retain last used options, recent files, preferences, colors, fields, whatever. I’m assuming that you can use Visual Studio’s new project wizard to make a new MFC application/project.

I am using Visual Studio 2008 SP1 on Windows Vista 32-bit as of the writing of this article.

In the main cpp file that is generated, find the InitInstance() function. In this function, there should be a function call to SetRegistryKey. This function takes in a LPCTSTR (which is a long pointer to string) and you can just give it any string (Microsoft recommends using the company name here). I would use it like this:

SetRegistryKey(_T("My Fookin Company"));

The “_T()” part is just a macro that converts the string to a LPCTSTR. This line is all you need to set your program to use the registry. If it isn’t in InitInstance(), then just add it.

To save information to the registry, you would use the following code:

// get the current application object
CWinApp* pApp = AfxGetApp();

// This is the string I want to save
CString engrName = "Alex";
int age = 24;

// "ArbitrarySectionName" is the collection of information to store (if you have multiple sections in your application such as "userProfile" and "userSettings" then you would use those different names)
// _T("engrName") is a variable in the registry. you create this here so you can name it "foobar" for all it cares
// just remember what you named it so you can get it later
// engrName (the last parameter) is what you're saving (in this case, it is the string "Alex")
pApp->WriteProfileString(_T("ArbitrarySectionName"), _T("engrName"), engrName );

// let's just save an integer for the hell of it
pApp->WriteProfileInt(_T("ArbitrarySectionName"), _T("engrAge"), age);

// not using pointer anymore
pApp = NULL;

To get information from the registry, you would have needed to save information there in the first place. If there is nothing there, you would get blank information. To get the information from the registry you would use the following code to get it:

// get the current application object
CWinApp* pApp = AfxGetApp();

// get string from registry and assign to a string
// GetProfileString returns a CString object
// GetProfileInt returns an integer
// "ArbitrarySectionName" is where the collection of information is stored (you set this on save)
// "engrName" and "engrAge" are the variables in the section "ArbitrarySectionName" where you saved single pieces of data
CString strVar = pApp->GetProfileString(_T("ArbitrarySectionName"), _T("engrName") ) );
int intVar = pApp->GetProfileInt(_T("ArbitrarySectionName"), _T("engrAge") ) );

// not using pointer anymore
pApp = NULL;

You can use both pieces of code anywhere in your MFC classes. Just note there are only functions for integers and strings. None for decimal numbers, unfortunately. For more information, here are some links to MSDN:

SetRegistryKey()
WriteProfileInt()
GetProfileInt()
WriteProfileString()
GetProfileString()
Article that I used to figure out how to use the registry

Formatting and Styling Strings (C/C++)

So, talking about formatting and styling strings for Java/Android has got me thinking about formatting and styling strings for C/C++. For C/C++, we use printf and sprintf. Both printf and sprintf work exactly the same except sprintf puts the output to a character array and printf puts the output to standard output (such as the console/terminal).

If you just want to put a line out to standard output, you’d write something like this:

printf("Hello World!\n");

If you want to output strings and numbers, you’d use it like this:

float freqMHz = 100.2578;
printf("%-30s: %5.2f MHz\n", "Frequency", freqMHz);

The previous example is a bit more advanced, but that’s probably as advanced as it gets. %-30s means the first parameter is a string (the “s”) and we want to allot 30 spaces for the string and left align it (the “-“). The %5.2f means the second parameter is a float and we want to format it such that we want 5 digits for the integer part with a 2 digit precision for the decimal part. The string sent to standard output would be as follows:

Frequency:                    100.26 MHz

Note that the float gets rounded up when it is formatted. For sprintf, the syntax is the same, except we add a character array to the parameters. The character array is where we would save the final string. For example, the following code will output the exact same thing as the above example:

char* outputStr = new char[1024];
sprintf(outputStr, "%-30s: %5.2f MHz\n", "Frequency", freqMHz);
printf(outputStr);

The cool thing about printf and sprintf is that you can add as many parameters as you want to the formatted string. The useful thing about printf and sprintf is that you can mix and match data types (like in the previous examples) without having to do typecasting or using (with cout) to get it to look nice. You can even format numbers to hexadecimal or scientific notation! Pretty slick, right?

Also, remember to add a newline character (“\n”) to the end of the string. It will run the next printf on the same line as the previous printf if you don’t.

Anyways, for all the different flags you can use to format printf and sprintf, check out the documentation at cplusplus.com.

Formatting and Styling Strings (Java/Android)

So, let’s say you’re reading the Android document on formatting and styling strings and come across the line

Hello, %1$s! You have %2$d new messages.

The document explains that %1$s and %2$d are a string and decimal value, respectively. The next chunk of code is

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Now, how does this relate? Well, the 1$ and 2$ means first and second parameter, respectively. So in this case %1$s is replaced with the variable username and %2$d is replaced with mailCount.

Why should you construct strings this way instead of constructing a string with the plus sign? Well, either way is valid, but this is just the “old” way of constructing strings. There was a time where using the plus sign to construct strings required converting decimal numbers to a string or else the compiler would complain. It was an obnoxious intermediate step. With Java though, the plus sign is smart enough to change the datatype of a decimal to a string if the rest of the line contains strings. To make a string (that contains a number) to a decimal, just add “+ 0” to the end and Java will take care of it. Pretty slick, right?

DialR: Texas Edition

We’ve released a special version of dialR for those of you who absolutely love Texas! Check out the screenshots at the website below:

Oh, and dialR is fun to say with a Southern accent: DAH-Ler (sounds like dollar). ūüėÄ

Services and notifications, just what you always wanted!

So have you ever wondered how to get a cool notification to show up in your app for something? Or have you ever wanted to know what a “service” in android was, or just something as simple as seeing some basic, example, skeleton code with some intuitive comments and variable names? Well, thats what I have produced for you! Some of this was copied from the Android site, some of this was created by some friends of mine, but all in all, its very basic stuff.

Download the code here. Just create a new project based off of this file and go nuts! The screenshots below show what it should generally look like when you run it!

Services are a great way to add some background processing to your applications. Services can almost be thought of as an Activity (which is what normal android programs are) but without a user interface. This means everything it does is in the background! Have a background service that sends a text message to your friend every 10 minutes, or have a service that creates a toast every 15 minutes to remind you how awesome you really are. This skeleton code gives you the framework to do just that!

If you found this example helpful, or would like to share how you utilized this in your own programs, post in the comments below, can’t wait to hear!

-Kevin Grant

I wanna be an engineer, so frickin’ bad

You said it brother.

New Stats!

We’ve been slacking here. ¬†We apologize. ¬†There is no excuse for this¬†insubordinance. ¬†This week look forward to our details of our experiences participating in the “Facebook Hackers Cup”, tutorials on Java Callback Functions, Services, Notifications, and Alarms! ¬†There’s so much we want to share, bear with us while we try to document it all!

In the meantime, I would like to share our current app stats for Milestone 2 with you!

Category Total Change
Total app downloads (1/21/2011) 40,498 (+19,458)
Avg user rating 3.67 stars (205 / 379 5-star ratings) (-.06)
Avg downloads / day since last milestone 540.5 (+346.5)
Avg downloads / day since launch (144 days) 281.2 (+137.2)
Avg income / day $.79 USD (+.79)

If you want to see how we’ve been progressing, check out our stats from Milestone 1.

If you haven’t seen our new app portal, check out phamous-apps.com.

Thank you everyone who has purchased Golden AP, your donations are immensely appreciated and we look forward to keep delivering you awesome apps throughout the rest of 2011.

-Kevin Grant

Limited Time Offer for Samsung Galaxy Vibrant owners and more!

Do you have a Samsung Galaxy Vibrant, or another Android Phone that has Mobile Access Point, and you wish that Mobile AP worked on your phone?

So do we! ¬†For this limited time offer, we are offering Vibrant and other proven¬†disfunctioning¬†model owners a free copy of our donate version of “Golden AP” (the paid version of Mobile AP) to those who can provide us log files to make Mobile AP compatible with your phone!

If you would like to participate, please send an email to support@phamous-apps.com and we will send you instructions on how you can help and then receive the newest version of Golden AP!

-Kevin Grant

We have a new home and a new app!

Just a quick announcement: the phamous-apps network is now active for easy access to our Android apps and this blog. Bookmark or remember the following link: http://phamous-apps.com/

dialR is a rotary phone app for Android

I’d also like to announce the first release of our new app dialR! dialR is a rotary phone app for Android. You can make phone calls with it. Like the old rotary phones, it doesn’t have speed dial. Unlike old rotary phones, if you mis-dial a digit, you don’t have to start over! You also get a set of “stickers” to put on the dial and the ability to change the background. We plan on releasing more stickers and backgrounds in the future. Download this app if you are a modern hipster who eschews the phone’s default dialer because it is way too modern and mainstream. It also adds a touch of class and retro-ness to your 21st century marvel! More information at http://dialr.phamous-apps.com/

Android: Gecko-Indoor Positioning Calibration tool for developers

So, any developers out there interested in jumping into this indoor positioning market? ¬†Well you’re not alone and a small company from Sweden called Qubulus wants to help facilitate your creativity.

Our aim is to support developer networks wherever they are to adopt indoor positioning to their projects, they are our most important customers.
‚ÄĒ Ant Maran

Today, they released an application called “Gecko” that allows you to calibrate indoor environments for indoor¬†positioning, with the ability to incorporate this positioning into your own personal applications, entirely for free! ¬†The calibration process is a little time consuming (according to their FAQ about 2 hours for an office sized building), and you do have to work tightly with the Qubulus development team to get all of the positioning properly generated and ready for use, but once finished, the results produced are quite good considering there is no need for installing external hardware.

Check out the video below, and to get started, just set up a developers account over at http://www.loclizard.com

Leave some comments about your experiences with this, and we will share ours soon as well!


http://www.loclizard.comm
http://www.qubulus.com