windows: finding GUID of self

January 22nd, 2010 thomas No comments

so in the previous post i created a utility to get some exe’s and .pdb’s file GUID’s. However i need to get this information for the process that is currently executed (self). So what do we do? We apply they same technique as before and just preset the filename with the current executable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include "exeguid.h"
void  printGuid(GUID &g)
{
    char buf[120];
    sprintf(buf,"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", g.Data1,g.Data2,g.Data3,UINT(g.Data4[0]),UINT(g.Data4[1]),UINT(g.Data4[2]),UINT(g.Data4[3]),UINT(g.Data4[4]),UINT(g.Data4[5]),UINT(g.Data4[6]),UINT(g.Data4[7]));
    printf("%s\n", buf);
}

int _tmain(int argc, _TCHAR* argv[])
{
   
    GUID g;
    if(!getmyGUID(g))
        printGuid(g);
    else
        printf("error getting my GUID\n");
   
    return 0;
}

sounds easy, eh? :)

so to proof its working:

>SelfGUID.exe
8b73f207-5509-4b7d-82c9-a4979208416f

executed the same binary again: GUID stays the same:

>SelfGUID.exe
8b73f207-5509-4b7d-82c9-a4979208416f

rebuild the binary two times and executed it after building:

>SelfGUID.exe
68ce44c6-5ca8-431d-a29b-5480cf7e53ac
>SelfGUID.exe
b91fae19-e200-4207-8468-a4ca54581949

EDIT: removed unused dependency from the project (zip updated)
Source code and executable: selfGUID.zip

Categories: coding, windows Tags:

windows GUID’s and debug builds

January 22nd, 2010 thomas No comments

So i was experimenting with windows and the debug functions and needed a fast utility that will give me the GUID’s for executables and .pdb files. This GUID is used to compare if the executable and the .pdb file fit together. (note: only in most recent [VS2008] versions, before a timestamp was used)

So with this little handy utility you can get the executable GUID:

>GUIDTool.exe exe RoR.exe
f8d102be-40e0-4409-8d94-c97e0bb4fce0

and its fitting .pdb file:

>GUIDTool.exe pdb ror.pdb
f8d102be-40e0-4409-8d94-c97e0bb4fce0

Please note that 99% of the code was copy-pasted together – origins are still in the source files.

Source code and executable: GUIDTool.zip

Categories: coding, windows Tags:

benchmarking std::vector

January 12th, 2010 thomas No comments

so i always wondered whats the fastest way is to iterate over a vector. So with this little snippet you can find out for yourself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// some simple std::vector benchmark tool
// Jan 2010, thomas{AT}thomasfischer{DOT}biz
// also see http://stackoverflow.com/questions/776624/whats-faster-iterating-an-stl-vector-with-vectoriterator-or-with-at
#include <stdio.h>
#include <vector>
#include "Timer.h"

#define VECSIZE 500000


typedef struct foo_t {
    char tmp[2];
} foo_t;

int main(int argc, char **argv)
{
    int amount = VECSIZE;
    int tests = 10;
    printf("testing with %d elements a %d bytes (%0.2f MB) and %d runs per test\n", amount, sizeof(foo_t), (amount*sizeof(foo_t))/1024.0f/1024.0f, tests);
   
    double time = 0.0f;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        vec.reserve(amount);
        foo_t f; // with random data in it
        Timer *t = new Timer();
        for(int i=0;i<amount;i++)
            vec.push_back(f);
        time += t->elapsed();
    }
    printf("add time (resized before): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        foo_t f; // with random data in it
        Timer *t = new Timer();
        for(int i=0;i<amount;i++)
            vec.push_back(f);
        time += t->elapsed();
    }
    printf("add time (dynamic resize): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        foo_t f; // with random data in it
        for(int i=0;i<amount;i++)
            vec.push_back(f);

        Timer *t = new Timer();
        for(std::vector<foo_t>::iterator it=vec.begin(); it!=vec.end(); it++)
        {
            // some example usage
            it->tmp[1] = 0;
        }
        time += t->elapsed();
    }
    printf("iterate time (version #1): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        foo_t f; // with random data in it
        for(int i=0;i<amount;i++)
            vec.push_back(f);

        Timer *t = new Timer();
        std::vector<foo_t>::iterator vecEnd = vec.end();
        for(std::vector<foo_t>::iterator it=vec.begin(); it!=vecEnd; ++it)
        {
            // some example usage
            it->tmp[1] = 0;
        }
        time += t->elapsed();
    }
    printf("iterate time (version #2): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        foo_t f; // with random data in it
        for(int i=0;i<amount;i++)
            vec.push_back(f);

        Timer *t = new Timer();
        for(int i=0; i<amount; i++)
        {
            // some example usage
            vec[i].tmp[1] = 0;
        }
        time += t->elapsed();
    }
    printf("iterate time (version #3): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        std::vector<foo_t> vec;
        foo_t f; // with random data in it
        for(int i=0;i<amount;i++)
            vec.push_back(f);

        Timer *t = new Timer();
        for(unsigned int i=0; i<vec.size(); ++i)
        {
            // some example usage
            vec.at(i).tmp[1] = 0;
        }
        time += t->elapsed();
    }
    printf("iterate time (version #4): %f\n", time/tests);


    time=0;
    for(int i=0;i<tests;i++)
    {
        foo_t vec[VECSIZE];
       
        Timer *t = new Timer();
        for(int i=0; i<amount; ++i)
        {
            // some example usage
            vec[i].tmp[1] = 0;
        }
        time += t->elapsed();
    }
    printf("iterate over array (version #1): %f\n", time/tests);

    time=0;
    for(int i=0;i<tests;i++)
    {
        foo_t *vec = (foo_t *)malloc(sizeof(foo_t) * amount);
       
        Timer *t = new Timer();
        for(int i=0; i<amount; ++i)
        {
            // some example usage
            vec[i].tmp[1] = 0;
        }
        time += t->elapsed();
        free(vec);
    }
    printf("iterate over array (version #2): %f\n", time/tests);

    return 0;
}

to compile, add the Timer.h from my previous post :)

which results in the following output on my windows machine (/Oi /O2)

testing with 500000 elements a 2 bytes (0.95 MB) and 10 runs per test
add time (resized before): 0.005281
add time (dynamic resize): 0.007693
iterate time (version #1): 0.004180
iterate time (version #2): 0.004107
iterate time (version #3): 0.000903
iterate time (version #4): 0.004891
iterate over array (version #1): 0.000277
iterate over array (version #2): 0.000773

what are your results?

Categories: coding, cpp, funstuff Tags:

cross platform timer class

January 12th, 2010 thomas No comments

So, as it seems there is no usable class in boost which can measure time down to milliseconds, so i rolled my own. (in fact i was a bit shocked that the timer class that ships with Boost only measures used CPU time) I tested it under linux and windows, and its working well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// simple Timer class, Jan 2010, thomas{AT}thomasfischer{DOT}biz
// tested under windows and linux
// license: do whatever you want to do with it ;)
#ifndef TIMER_H__
#define TIMER_H__

// boost timer is awful, measures cpu time on linux only...
// thus we have to hack together some cross platform timer :(

#ifndef WIN32
#include <sys/time.h>
#else
#include <windows.h>
#endif

class Timer
{
protected:
#ifdef WIN32
    LARGE_INTEGER start;
#else
    struct timeval start;
#endif

public:
    Timer()
    {
        restart();
    }

    double elapsed()
    {
#ifdef WIN32
        LARGE_INTEGER tick, ticksPerSecond;
        QueryPerformanceFrequency(&ticksPerSecond);
        QueryPerformanceCounter(&tick);
        return ((double)tick.QuadPart - (double)start.QuadPart) / (double)ticksPerSecond.QuadPart;
#else
        struct timeval now;
        gettimeofday(&now, NULL);
        return (now.tv_sec - start.tv_sec) + (now.tv_usec - start.tv_usec)/1000000.0;
#endif
    }

    void restart()
    {
#ifdef WIN32
        QueryPerformanceCounter(&start);       
#else
        gettimeofday(&start, NULL);
#endif
    }
};

#endif //TIMER_H__

how you use it:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include "Timer.h"

int main(int argc, char **argv)
{
    Timer *t = new Timer();
    Sleep(1000);
    printf("time gone: %f\n", t->elapsed());
    return 0;
}

which results in my windows machine into this output:

time gone: 0.999699
Categories: coding, cpp, tutorials Tags:

IP WHOIS query via python

January 7th, 2010 thomas No comments

this is a simple example (based on some 2006 rwhois code) how to get further information about an IP address:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os, sys, string, time, getopt, socket, select, re, errno, copy, signal

def queryWhois(query, server='whois.ripe.net'):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while 1:
        try:
            s.connect((server, 43))
        except socket.error, (ecode, reason):
            if ecode==errno.EINPROGRESS:
                continue
            elif ecode==errno.EALREADY:
                continue
            else:
                raise socket.error, (ecode, reason)
            pass
        break

    ret = select.select ([s], [s], [], 30)

    if len(ret[1])== 0 and len(ret[0]) == 0:
        s.close()
        raise TimedOut, "on data"

    s.setblocking(1)

    s.send("%s\n" % query)
    page = ""
    while 1:
        data = s.recv(8196)
        if not data: break
        page = page + data
        pass

    s.close()
   
    if string.find(page, "IANA-BLK") != -1:
        raise 'no match'
       
    if string.find(page, "Not allocated by APNIC") != -1:
        raise 'no match'
   
    return page
   
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "usage: %s <IP address>" % sys.argv[0]
        sys.exit(1)
    ip = sys.argv[1]
   
    for server in ['whois.arin.net', 'whois.ripe.net', 'whois.apnic.net', 'whois.lacnic.net', 'whois.afrinic.net']:
        try:
            res = queryWhois(ip, server)
            print '======', server
            print res
            break # we only need the info once
        except:
            pass

just run it with the IP as argument

Categories: coding, python Tags:

useful c++ assert macro snippets

December 31st, 2009 thomas No comments

i just coded on this little bit, and i thought it might be worth to share the information as an example of how to roll your own assert, how to jump into the debugger and how to add log messages with proper calling information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// this is the master swith to debug the stream locking/unlocking
#define DEBUGSTREAMFACTORIES

#define OGREFUNCTIONSTRING  String(__FUNCTION__)+" @ "+String(__FILE__)+":"+StringConverter::toString(__LINE__)

#ifdef DEBUGSTREAMFACTORIES
# define LOCKSTREAMS()       do { LogManager::getSingleton().logMessage("***LOCK:   "+OGREFUNCTIONSTRING); lockStreams();   } while(0)
# define UNLOCKSTREAMS()     do { LogManager::getSingleton().logMessage("***UNLOCK: "+OGREFUNCTIONSTRING); unlockStreams(); } while(0)
# ifdef WIN32
// __debugbreak will break into the debugger in visual studio
#  define MYASSERT(x)       do { if(!x) { LogManager::getSingleton().logMessage("***ASSERT FAILED: "+OGREFUNCTIONSTRING); __debugbreak(); }; } while(0)
# else //!WIN32
#  define MYASSERT(x)       assert(x)
# endif //WIN32
#else //!DEBUGSTREAMFACTORIES
# define LOCKSTREAMS()       ((void)0)
# define UNLOCKSTREAMS()     ((void)0)
# define MYASSERT(x)         ((void)0)
#endif //DEBUGSTREAMFACTORIES

also, you might enjoy this very well written tips and tricks for asserts: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/

btw, happy new year ;)

Categories: coding Tags:

debugging crashing applications on customer’s windows

September 13th, 2009 thomas No comments

this snipped using the windows debugger (shipped with win 2k and after) was very helpful in tracking a problem:

set _NT_DEBUG_LOG_FILE_OPEN=debug-log.txt
ntsd -v -c "kb;q" .exe

also, you might want to read this awesome article:
http://blogs.msdn.com/pfedev/archive/2008/09/26/all-the-ways-to-capture-a-dump.aspx

and generally:
http://blogs.msdn.com/pfedev/

have fun debugging :)

Categories: coding, windows Tags:

my first real publication :D

September 12th, 2009 thomas No comments

“A Pattern for Secure Graphical User Interface Systems” by “Thomas Fischer and Ahmad-Reza Sadeghi and Marcel Winandy”
in “3rd International Workshop on Secure systems methodologies using patterns (SPattern’09)”. Publisher: IEEE

Categories: university Tags:

how to put several pdf pages onto one page

August 26th, 2009 thomas No comments
1
2
emerge -av app-text/pdfjam
pdfnup --nup 2x2 --column true --frame true your_pdf.pdf
Categories: website Tags:

microsoft XNA: whats the downfall?

August 17th, 2009 thomas No comments

read this interesting article to get to know: http://www.xboxist.com/xbox-360/games/xna-community-games-is-a-failed-experiment-010122.php

nice quote:

As Fouts revealed in his blog post, Microsoft takes a base cut of 30% plus an additional 10-30% for advertising. Simply put, 40-60% of profits goes to Microsoft, and the remainder to the developers.

Categories: website Tags: