undefined reference to IID_IPicture

I was writing a small test application to display some pictures and ran into a linker error complaining about an undefined reference to IID_IPicture. In COM interfaces are identified by a globally unique identifier string. To get the linker to pick them up you need to link to uuid.lib.

Tags: , , ,

Wednesday, May 28th, 2014 C, Cpp, Programming No Comments

Replace Non-Alphanumeric Characters in a C++ String

I needed to replace the non-alphanumeric characters in a std::string. While the Java String class has the replace() and replaceAll() methods for doing this, the std::string class has no such utility methods. Instead you can use the std::replace_if function from the Standard Template Library. Here’s some example code:

#include <iostream>
#include <string>
#include <algorithm>

int isNotAlphaNum(char c)
{
        return !std::isalnum(c);
}

int main(int argc, char* argv[])
{
        std::string s1 = "some/string/with*/nonalpha/characters+1";

        std::cout << s1 << " : ";
        std::replace_if(s1.begin(), s1.end(), isNotAlphaNum, ' ');
        std::cout << s1 << std::endl;

        return 0;
}

The third parameter of the replace_if() function is a pointer to a function that performs the desired check and returns either true or false if the input satisfies the condition. The last parameter is the character to replace the matched character, in this case a space.

This program produces the following:

$ ./replace.exe 
some/string/with*/nonalpha/characters+1
some string with  nonalpha characters 1

Tags: , , , , ,

Tuesday, December 10th, 2013 Cpp, Programming, Tips and Tricks No Comments

Convert C++ String to Lower Case (or Upper Case)

I don’t usually need to convert string case in C++ so when the need comes up I’ve usually forgotten how to do it and have to Google.

While the Java String class has toLowerCase() and toUpperCase(), C++ std::string does not have such a utility method. Instead, you need to use the std::transform() function. Here’s some example code:

#include <iostream>
#include <string>
#include <utility>

int main(int argc, char* argv[])
{
        std::string s1 = "lowertoupper";
        std::string s2 = "UPPERTOLOWER";

        std::cout << s1 << " : ";
        std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
        std::cout << s1 << std::endl;

        std::cout << s2 << " : ";
        std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
        std::cout << s2 << std::endl;

        return 0;
}

Produces the following:

$ ./case.exe 
lowertoupper : LOWERTOUPPER
UPPERTOLOWER : uppertolower

Note that while the Java toUpperCase() and toLowerCase() methods do not modify the original string, the std::transform function does.

Tags: , , , , ,

Tuesday, December 10th, 2013 Cpp, Programming, Tips and Tricks No Comments

Baby’s First Game

Baby's Fisrt Game I’ve just launched Baby’s First Game for Android on Google Play. This is a game designed specifically for infants, toddlers and preschool age children. They will have fun learning about colors, shapes, numbers and letters. The skill levels are very customizable and can grow with your child. This game was inspired by Baby Smash by Scott Hanselman and watching the games my own kids were interested in.

Three mini-games provide a fun way to get familiar with and learn to identify colors, shapes, numbers and letters. Just for Baby is targeted specifically at infants and young toddlers. It teaches action/reaction while familiarizing baby with colors and shapes. When baby touches the screen, a happy shape appears. Find It! tests your child’s ability to identify colors, shapes, letters and numbers by picking them out of a group. Pop It! is a fun way to work on your child’s hand/eye coordination while “popping” shapes as they fly across the screen.

Baby’s First Game is and always will be Ad free.

Baby’s First Game for Android is available now on Google Play:

Get it on Google Play

Tags: , , , , , , , , , , , , , , ,

Sunday, December 1st, 2013 Uncategorized No Comments

Are you infected?

Ever think you could never be a part of a botnet without knowing about it? There are millions of PCs out there infected. Most of the owners do not know about it, many don’t know enough about it to realize they have a problem and some just don’t care. But it is a serious issue.

I found some interesting activity on my website recently that really highlights how alive and well these botnets really are. I have a Wiki set up that I don’t really publicize since it really is only a collection of notes that I find convenient to have accessible online. A few months ago a spammer found it. Now, I am at fault for not initially disabling open registration which means anyone could sign up for an account and add content. Well, over a few weeks using a botnet the spammer posted about 50,000 spam articles before I noticed they were there.

I’ve since cleaned up the mess and disabled open registration. However, the botnet was still chugging away trying to create new accounts and post articles. Trying to ease the traffic I started blocking IP address ranges from the worst offending networks. This helped a lot but they were still chewing up bandwidth. Finally I just blocked everyone but my own IP address to see how long it would take before they gave up.

They are still trying harder and harder.

Monthly Traffic

So, normal traffic to my site was about 30 visitors a month. As soon as I noticed this jumped I knew exactly what happened. After I cleaned up the mess and blocked most of the offending networks my bandwidth dropped back down but I still can’t see normal visitors through the noise. Finally I gave up and just blocked everyone but my own IP address. If you try to access the Wiki now you will get a “403 Forbidden” error.

Daily Traffic

I noticed the traffic dropped significantly when people shut down the machines over the long 4th of July holiday weekend. So, your machines are infected but you don’t know or don’t care enough to figure it out and fix it? Hmmm…

Traffic Source

What did surprise me was the source of the traffic. Apparently most traffic is coming from Canada followed in second by France. USA is 4th… Why would the Canadian and French machines go down over the 4th weekend? I’m at a loss on that one.

Now, even though the entire site is blocked returning a 403 error, instead of giving up and going away they seem to be trying even harder. After I blocked everyone the hits dropped to about 160k in May. However, in June this jumped to 1.2 million! And now about a third of the way into July it’s already passed that at 1.4 million? Geesh! That’s on track for almost 5 million hits this month.

I may put it back online in a few weeks. However, the 15 byte error page takes up a lot less bandwidth than the actual site pages so we’ll see.

Tags: , , ,

Friday, July 12th, 2013 Security No Comments

Pretty Print Tables

A while ago I wrote a chunk of code to dump data to a console window in a formatted table. Something like you’d see in a SQL console when doing queries. I’ve finally gotten around to open sourcing it so here’s a description of how to use it and where to find it.

To format data into the table structure, a table must first be initialized using table_init(). The data will be output in either CSV format or a pretty printed table depending on the
value of the verbose parameter.

Rows are added to the table with the table_row() function. Each column in the row is specified by a position in the format string. For example the first format specifier is the first column, second column two, etc. The total number of columns for a table is determined by the call to table_row() with the highest number of format specifiers.

The format parameter for table_row() takes a C style format string. The acceptable format specifiers are a (mostly) subset of the printf() format specifiers with the addition of 't' for a timestamp.

Format specifiers:

specifier Output
s or S String of characters
c or C Character
d or i Signed decimal integer
u Unsigned decimal integer
o Unsigned octal integer
t Timestamp specified by a time_t value
x or X Unsigned hexadecimal integer
f, F, g or G Decimal floating point

[width].[precision] are also supported. Note that precision defaults to 0 so with floating point values it must always be specified or the value will be truncated to a whole number.

Finally, when data is done being added to the table, it is printed using the table_commit() function.

int main(int argc, char* argv[])
{
	struct table_t* table = table_init(stdout, "Test Table", 1);

	table_row(table, "%s%s", "Column 1", "Column 2");

	table_separator(table);

	table_row(table, "%s%s", "String value", "test string");
	table_row(table, "%s%c", "Character value", 'c');
	table_row(table, "%s%d", "Signed integer", -1);
	table_row(table, "%s%u", "Unsigned integer", 42);
	table_row(table, "%s%o", "Octal integer", 511);
	table_row(table, "%s%8x", "Hexadecimal integer", 255);
	table_row(table, "%s%.5f", "Floating point", 3.14159);
	table_row(table, "%s%t", "Timestamp", 0);

	table_commit(table);

	return 0;
}

Produces the following output:

$ ./table.exe 
+---------------------------------------------------------------+
| Test Table                                                    |
+---------------------+-----------------------------------------+
| Column 1            | Column 2                                |
+---------------------+-----------------------------------------+
| String value        | test string                             |
| Character value     | c                                       |
| Signed integer      | -1                                      |
| Unsigned integer    | 42                                      |
| Octal integer       | 0777                                    |
| Hexadecimal integer | 0x000000ff                              |
| Floating point      | 3.14159                                 |
| Timestamp           | Thursday, January 01, 1970, 12:00:00 AM |
+---------------------+-----------------------------------------+

The code is free under the MIT license and can be downloaded here.

Tags: , , , , ,

Monday, June 17th, 2013 C, Programming No Comments

Securing WordPress

In light of the latest attacks on WordPress sites everywhere I thought I should probably step up security a bit here. Not that I thought I had a bad password or anything but I still had the default admin account enabled. This attack is being conducted by a huge botnet of about 90,000 computers trying a large dictionary of passwords against the admin account. I wasn’t too concerned but the fact that it was targeting the admin account and seeing a huge spike in access to wp-login.php I still had an uneasy feeling in my gut.

First, the admin account is now disabled. They can target it all they want!.

Second, no one should need to access wp-login.php from anywhere in the world. So I updated .htaccess to only allow access from my IP address.

<Files "wp-login.php">
Order Deny,Allow
Deny from all
Allow from xxx.xxx.xxx.0/24
ErrorDocument 403 "http://www.google.com"
</Files>

Third, installed the Limit Login Attempts plugin for WordPress which will automatically block an IP after so many failed login attempts.

Tags: , , , ,

Tuesday, April 16th, 2013 Security, Tips and Tricks No Comments

Disabling UAC Prompt for Cygwin run.exe in Windows 7

So, every time I try to start Cygwin (actually, the cygwin XWin server) I get that ugly UAC prompt. Very annoying since this is an application I use quite frequently. Unfortunately, changing the properties to always “Run As Administrator” does not fix it.

The only way I’ve successfully managed to get it to work is through the Application Compatibility Toolkit from the Microsoft Download center.

Download and install the Application Compatibility Toolkit.

Once installed, run ACT as administrator. Right click on its menu entry while holding the Shift key and select Run as Administrator. Note that you have to run the correct version for the architecture of the target program. For instance, Cygwin is 32-bit so the 32-bit version of ACT must be used.

Application Compatibility Toolkit

This will open up a new custom database that we will edit. From here click the Fix icon in the toolbar.

Application Compatibility Toolkit

In the Create new Application Fix dialog, give it a name, the vendor is optional, and select the path to the executable. The executable we want is run.exe on my machine at C:\cygwin\bin\run.exe.

Create new Application Fix

Click next, and next again. From the Compatibility Fixes list, select “RunAsInvoker” and click Finish.

Create new Application Fix

Next save the database by clicking the save icon. You will be prompted to provide a name.

Database Name

Once it is saved we have to install it. Right click on the database in the tree and select Install.

Application Compatibility Toolkit

Installed dialog

Now you should be able to click the Cygwin XWin Server icon and it will start without prompting you. One additional issue I had was that before any of this I had created a Quick Launch icon for the XWin Server. For some reason, these changes do not take affect right away for the Quick Launch icon only. I had to delete and recreate the Quick Launch icon before I could start it that way without being prompted.

Tags: , , , , , ,

Tuesday, January 22nd, 2013 Tips and Tricks No Comments

Managing MinGW packages with mingw-get

This isn’t meant to be a comprehensive tutorial on everything you can do with mingw-get but rather a place for me to document some tips that I found Googling and felt I should consolidate here. If something here does not work for you or if you have some other useful pointers let me know and I’ll update this post.

MinGW is standard equipment on any new Windows computer I need to use which includes most of the tools I use regularly. I had a need to create zip files recently however zip and unzip are not included in the base MinGW installation. The easiest way to install these utilities is through mingw-get.

$ mingw-get install msys-zip
$ mingw-get install msys-unzip

This lead me to the question, what additional packages are actually available this way? Cygwin provides a nice GUI to pick and choose packages and of course there are the various package managers available for the many Linux distros. There doesn’t seem to be an easy way to list packages that can be installed with mingw-get. One method I found is to look in the catalog file.

$ grep "package name" /mingw/var/lib/mingw-get/data/*.xml

Or to make the output a little nicer:

$ grep "package name=" /mingw/var/lib/mingw-get/data/*.xml |
    sed 's/.*package name="\([^\"]*\)".*/\1/'

These commands will show you all the packages available whether they are already installed or not. To list packages that are already installed there is a nifty little script that can be found here. Download mingw-get-info and copy it to /mingw/bin.

$ mingw-get-info installed

This script is not an official MinGW tool and seems to only exist as an attachment to the linked bug report. From the comments on that page the script will generate a number of reports:

$ mingw-get-info all

to see current status for all available packages,

$ mingw-get-info installed

to filter on only those packages which are installed locally, or

$ mingw-get-info index

to see an index for the repository catalogue.

Tags: , , , , , ,

Friday, January 4th, 2013 Programming, Tips and Tricks No Comments

OpenGL Globe Screensaver (Update)

I’ve been running this OpenGL Globe screensaver I wrote for several years now on various machines without any issues. Until the other night when I installed it on my new laptop.

So I did a little digging and figured out what was going on. After I installed the screensaver and tried to activate it I got the following error:

LoadLibrary failed with error 1114: A dynamic link library (DLL) initialization routine failed.

Googling for information brought me to this site. Further down in the discussion they start talking about switchable graphics with the AMD drivers (which I have) and the Catalyst control center. It seems that by default screensavers are run in “Power Saving” mode with hardware acceleration disabled. Well, since this is an OpenGL screensaver that just will not do!

I open up the Catalyst control center and it’s not listed in the recent applications. I guess since it hadn’t run yet it wouldn’t show up. Of course, the Browse… button on the Other Applications box below won’t allow you to select a .scr file. So I try right clicking the .scr file to try to test it that way:

Explorer context menu | Test

Hey, it starts! So I jump back into the Control Center and there it is!

Catalyst Control Center

I set it to “High Performance” and go back to the screensaver control panel and it starts right up!

Interesting. I’m not sure if or how I could update the installer to detect and adjust these settings automatically. I’ve been running this screensaver (or previous builds of it) on various computers for several years now and this is the first time I’ve encountered an issue with it. I’m not sure if I can even consider it an issue with the screensaver or not though I’d argue either the screensaver or the installer need to be updated since I can’t really expect end users to have to jump through hoops to get my software to work.

I’m probably not going to do it any time soon however. If you have an idea for fixing this problem in an automated fashion I’d be interested in hearing it. If you’d like to play around with the installer code let me know and I’ll give it to you.

In the mean time, if you see this issue, let me know if this fixes it.

Update 3/12/2013

So, I’ve been playing around with the drivers and mucked things up enough that I had to revert. However, when I installed the original version and encountered the same problem with the screensaver described above, the above fix did not work this time! Grrr…

To get around it I had to go in and manually add it in the registry. One of the existing screensavers that actually ran without issues was listed in the Switchable Graphics dialog as shown above. I picked ribbons.scr. I opened regedit32.exe and searched for “ribbons.scr”.

Registry Editor

I edited this string value and changed the path to specify “ssglobe.scr” instead of “ribbons.scr”. When I reopened the Switchable Graphics dialog it was listed though it was marked as “Not Assigned”. I’m sure there are other registry settings that can be updated to add it and give it the proper settings all in one shot. However, getting it to show up in the dialog is sufficient for now as the settings can then be changed from there. I set it back to “High Performance” and all is again working.

Further reading:

Tags: , , , , , , , ,

Thursday, December 6th, 2012 Graphics, OpenGL, Tips and Tricks 2 Comments