Oct 22, 2009

Another good reason not to run program by its name in Windows

When searching for executable by its name Windows always looks in:
  • Current working directory
  • Windows system directory (use GetSystemDirectory() to obtain it)
  • Windows directory (use GetWindowsDirectory() to obtain it)
  • Directories listed in PATH environment variable
This list (I will refer to it as search directories) can be expanded with more directories when performing specific tasks.
If name does not contains file extension (for example notepad or attrib), Windows will use glob "name.*" to find file with extension that is listed in PATHEXT environment variable.

It's okay if you need just to run notepad or mspaint from command line. But what if you need to execute some program many times? Well, running it by its name is definitely not your choice. I'll try to explain why.

Oct 21, 2009

Dangerous logging

Today I will try to scare you:)
The most common realization of logging is something like that (simplified):
#include <stdio.h>

#ifdef _MSC_VER // workaround for MS VC
#define snprintf(b, bsz, f, ...)                \
        _snprintf_s(b, bsz, _TRUNCATE, f, __VA_ARGS__)
#endif
#define LOG_LEVEL 2
#define LOG(lvl, ...) do {                       \
    if (LOG_LEVEL < lvl) break;                  \
    char b[1024];                                \
    snprintf(b, sizeof(b) - 1, __VA_ARGS__);     \
    b[sizeof(b) - 1] = 0;                        \
    printf("%i! %s\n", (int)(lvl), b);           \
} while(0 == __LINE__)

Oct 5, 2009

Whether this windows binary is 32- or 64-bit?

The fastest way to answer on this question without using special software tools is:
  1. Open windows binary file in any text/hex editor (notepad is enough)
  2. Find first occurrence of "PE" string (it will be probably placed at offset 0xD0, 0xF0 or something like that after phrase "This program cannot be run in DOS mode")
  3. Skip 2 non-printable symbols (both '\0')
  4. Look on the symbol you are at:
    • 'L' - i386 binary (x86)
    • 'd' - amd64 binary (x64/x86-64)
    • '\0' (zero) - ia64 binary
p.s. You can use that method for any PE binary (like .exe, .dll and others)

Oct 3, 2009

Games: static stuff

Even the most simple thing can confuse sometimes. Usually this happens because of lack of particular experience. How to obtain such experience? Well, of course to play!

I hope it will be not the last post in that blog. Under the heading "Games" I am going to talk about things that you have always wanted to try, but because of laziness or high employment always were afraid to do it. So, let's play!

Today I want to play with static data in C++.

Oct 1, 2009

Ping Post

Meaningless post just to see that all that stuff called blogger.com works.
I don't have any super original idea for the first post, so let it be just a "ping" post.