Tuesday, October 25

Registry Redirection, for 32-bit application in 64-bit Windows OS

Some people might have noticed, and some might not:

In Microsoft's 64-bit operation system, there is one "C:\Program Files\" folder and one "C:\Program Files (x86)" folder. 32-bit applications are placed in the second one. The tricky thing is, when the 32-bit applications internally are trying to visit "C:\Program Files\" folder, they are being redirected to the "C:\Program Files (x86)" folder. This is call "WoW64 File System Redirection". When the 32-bit applications are trying to access Windows\System32\ folder, they are actually accessing Windows\SysWOW64\ folder.

Microsoft use this "WoW64" approach to keep 32-bit applications in the same boxes with 64-bit application, and let the 32-bit applications access 32-bit environment (DLLs), not messing around the 64-bit environment.

Take a wild guess, what is the meaning of "WoW64"?

For registry, we have the same story. For example, there are registry path KEY_LOCAL_MACHINE\SOFTWARE\Microsoft, and HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft. When you run "regedit" in your OS, of course it is 64-bit application, so you can see both paths. When requesting "HKLM\SOFTWARE\Microsoft", a 64-bit application will get the content of first path, but a 32-bit application will get the content of second path.

If a 32-bit application wants to get the content of the first path, in the source code, it will need to open the registry key with extra KEY_WOW64_64KEY option:
RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_SET_VALUE|KEY_WOW64_64KEY, &hKey);

Note: Visual Studio, by default, is creating 32-bit applications, even when it is running in 64-bit operating system.

Yesterday I was too lazy to use "RegOpenKeyEx". I just want to call the external regedit.exe to import an existing Ben.reg file, as part of the configuration stage. The registry keys should be in KEY_LOCAL_MACHINE\SOFTWARE\Ben folder. I ran "regedit /s Ben.reg" a thousand times and the content is imported successfully. But when the same command line is called from my application, it reports "imported successfully" but the keys are not in the KEY_LOCAL_MACHINE\SOFTWARE\Ben folder. Of course now you know the keys are in KEY_LOCAL_MACHINE\SOFTWARE\WoW64\Ben folder, since the Visual Studio generated application is a 32-bit application.

After googling thousands of webpages, I found one working solution from Greg Domjan:

Add a class:
class Wow64RedirectOff {
typedef BOOL (WINAPI *FN_Wow64DisableWow64FsRedirection) ( __out PVOID *OldValue );
typedef BOOL (WINAPI *FN_Wow64RevertWow64FsRedirection) ( __in PVOID OldValue );

public:
Wow64RedirectOff() {
LPFN_Disable = (FN_Wow64DisableWow64FsRedirection)GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"Wow64DisableWow64FsRedirection");
if( LPFN_Disable ) {
LPFN_Disable(&OldValue);
}
}

~Wow64RedirectOff() {
if( LPFN_Disable ) {
FN_Wow64RevertWow64FsRedirection LPFN_Revert = (FN_Wow64RevertWow64FsRedirection)GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"Wow64RevertWow64FsRedirection");
if( LPFN_Revert ) {
LPFN_Revert(OldValue);
}
}
}

private:
FN_Wow64DisableWow64FsRedirection LPFN_Disable;
PVOID OldValue;
};

Then in the program, you define Wow64RedirectOff scopedRedirect; before calling the external program "regedit". You can wrap these 2 actions into one bracket so that scopedRedirect is being destructed right after the external program is terminated, and the WoW Redirection is being restored to normal.

Labels:

Friday, October 21

转贴:相声集锦

侯耀文 石富宽《火红的心》
http://www.tingcd.net/thread-1400-1-1.html

马志明 黄族民 《大保镖》
http://www.youtube.com/watch?v=SnmV2yOtuN0

马季 赵炎 王金宝 刘伟 冯巩 《五官争功》
http://www.youtube.com/watch?v=J96tQUfVgIE

苏文茂 赵世忠《论捧逗》
http://www.youtube.com/watch?v=njzpP-qA0DQ

高英培 范振钰 《不正之风》
http://www.youtube.com/watch?v=Aq6eE8GbrjU

高英培 范振钰《钓鱼》
http://www.youtube.com/watch?v=mvZTfdvC5O4

侯寶林 郭啟儒 《夜行記》
http://www.youtube.com/watch?v=vrQhHUVmNBk

刘宝瑞 《风雨归舟》
http://www.youtube.com/watch?v=ABQzwIbJido

马三立 王凤山《买猴》
http://www.youtube.com/watch?v=L4T-F692u0E

马三立 《逗你玩》
http://www.youtube.com/watch?v=cj2z49cFQHo

马三立 王凤山 《十点钟开始》
http://www.youtube.com/watch?v=IfliJ7jBSSs

黄铁良、尹笑声《托妻献子》
http://v.youku.com/v_show/id_XMTU3MzMyMDQ=.html

姜昆 李文华《想入非非》
http://www.youtube.com/watch?v=MtjWEnBJol0

姜昆 唐杰忠 《着急》
http://www.youtube.com/watch?v=fclvWIBY-Sw
====

王自健 徐强 《最近》
http://www.youtube.com/watch?v=jGV6QI-_GGM

曹云金,郭德纲《香水无毒》
http://www.youtube.com/watch?v=C01zGUQZTnU

郭德纲《白小平上坟》
http://www.youtube.com/watch?v=5nbiHEzhEcY

郭德纲 于谦 《托妻献子》
http://www.youtube.com/watch?v=kIznu5ZMSzA

郭德纲,于谦 《我是黑社会》
http://www.youtube.com/watch?v=oNqXNcHHLPM

方清平《幸福童年》
http://www.tudou.com/programs/view/l_VIw4_x6S0

冯巩 牛群《小偷公司》
http://www.youtube.com/watch?v=oeJv0wbHsMs

李金斗 陈涌泉《武松打虎》
http://www.youtube.com/watch?v=4G5fUiGoAiM

杨仪 杨少华 《肉烂在锅里》
http://www.tudou.com/programs/view/Np2bF9hiHdI/

赵伟洲 杨少华《危言耸听》
http://www.letv.com/ptv/vplay/798720.html

杨振华 金炳昶 《假大空》
http://v.youku.com/v_show/id_XMTUxMDA3MjA4.html

姜昆 唐杰忠 《虎口遐想》
http://www.youtube.com/watch?v=R-MvFdhTt-I

姜昆 李文华《祖爷爷的烦恼》
http://www.youtube.com/watch?v=pEilJTSVjtQ

马志明 黄族民 对春联
http://www.youtube.com/watch?v=zfpanLMSuWg

《卖挂票》 马志明 黄族民
http://www.youtube.com/watch?v=JgydYO9uvcA

《报菜名》 马志明黄族民
http://www.youtube.com/watch?v=Eys_-twcBGE

相声 太平歌词 北大版1 马志明 黄族民

http://www.youtube.com/watch?v=vPP_x9QR7cE

汾河湾1 苏文茂 王佩元
http://www.youtube.com/watch?v=1zIWWNpCK1k

《扒马褂》 刘宝瑞 马季 郭启儒
http://www.youtube.com/watch?v=3CKq3w4LCDk

每个链接上都有类似的相声。

Thursday, October 13

Four Strong Winds

Four Strong Winds, 我最喜欢的还是四兄弟的版本:

Four strong winds that blow lonely, seven seas that run high,
All those things that don't change, come what may.
四风吹,七海起。万物恒,永恒。

Well our good times are all gone, and I'm bound for moving on.
I'll look for you if I'm ever back this way.
我们的好日子已经结束了,我肯定要向前看。
真的要回来的话,我还会找你的。

I think I'll go out to Alberta, the weather's good there in the fall.
I got some friends that I can go to workin' for.
Still I wish you'd change your mind, if I asked you one more time
But we've been through that a hundred times or more.
我想,我会去阿尔伯塔省,那里秋天的天气还好。
那里有几个兄弟,我可以跟他们干活。
依然,我希望你能改变主意,如果我再问你一次,
但是我们已经讨论过无数次了。
[Chorus]

If I get there before the snow flies, and if things are looking good.
You could meet me if I sent you down the fare,
But by then it would be winter, nothing much for you to do.
And the winds sure can blow cold way up there.
要是我能在下雪前到那里,一切顺利的话,
会给你寄来路费,让你过来。
但到时候就已经冬天,没啥让你干的,
而且很冷。
  

Labels: