Sunday, June 29, 2008


Microsoft's Research team maintains a project that allows you to intercept any function call. The library does this by injecting code into the memory of that process. You then have the ability to do whatever you want and if you choose pass the call through. This can be very useful for instrumentation for example.

Here's a very simple example that isn't of much use since everything is in the same process, but it shows how I can intercept the call to Window's sleep function.

// Detours Test Program (simple.cpp of simple.dll)
// Microsoft Research Detours Package, Version 2.1.
// Copyright (c) Microsoft Corporation. All rights reserved.
// This DLL will detour the Windows Sleep API so that TimedSleep function
// gets called instead. TimedSleep records the before and after times, and
// calls the real Sleep API through the TrueSleep function pointer.
#include "detours.h"

static LONG dwSlept = 0;
static VOID (WINAPI * TrueSleep)(DWORD dwMilliseconds) = Sleep;

VOID WINAPI TimedSleep(DWORD dwMilliseconds)
DWORD dwBeg = GetTickCount();
DWORD dwEnd = GetTickCount();

InterlockedExchangeAdd(&dwSlept, 3636);

//BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
int __cdecl main(int argc, char ** argv)
LONG error;

printf("simple.dll: Starting.\n");


DetourAttach(&(PVOID&)TrueSleep, TimedSleep);
error = DetourTransactionCommit();

if (error == NO_ERROR) {
printf("dwslept %d",dwSlept);
printf("simple.dll: Detoured Sleep().\n");
printf("sleep5.exe: Starting.\n");


printf("sleep5.exe: Done sleeping.\n");
printf("dwslept %d",dwSlept);
else {
printf("simple.dll: Error detouring Sleep(): %d\n", error);

DetourDetach(&(PVOID&)TrueSleep, TimedSleep);
error = DetourTransactionCommit();

printf("simple.dll: Removed Sleep() (result=%d), slept %d ticks.\n",
error, dwSlept);

return 0;

///////////////////////////////////////////////////////////////// End of File.

In order for this to work you first need to download the detours package from

Compile the source as a lib and link to it.

Give it a shot its pretty cool!


c# using statement

The C# 'using' statement provides a convenient syntax that ensures the correct use of IDisposable objects.


using (BlackJackForm blackjackform = new BlackJackForm())

The above is the equivalent of the following more verbose code:

BlackJackForm blackjackform = new BlackJackForm()
try {
finally {

The using statement only works with items implementing the IDisposable interface.

Friday, June 27, 2008

C++ War Card Game

While reading Data Structures in C++: Using the Standard Template Library (STL)

The author brought up the War card game. I thought it would be interesting challenge to see how fast I could create the game. So just for fun here is the source in case anyone is interested.

The rules are simple. Each player has half the deck they flip over the top card each time. Whichever card is higher (2 being lowest Ace being the highest) that person gets both cards. On tie each player flips over three cards and the fourth determines who wins everything.

#include <iostream>
#include <vector>
#include <queue>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
using namespace std;
struct Card {
Card(int value) {
this->value = value;
int getValue() {
return value;
int value;
struct Player{
queue cards;
struct Deck {
vector cards;
Deck() {
for(int i=1;i<14;++i) j="1;j<5;++j)" j="0;j<10;++j)" i="0;i< cards.size();++i)" int="" r="(rand()" card="" temp="cards[i];" struct="" game="" void="" deck="" player="" cout=""><< "dealing" << i="0;i<<> undecided;
int count =0;
while( >0 && >0) {
cout << ++count << ": A cards: " << acard =";" bcard =";"> bCard.getValue()) {
int size = undecided.size();
for(int i=0;i< size =" undecided.size();" i="0;i< size;++i)" else="" were="" in="" take="" three="" more="" from="" each="" player="" int="" i="0;i<3;++i)" if=""> 0 ) {
if ( > 0 ) {
int main() {
Game g;
return 0;

C++ get random number

#include // For time()
#include // For srand() and rand()
void foo() {
srand(time(0)); // Initialize random number generator.
int r = (rand() % 10) + 1;

c++ std::find algorithm example

C++ standard library has many useful features. One of which is the std::find algorithm.

Here is a simple example:
#include <iostream>
#include <algorithm>

int main() {
int data[100];
data[89] = 7;
int * where = std::find(data,data+100,7);
std::cout << *where << std::endl;

This will output '7'. The interesting part is the algorithm will work with any data structure not just arrays.

#include <iostream>
#include <algorithm>
#include <list>
int main() {
std::list<int> data;
std::list<int>::iterator where = std::find(data.begin(),data.end(),7);
std::cout << *where << std::endl;

For more information I recommend the following book:
Data Structures in C++: Using the Standard Template Library (STL)

Thursday, June 26, 2008

Using java as a shell language

I've been trying to find what shell language I like better as the 'glue' needed between real applications. I've used perl a lot in the past, experimenting with python and ruby now. But I don't want an interpreted language. I really want a compile time language.

So today I figured why not use java.

The main problem is that most scripts are easy to modify and don't require compiling. For example you start your python script with


It would be nice if I could do the same for Java. Well now you can.

Introducing the 'JavaLoader'

class JavaLoader {
public static void main(String[] args) throws Exception {
String pwd = System.getProperty("user.dir");
System.out.println( "Received argument " + args[0]);
String javaFile = args[0];
if (javaFile.startsWith("./"))
javaFile = javaFile.replace("./","");

// Find class file
String className = (javaFile.split("\\."))[0];

// Comment out the shebang line so we can compile it.
RandomAccessFile file = new RandomAccessFile(javaFile, "rw");;

// Compile it
executeCommand(String.format("javac -cp $CLASSPATH:%s %s",pwd,javaFile));

executeCommand(String.format("java -cp $CLASSPATH:%s %s",pwd,className));

// Put back #! line.
file = new RandomAccessFile(javaFile, "rw");;
public static void executeCommand(String cmd) throws Exception {
System.out.println("Executing command: [" + cmd + "]");
Process p = Runtime.getRuntime().exec(cmd);
public static void printStream(InputStream is) throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = input.readLine()) != null) {

By using this you can now write a script like so:

#!/usr/bin/java JavaLoader

class my {

public static void main(String[] args) {
System.out.println ("my: from here I am in java");

To execute you first need to make sure the JavaLoader is compiled and in your classpath. Then execute your script. My script was called so I did:

[root@pioneer local]# ./
Received argument ./
Executing command: [javac -cp $CLASSPATH:/usr/local]
Executing command: [java -cp $CLASSPATH:/usr/local my]
my: from here I am in java

This is great no more settling, I get to use Java everywhere now!

Start cygwin's x server without access control

To start cygwin's x server without access control use the following command from cygwin

xwin -multiwindow -ac &

You don't need multiwindow but I like it. The -ac is what disables access control

CUDA Hello World

CUDA is an API to Nvidia's GPU's. It allows programmers access to a parallel machine that will run faster than cpu's for particular kinds of projects. In this 3 part tutorial will first setup Visual Studio to work with Cuda. If you are not using Visual Studio you can skip to Part 2

Step 1 is to download a new template type for Visual Studio. This will setup the compiler and other settings and get you ready to write code. To do this download following wizard

Step 2 is optional, but I like to do it. This will set up syntax highlighting for your .cu (cuda) files.
In Visual Studio go to Tools->Text Editor->File Extensions and add .cu extensions to the list of cpp files.

Step 3, download the cuda SDK and driver if you have a cuda enabled card. Simply go to nvidia's webside and download what you need:

That's it, now you ready to create your first project. Continue To Part 2

Wednesday, June 25, 2008

Enumerate all hosts in a dns domain using Perl

I had a need to search for a hostname at my company. I found the Net::DNS perl module to be useful. Here is an example of enumerating all hosts for a domain.

use strict;
use Net::DNS;

my $res = Net::DNS::Resolver->new;
$res->nameservers("nameserver ip address");

my @zone = $res->axfr("");

foreach my $rr (@zone) {
print $rr->print;

Tuesday, June 24, 2008

Python and CUDA

CUDA is Nvidia's api for leveraging the power of the GPU for parallel processing. The cuda api is in C and can be daunting to use. The following how to shows how to use PyCuda to access this powerful API from your python code.

First install PyCuda. You can fetch the latest package from

Before you can use Cuda you must initialize the device the same way as you would in your C program.

import pycuda.driver as pycuda

assert cuda.Device.count() >= 1

cudaDev = cuda.Device(0)
cudaCTX = dev.make_context()

For a cuda program the basic methodolgy is to copy from system memory to devices memory, perform processing, then copy data back from the device to the system. PyCuda provides facilities to do this.

First let's create a numpy array of data that we wish to transfer:

import numpy
a = numpy.random.randn(4,4)
a = a.astype(numpy.float32)
a_gpu = cuda.mem_alloc(a.size * a.dtype.itemsize)
pycuda.memcpy_htod(a_gpu, a)

We now have our data on the device, we need to instruct the GPU to execute our Kernel. A Kernel, when talking about CUDA, is the actual code that will be executed on the GPU. PyCuda requires that you write the kernel in C and pass it to the device.

For example here is a kernel that adds one to the value of each element.

mod = cuda.SourceModule("""
__global__ void addOne(float *a)
int idx = threadIdx.x + threadIdx.y*4;
a[idx]+= 1;

Now tell the device to execute our kernel.

func = mod.get_function("addOne")
func(a_gpu, block=(4,4,1))

Lastly we copy the contents from the device back to system memory and print the results.

a_addOne = numpy.empty_like(a)
pycuda.memcpy_dtoh(a_doubled, a_gpu)
print a_doubled
print a

Other Resources:

Python web service client using soaplib

Here is an example of a python web service client using soaplib. I personally find the SOAPpy implementation better.
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.primitive import String
from soaplib.client import make_service_client
class YYY(SimpleWSGISoapApp):
__tns__ = ''
def IsValidISBN13(self,sISBN):
client = make_service_client('',YYY())
print client.IsValidISBN13('0000000000000')
print client.IsValidISBN13('9780061374227')

NTLM Authentication from Java

Connecting to a web page from Java is very easy. You use the HttpURLConnection method like so:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {

This works, but what if you need to access a password protected web page? Then you need to define an "Authenticator."
static class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("trying to authenticate");
return new PasswordAuthentication("username", "password".toCharArray());

Now before you call HttpUrlConnection you set the authenticator. The full code is displayed below:

class Notify {
static class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
System.out.println("trying to authenticate");
return new PasswordAuthentication("username", "password".toCharArray());
public static void main(String argv[]) throws Exception {
// Construct data
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("<ntlm protected="" url="">");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {


Hope that helps...

Thursday, June 19, 2008

PHP redirect

If you want to redirect to another page in php use the header function to set a new location

header("Location: redirectTo.html");

Javascript setInterval, and setTimeout

Here is how you can set a timer in javascript.




expression is a function that you want to call where interval is the time in milleseconds.

setInterval will repeatedly call the expression on the interval whereas setTimeout will call it only once.

Tuesday, June 17, 2008

Java Regular Expressions

Here is how you can use regular expression in Java to parse a file.

The file I'm parsing looks like this. Some of you may recognize this as the output of bhosts from an LSF farm.

my-host ok - 4 0 0 0 0 0

To parse this I use the following:

Pattern pattern = Pattern.compile("(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
// now holds the first one matched etc.

Java parse system command

Here is how you can parse a system command in java.

Process p = Runtime.getRuntime().exec("/usr/local/lsf/bin/bhosts -w " + hostGroup);

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null )
//Do what you want here...

Monday, June 16, 2008

Python read command line options

Here is an example of how to read command line arguments with Python.

import getopt, sys

def main():
opts, args = getopt.getopt(sys.argv[1:], ":i:r:")
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
output = None
verbose = False
for opt in opts:
if opt[0] == "-i":
id = opt[1]
if opt[0] == "-r":
root = opt[1]
print id
print root
if __name__ == "__main__":

Saturday, June 14, 2008

Java filename generation

I have a Java web application that takes user inputted values and makes a nicely formatted png image for the column heading.

I ran into an interesting problem today that I thought would be simple to solve but was rather suprised that it wasn't.

For performance reasons when I generated this png I cache it to disk, out of laziness I simply stored it with the filename the user gave. Obviously now the app is beginning to be used the user decided to enter 'MyName (8/4/8)' and my app barfed because the '/' is a special character.

This was somewhat expected and I decided to fix it real quick but was surprised that there was no way to easily escape this input.

In the end i ended up Base64 encoding the value then writing it as a string. In other words using a simple hash as the name.

So I pose the question, is there a better way to do this? Will the Base64 always return valid file name?


Chapter 10 - Managing memory and low-level data structures

The Page continues the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series)

List of key concepts from this chapter.


A pointer is a value that represents the address of an object. Each object in your system has a unique memory address. For example if you have an object 'x' then '&x' is the address of x. And if p is the address of an object then *p is the value of that object.

Pointers to functions

In addition to pointing to objects, built-in and class types, you can also point to functions.

int (*fp)(int) // A pointer to a function that takes an int as a parameter

Now if we define a function with the same signature

int next(int n) { return n+1; }

Then the following will assign the pointer

fp = &next // or fp=next works as well

fp=next works above as well since you can only take an address from a function the '&' is implicit. Similarily you can call it either way:



An array is a basic container of same objects. int[3] for example. Arrays are not class types so they have no members.

You can initialize an array when you declare it, which is something you can't do with stl containers.

const int month_lengths[] = {31,28,31,30,31,30,31,31,30,31,30,31 };

Reading and Writing Files
You can write to standard error by using either cerr, or clog.

Example to copy file from one to another

int main() {
ifstream infile("in");
ifstream infile("out");

string s;
outfile << s << endl

return 0;

Memory management
There are two distinct types of memory management: automatic and statically allocated.

Automatic memory managemt is associated with local variables. The system allocated memory when its first encountered and frees it with variable falls out of scope.

Statically allocated is associated with use of the 'static' keyword. For example

int* pointer_to_static() {
stataic int x;
return &x;

The third way is to allocate memory yourself from the heap through the keyword new.

int* p = new int(42);

Chapter 9 - Defining new types

The Page continues the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series)

There are two types in C++ built-in types and class types. Built-in types are part of the core language (int, double, char, etc) whereas class types are typically built on top of the built-in types (vector, string, user-defined classes, etc).

Chapter 9 of Accelerated C++ revisits example from previous chapters and implements them as Classes. For those of you who don't know a simple class in c++ can be declared as follows:

class MyClass {
std::string my_field;
std::string getMyField() const { return my_field;}

The above code declares a class ( all methods default to private access ) with one member field, and one assesor function.

Member Functions
As you see above our classes not only contain data but also member functions. When should you use a member function? The general rule is when the function changes the state of the object.

Accessor functions
Accessor functions are names given to member functions that serve to expose hidden fields of the object.

Constructors hold code that is executed upon object construction. More formally, "Constructors are special member functions that define how objects are initialized."

A constructor can take no arguments, known as the default constructor or it can take variables that you use for initialization.

user-defined types can be defined as either structs or classes. Structs default to always public access, where as classes start with private access.

Protection labels control access to members, both fields and function, of a class or struct. So you can start with a struct and declare parts private, or you can start with a class and decalare parts public.

Constructor initializer list is a comma separated list of member-name(value) pairs that indicate how to initialize the object.

Chapter 8 - Writing generic functions

The Page continues the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series).

C++ provides templates as an ability to focus on writing generic algorithms for any number of data types. A common example is the stl vector container. When you instantiate the vector obect you provide the type of object that the vector will contain. (vector<int> for example)

In discussing generic function this chapter also focuses on iterators and the different types: Sequential read-only, Sequential write-only, Sequential read-write, Reversible, and Random access.

Accelerated C++ Chapter 0 - Getting Started

The Page starts the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series)

Accelerated C++ is a new approach to teaching C++. It teaches the reader the most useful aspects of C++ instead of starting with teaching C. The reader is introduced to high level data structures from the start. It also focuses on solvings problems through clear examples instead of just explaining the features and libraries as most textbooks tend to do.

The first chapter of this book is named Chapter 0 in reference to zero index arrays. As with all beginning programming books it begins with the Hello World program.

int main() {
std::cout << "Hello World" << std::endl;
return 0;

This simple examples teaches a lot. It teaches proper scoping, comments, functions, return values, white space rules, and include directive. The remaining couple pages of chapter 0 explain those issues. The chapter ends with the user having written and compiled their first program and ready to move on.

Accelerated C++ Chapter 1 - Working with Strings

The Page continues the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series)

Chapter 1 immediately introduces the reader to an object by modifying the Hello World program from chapter 0 to ask the user for his name then saying Hello [user].

using namespace std;
int main() {
cout << "Enter Name: " <> name;

string name;
cin >> name;

cout << "Hello " << name << endl;
return 0;

Once again this simple example introduces a lot of new concept. Were introduced to variables, objects,definitions, interfaces, initialization, and the string object.

The String object is the most important concept from this chapter. By using it the reader is introduced to the constructor, and the idea of an interface for the object. For example the reader can now time name.size() to get the size of the object. This new knowledge is put to use to frame the name with a series of asteriks leading to the close of Chapter 1.

Accelerated C++ Chapter 2 - Looping and Counting

The Page continues the discussion of Accelerated C++: Practical Programming by Example (C++ In-Depth Series)

Loops and conditionals are an integral part of any programming language. Chapter 2 of Accelerated C++ introduces the reader to the while, if, logical operators, and constructs.

Being an experienced developer I thought the amount of detail used to explain the how to configure the while construct was a too much. However, I must assume that the author's had good reason to include so much detail as previous students of theirs must of been confused, and with loops being such an important concept I suppose the added explanation was worth it.

The author then goes on to describe the different built in types for C++:

Built-in type representing truth values; may be either true or false

Integral type that contains only non-negative values

Integral type that must hold at least 16 bits

Integral type that must hold at least 32 bits

Unsigned integral type (from ) that can hold any object's size

Unsigned integral type that can hold the size of any string

Ruby - Hello World

This Post is the first in a series of post about the Ruby programming languages.

For more information about Ruby I recommend this book: The Ruby Programming Language.

The best way to start any introduction to a new language is with the classic Hello World Program. At this point I assume you already have the ruby interpreter installed. If not you can download it from

Once you have the interpreter create a file with the following contents:

puts "Hello World!"

Save the file as helloworld.rb, then execute ruby helloworld.rb. Your output will be "Hello World!"

Perl - SOAP Client (using SOAP::Lite)

This Post shows how to use Perl as a SOAP Client to an existing web service.

For more information on Perl and WebServices I recommend: Programming Web Services with Perl

To access SOAP web services with Perl, I recommend the SOAP::Lite CPAN module. If you don't have SOAP::Lite already installed you can install it by issuing the following commands and answering any prompts.

perl -MCPAN -e shell
CPAN> install SOAP::Lite

Here is a simple example perl call that calls the stock quote webservice at

use strict;
use SOAP::Lite +trace => 'debug';
my $soap = SOAP::Lite->new();
my $serializer = $soap->serializer();
my $service = $soap->service('');

print $service->GetQuote("nvda");

Note: The recent version of SOAP::Lite appears to have a bug, when the service is a newer one like a .NET or axis the above code doesn't work. I can't find any indication that SOAP::Lite developers are going to fix this or not. If anyone has any information let me know.

C# Web Services

This Post illustrates how to write a simple C# Soap Client.

For more information about C# I recommend the following book: Programming .NET Web Services.

C# is not an interpreted language like Perl or Python, so before you can consume a web service you must first generate a stub class. In Visual Studio click Tools->Visual Studio Command Window

In the command window type wsdl this will generate a c# stub file your current directory. Copy this file into your c# project.

For an example lets use the following web service:

This will create a stub file called StockQuote.cs.

To call this service you just need the following code

using System;

using System.Collections.Generic;
using System.Text;
namespace TestWebService
class Program
static void Main(string[] args)
StockQuote stockQuote = new StockQuote();

Thats it! Make sure to include the System.web.Services references by right click on your project name and selected Add References.

Python web service using SOAPPy

Here is an example of how to call a WebService from Python. For more information on Python and Web Services I recommend the following book: Python Cookbook.

This requires the fpconst and SOAPPy package. You can download them from and Unzip the packages then run python install on each package.

The following code will then connect to the web service and execute the remote method.

import sys

from SOAPpy import WSDL

url = ''
server = WSDL.Proxy(url);
print server.IsValidISBN13(sISBN ='0000000000000')

Note there is another web service package called ZSI, I found SOAPPy to be easier to use, but from what I understand both packages are going to merge together.

Java Soap Client using axis library

This post is an example Java Soap client call using the apache axis library.

First you need to download axis from This library provides tools to convert a wsdl into a Java stub class that you can then use.

Once you've extracted the axis directory run the following command on the wsdl that you want to consume.

java -cp axis-1_4/lib/wsdl4j-1.5.1.jar:axis-1_4/lib/saaj.jar:axis-1_4/lib/jaxrpc.jar:axis-1_4/lib/axis.jar:axis-1_4/lib/commons-logging-1.0.4.jar:axis-1_4/lib/commons-discovery-0.2.jar:. org.apache.axis.wsdl.WSDL2Java

If you have your classpath set up you probably don't need as much but for simplicity I included all the jars in the axis distribution.

Now you can use the stubs that were generated

import com.daehosting.webservices.ISBN.*;

class client {

public static void main(String argv[]) throws Exception {

ISBNServiceSoapType service = new ISBNServiceLocator().getISBNServiceSoap();




For further reading I recommend the following book Java Web Services.

Ruby SOAP Client

Here is an example of how to call a web service using Ruby. This example requires no external libraries, everything is built into the language. However it does require version 1.8.5 early version don't have the create_rpc_driver method.

#Requires Ruby version 1.8.5 or highet
require 'soap/wsdlDriver'
wsdl = ''
driver =

# Log SOAP request and response
driver.wiredump_file_base = "soap-log.txt"

response = driver.IsValidISBN13(:sISBN => '0000000000000')
puts response.isValidISBN13Result

Let's examine the code a bit more.

The first part brings in the required headers, namely soap/wsdlDriver

We then declare a variable wsdl to point to the web service description. The wsdl allows the client code to understand what method are available and how to call them.

We then turn on some debugging, this is optional but if its your first time I recommend it.

drive.isValidISBN13 actually exectues our call. Based on the wsdl it knows how to serialize the call and get back the results which I store in the 'response' variable.

That's It.

I found Ruby to be one of the easiest languages to make a web call from. It doesn't require building any complex wrappers and has all the libraries built it.

If you have any trouble getting it working, leave a comment and I'll be happy to take a look.

For more information about Ruby and Web Services I recommend Ruby Cookbook (Cookbooks (O'Reilly)).

Python database access

Here is an example of how to connect to a MysqlDB using python.

import MySQLdb
import sys

class Table:
def __init__(self, db, name):
self.db = db = name
self.dbc = self.db.cursor()

def execute(self):
query = "select 1,2 from you_table "
return self.dbc.fetchall()

def next(self):
return self.dbc.fetchone()

db = MySQLdb.connect(host="<your host>",user="<your username>", db="<your db>", passwd=<your passwd>)
event_table = Table(db, "event_table")

records = event_table.execute()
for r in records:
print str(r[0])+","+str(r[1])

C++ How to trim a string

To trim a string in C++ you should use the Boost library. Writing C++ without Boost is no fun. Boost provides great libraries for almost any c++ work and its a proving ground for future revisions of the c++ standard library.

So how do you trim a string in c++ using boost?
#include <iostream>
#include <boost/algorithm/string/trim.hpp>

int main(int argc, char * argv[]) {

  cout << trim(argv[1]);

Got error 28 from storage engine query

I ran into this problem today with mysql. Took me a while to figure out that it was caused by my /tmp drive being full.

Clearing the /tmp drive solved the issue.

VB/VBA Call WebService

You can call a webservice from VBA simply by posting the soap envelope. Here is an example:
    Dim http As New WinHttp.WinHttpRequest
Dim URL As String
Dim envelope As String
URL = "http://notificationserver/NotificationServer/NotifyService"
envelope = "<?xml version=""1.0"" encoding=""UTF-8""?><soap:Envelope soap:encodingStyle="""" xmlns:soap="""" xmlns:soapenc="""" xmlns:xsi="""" xmlns:xsd="""" xmlns:tns=""""><soap:Body><tns:notify><id xsi:type=""xsd:string"">blah</id><from xsi:type=""xsd:string"">from </from><subject xsi:type=""xsd:string"">subject</subject><details xsi:type=""xsd:string"">details</details></tns:notify></soap:Body></soap:Envelope>"

Call http.Open("POST", URL)

http.SetRequestHeader "Content-Type", "text/xml"
http.SetRequestHeader "SOAPAction", " "

http.Send envelope
MsgBox http.ResponseText

undefined symbol: __pure_virtual

While trying to install mysql dbd module for ruby I encountered the following error:
/usr/local/ruby/lib/ruby/site_ruby/1.8/i686-linux/ /usr/local/ruby/lib/                                           ruby/site_ruby/1.8/i686-linux/ undefined symbol: __pure_virtual - /usr/                                           local/ruby/lib/ruby/site_ruby/1.8/i686-linux/ (LoadError)        from ruby-db-client.rb:4

After some googling I found this is a problem with mysql 5.0.22. I was able to fix it by modyfing /usr/local/mysql/bin/mysql_config and adding -lgcc to the libs argument.

After reinstalling ruby everything worked fine.

Ruby database access using mysql dbd.

Step 1: Install mysql driver for ruby.

You can download package from here

Execute: ruby extconf.rb --with-mysql-config

Then make;make install

Step 2: Write simple ruby client.
  require "mysql"
my = Mysql.connect("localhost", "", "", "test")
puts my
res = my.query("select * from tblMovies")
res.each do |row|
puts row[0]+row[1]

That's it! Now you can access a database with Ruby.

Python mysql access

Here is how you can access mysql using python:

import MySQLdb
import sys
import time

db = MySQLdb.connect(host="reportsdb",user="root", db="tool_metrics")
dbc = db.cursor()
str = "select '1','2','3'";
records = dbc.fetchall()

for r in records:
print r[0]+","+r[1]+","+r[2]

For more information on Python and databases I recommend the following book for futher reading: Python Pocket Reference (Pocket Reference (O'Reilly))

Friday, June 13, 2008

DirectX draw triangle

This is an example of how to draw a simple triangle using DirectX:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectXHelloWorld
public partial class DirectXHelloWorld : Form
//Define device to write to.
Device device = null;
// Define a vertexBuffer to store the 3d objects.
VertexBuffer vertexBuffer = null;

//Main method.
static void Main()
// Instantiate form, initialize graphics and show the form.
DirectXHelloWorld form = new DirectXHelloWorld();

// Render loop
while (form.Created)
// Called to draw screen.
public void render()
//Clear the backbuffer to a blue color (ARGB = 000000ff)
device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);


device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

//End the scene
// Initalize the graphics system.
public void InitializeGraphics()
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;

// On my chepo laptop I don't have hardward support so I'm
// using SoftwareVertexProcessing. If you have a real graphics
// card you should change this to HardwareVertexProcessing.
device = new Device(0,
new System.EventHandler(this.OnResetDevice);
OnResetDevice(device, null);

catch (DirectXException e)

MessageBox.Show(null, "Error intializing graphics: " + e.Message, "Error");
public void OnResetDevice(object sender, EventArgs e)
Device dev = (Device)sender;
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored),3, dev,

GraphicsStream stm = vertexBuffer.Lock(0, 0, 0);
CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];

verts[0].X = 150;
verts[0].Y = 50;
verts[0].Z = 0.5f;
verts[0].Rhw = 1;
verts[0].Color = System.Drawing.Color.Red.ToArgb();
verts[1].X = 250;
verts[1].Y = 250;
verts[1].Z = 0.5f;
verts[1].Rhw = 1;
verts[1].Color = System.Drawing.Color.Green.ToArgb();
verts[2].X = 50;
verts[2].Y = 250;
verts[2].Z = 0.5f;
verts[2].Rhw = 1;
verts[2].Color = System.Drawing.Color.Blue.ToArgb();



For more information on DirectX with c# I recommend the following book: <a href="">C# and Game Programming: A Beginner's Guide, Second Edition (Book & CD-ROM)</a><img src="" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />