12/26/2007

VS2005 and VS2008 .sln file icon incorrect

I had installed VS2005 and VS2008 on Vista Business, both work fine. But today, I found .sln file icon losted and cannot open it with double-click. So I associate .sln with Visual Studio Version Selector, it works again, but the file icon is incorret.

I spent all the afternoon to make a ton of mistakes, the final solution is: associate .sln with <osroot>\Program Files\Common Files\microsoft shared\MSEnv\VSLauncher.exe.

The headache had been resolved, but I cannot find the reality. The conclusion is more bigger and more danger.

12/25/2007

Pseudorandom number generation enhanced in VS2005

1. Define the _CRT_RAND_S macro before the #include <stdlib.h> .
2. Does not need call srand function to generate the seed.
3. rans_s now has returned value which is type of errno_t, zero if successful.
4. Use this formula: dst_num = (unsigned int)((double)rand_num / (double)UINT_MAX * rand_max + rand_min) to specify the range[rand_min, rand_max] .


12/24/2007

程序员版<东邪西毒>

1. 每个programer都会经过这个阶段,见到一个program,就想知道program后面是什么。我很想告诉他,可能debug到program后面,你会发现没什么特别。回望之下,可能会觉得这一边更好。但我知道他不会听,以他的性格,自己不debug过又怎会甘心?

2. 去一个没去过的地方,希望可以闯出一个名堂,如果你以后在江湖上听到一个从不debug的英雄,那么一定是我。

3. 其实写一个program不是很容易,不过为了生活,很多人都会冒这个险。

4. 我很奇怪为什么会有C#,Anders Hejlsberg说人最大的烦恼就是记性太好。如果什么都可以忘了,以后的每一天都将会是一个新的开始。你说,那多开心。

5. 你知道programming和debugging的区别吗?debug越debugging越暖,program会越programming越寒。

6. 也许太久没有看program了,翌年的春天,我去了那人(Bill.Gates)的公司,我觉得很奇怪,那里根本没有program。

7. 他虽然是一个落泊的programer,但他的生活很有规律,每天都会来这里喝一杯酒,吃两碗饭,到月亮下山的时候他就会走。

8. 今年五黄临太岁,到处都是newbie。有newbie的地方一定有麻烦,有麻烦......,那我就有生意。我叫JoeM,我的职业就是帮助别人解除烦恼。

9. 在我出道的时候,我认识了一个人。因为他喜欢在computer边出没,所以很多年之后,他有个绰号叫Newbie。

This post is migrated from <Holy Joe's csdn blog at 9/28/2005>.

12/16/2007

A small example of compute Fibonacci's formula statically in C++

static unsigned int const value = Fibonacci<N-1>::value + Fibonacci<N-2>::value;
};

template<>
struct Fibonacci<1> {
static unsigned int const value = 1u;
};

template<>
struct Fibonacci<2> {
static unsigned int const value = 2u;
};

int main(int argc, char *argv[]) {
unsigned int v3 = Fibonacci<3>::value;
unsigned int v9 = Fibonacci<9>::value;

return (0);
}


12/11/2007

A sealed class in C++

I want a 'sealed' class in native C++ just like 'sealed' in C++/CLI and C# or 'final' in Java. I googled it then got following solution.

template<typename T>
class DeriveLock {
private:

DeriveLock() {}
~DeriveLock() {}
friend T;
};

class SealedClass : virtual public DeriveLock<SealedClass> {
};

You can found above sample code at Bjarne Stroustrup's article.

11/15/2007

It is upon the Trunk that a gentleman works

首先,我是一个纯血统的汉人,其次也是古典汉学的爱好者。虽然对孔子的经典涉猎不多,但至少比寻常的高鼻深目非我中土人氏耳熟。昨晚在翻阅svn-book时碰到一件怪事,在读到第4章(Branching and Merging)时,眼前忽然一花,似乎略微看到几个汉字,心道:我的OS是英文版的,svn-book也是,怎么可能出现汉字?!因此,不敢大意,遂凝神细读,原来svn-book的作者将孔子的一句经典作为了第4章的眉批了。

原文是以下粗体部分:

"君子务本(It is upon the Trunk that a gentleman works.)" -- Confucius

注意在上句中的Trunk是首字母大写的,Trunk在版本控制系统中是特定术语,可以不负责任地将其译作:主线或主干。而此眉批出自<论语.学而>,原文为:"子曰: 其为人也孝弟,而好犯上者,鲜矣; 不好犯上,而好作乱者,未之有也; 君子务本,本立而道生。孝弟也者,其为人之本欤!" 而此眉批恰如其分、一语双关的点出了第4章的主旨,堪称妙绝。虽然,svn-book中妙语接踵(这在技术文档中罕见),而其它章节却皆无眉批,因此第4章"君子务本"的眉批显得就格外抢眼了,也正因为它处皆无此处独有,方显得作者的手段(注: 不悟者可细品之,细品之后还不悟者,非吾之罪也)。

注: 文中提及'古典汉学'爱好者,而不说当下时髦用语'国学'爱好者,原因大体有二:
1. 所谓'古典汉学',本之于中,放之四海皆准,未闻有'国'者,'国学'一说,盖出自满清之后,为何?!;
2. 不明所以,打着'国学'奉旨乞食的招牌,到处贩卖祖产,真有伤国体也。

10/31/2007

LNK2028, LNK2019 without extern "C"

I wrote a Win32 dll in VS2005 and a CLR-Console client, the CLR-Console client is the caller of Win32 dll. In the Win32 dll, using /TC compiler option to build it and using /clr option to build the CLR-Console client. The all things is fine, such as 'dllexport/dllimport', 'Additional Include Deirectories' and 'Additional Dependencies'. But I got LNK2028 unresolved token and LNK2019 unresolved external symbol, the reason is I forgot using the extern "C" to include all dllexport functions.

10/23/2007

Reversing string in C

There is a example for reversing string in the Section 3.5 of K&R's 'The C Programming Language' . The example code look like this:
void reverse(char s[]) {

char c;
int i, j;

for (i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
c = s[i], s[i] = s[j], s[j] = c;
}
}

But the above code can be improved. There is 'Pointer Version':
void reverse(char *s) {
char *l = s;
char c;

while (*s++)
;
s -= 2;

while (l < s) {
c = *l;
*l++ = *s;
*s-- = c;
}
}

The *strrev* in C standard library implements the same functionality, but generally implements in assembly code.

10/20/2007

Item not found: Could not find this item! on Vista

When u try to remove a directory under the Vista GUI, u'll get 'Item not found: Could not find this item'. Or, u'll get 'The system cannot find the file specified' under the Vista command prompt.

Try the following:
1. Open the command prompt.
2. Execute *dir /x* to display 8.3 file name
3. Execute *rd filename* the filename 8.3 format that u got it in step 2.

10/18/2007

The #end joke

I found a joke when I publish my blog through the gmail. Wrote a blog which includes a piece of C/C++ code. Within that, the *# e n d i f* C/C++ directive had been used. So after send my blog, I got truncated blog content, Ha.

Because, u can use the *# e n d* flag in ur gmail in order to indicate blogger to truncate the content after *# e n d* flag. It's a joke.

Organize IA-32 machine code in C++ class

First, look the following code:

#include <iostream>

#pragma pack(push, 1)
class MachineCode {
public:
unsigned char jmp_;
unsigned long proc_;
};
#pragma pack(pop, 1)

typedef void (__stdcall *ProcCall)(void);

class Adapter {
public:
int Init(void* proc, void* pthis) {
thunk_.jmp_ = 0xE9;
thunk_.proc_ = (unsigned long)proc - ((unsigned long)pthis + sizeof(thunk_));

::FlushInstructionCache(::GetCurrentProcess(), pthis, sizeof(thunk_));

return (true);
}

ProcCall GetProcCaller(void) {
return (reinterpret_cast<ProcCall>(&thunk_));
}

static void DefProcCall(void) {
std::cout<<"in Adapter::DefProcCall.\n"<<std::endl;
}

private:
MachineCode thunk_;
};

Then, I use the following code to call X::DefProcCall function:

Adapter x;
x.Init (&Adapter::DefProcCall, &x);
ProcCall call = x.GetProcCaller();
call();

The all magic is at 'reinterpret_cast<ProcCall>(
&thunk_)' in GetProcCaller member function of Adapter class and two data members in MachineCode class. Directly, generates such IA-32 code 'call dword ptr [function-address]' when C++ compiler meet 'call()' statement. Because of the function-address exactly is the address of the thunk_ which is the first data member of Adapter class. So the machine code in thunk_ has been executed.

The jmp_ data member of MachineCode class stores 0xE9 that is 'jmp' instruction in IA-32. And the proc_ data member stores relative address since the ending address of the thunk_ which in Adapter class. So whole MachineCode class generate such IA-32 code 'jmp address'.

10/16/2007

[] Operator in IA-32 assembly code

[] bracket operator means get the content of the memory location, u can treat [location] as addressing expression.

The following C code:
int i;
i = 0x1234;

can be translate to assembly code which looks like this:
mov dword ptr [i], 1234h

the dword ptr means the content of variable i occupied 4 bytes. It is required because of IA-32 is byte addressable. And above mov instruction does not know how much storage requires for storing immediate number 1234h.

10/15/2007

Cannot install Microsoft Server 2003 SP2!

*update.inf*

1. net stop cryptsvc
2. ren %systemroot%\system32\catroot2 old-catroot2
3. net start cryptsvc

10/14/2007

Manipulate bits in C

1. Definition:
const int FIRST = 1 << 0;
const int SECOND = 1 << 1;
const int THIRD = 1 << 2;

Why defines the bit flag like this? Oh, just for clarity.

2. Setting bit flag:
int flags = 0;

flags |= FIRST;
flags |= SECOND;
flags |= THIRD;

if (flags & FIRST) {
printf("FIRST bit has been set.\n");
} else if (flags & SECOND) {
printf("SECOND bit has been set.\n");
}
...

3. Clearing bit flag:
flags &= ~FIRST;
flags &= ~SECOND;
...