Archive for May, 2007

Google Gears Launch

Google Reader于今日发布了"Google Gears",相比较Google其他的一些实验室产品,Google Gears是一个革命性的开发工具/模式。它主要解决了一个问题,而这个问题也是影响WEB体验一个重要的问题,就是:用户处于离线状态,网络应用该如何继续。通常情况下,网络断后,我们只能等待网络接通后继续,有了Google Gears,一切将改变。使用GG开发的Web应用程序,会在网络连通的时候下载一些必要的数据,缓存到本地,当网络处于离线状态时,用户的数据将保存在本地,等到网络再次可以访问时,用户在离线时期所作的更改将和服务器进行同步,这样离线与否对于用户体验并没有多大的影响,这在以前是不可想像的。我们可以设想一下有了Google Gears将会对那些原有产品产生影响,Google Document就是其中一个典型,原先有很多人批评Google Doc只能在线使用,一旦离线,所有的一切都是空谈。有了GG,用户所编辑的文档都可以缓存在本地计算机,即使网络断了,只要等网络恢复后,这些数据将会和线上的数据同步,以保证数据的完整性。

但是它也有其不完美的地方,比如:某个数据被多个用户共同访问,如果某个用户断线了,那么该用户在离线模式下所作的修改该如何和其他用户的数据同步。等等。

Google用 LocalServer来管理对离线应用程序的访问,而 LocalServer使用 Database来管理用户数据,需要提一下:Google这次选择了SQLite,这是个开源的数据库系统,非常小巧,但功能丝毫不差。如果有大量的数据需要同步,WorkerPool则可以解决这个问题,有兴趣的朋友可以去Google Gears的官方站点看看:http://code.google.com/apis/gears/index.html

下图是我今天登录Google Reader的时候,Google Gears 插件同步数据的图示:

Google Gears

New release of Windows Live Messenger 8.5

[Hot News: WLM 8.5 is available at official site now: http://g.live.com/1rebeta/WLMsgr85_en-us ] 

Earlier in last week, a new release of Windows Live Messenger 8.5 (WLM85 for short) leaked on a Spanish website. And now, this version already spread out all over the world. It is said that many new features are added in WLM85, for instance: New UI Design, New Installer(maybe this is not a feature indeed :), Reinforced Security, etc. What impressed me most is the new UI resembling Vista. The UI of Windows Live Messenger(formerly known as MSN Messenger) is the tread of IM applications. well, do you want a go? try the following links:

http://www.msgstuff.com/news/files/Windows%20Live%20Messenger%208.5.exe

Not a Spanish speaker? download this DLL file and replace the one what was already there in your WLM85 installation directory. take a look at my snapshot:

Windows Live Messenger 8.5

FYI:

Before you download files, please read this.

Tips/FAQs on VS2005

  1. Q:How to recover from "No visual studio template information found"?
    A:The answer is quite simple, just run 'devenv /setup'.
  2. The IDE remembers the last 20 items copied. To rotate through the list of copied items and choose one to paste into the current file, press CRTL + SHIFT + V.
  3. Q:How to select a rectangle of text?
    A:Hold down the ALT key, press down on the left-click button of your mouse, and then drag the cursor over the text to select.  or, Hold down the SHIFT+ALT keys and press any Arrow key.
  4. Q:How to set the Next Statement?
    A:In the Visual Studio debugger, you can move the execution point to set the next statement of code to be executed. A yellow arrowhead in the margin of a source or Disassembly window marks the location of the next statement to be executed. By moving this arrowhead, you can skip over a portion of code or return to a line previously executed. You can use this for situations such as skipping a section of code that contains a known bug.

建立自己的消息循环(续)

前面的帖子《建立自己的消息循环》中提到使用从CCmdTarget派生的类作为消息传递的载体,但这种方法不能传递多余的参数(也许可以,但我不知道怎么搞:)。为了使这个机制支持大多数的消息,我把前面的例子改动了一下,把那些用于消息处理的类,改为从CWnd派生,然后再Create这些窗口。这样这些类其实就是个标准的窗口了,只是我没有把它们显示出来。

本例和前面的例子,主要用在那些多使用消息机制的Project中,其实也可以不必这么做,完全可以直接去调用类的方法。但是如果消息机制比较适合,此方法不失为一种选择。

点击这里下载Demo

建立自己的消息循环

在MFC程序中使用消息机制的时候,常常会在程序即将完成的时候发现,在某个窗口中加入了太多的消息处理宏,并且这些消息的Handler和UI界面的方法混合在一起,待到项目规模大的时候,会导致后续的维护越来越困难。我们希望将UI的动作和业务处理的动作隔离开,互不影响,我们可以模仿MFC的消息流转机制,就本文来说,是Command的流转(Command Routing),大家知道,所有的从CCmdTarget派生的类都可以支持消息处理,比如CDocument, CView, CDocTemplate等。Framework在内部使用了Chain of Responsiblity。消息会在一个链条上找到自己的执行的地方,我们可以把不同的业务逻辑分割为各个不同的环,我们可以在修改一个环的情况下,而不去影响其他的环。附件中的例子只是简单的发送一个WM_COMMAND消息,消息的ID,表示了不同的动作。此方法适合发送消息时候,参数很少或者不用参数的情况。

 示例代码的简单说明:

  1. 业务处理类需要从CCmdTarget派生
  2. 在每个业务处理类里面加入适当的消息映射(注意更新MyCommand.h中的消息的宏)
  3. 在CCmdManager类的BuildCmdTarget()中加入类的初始化
  4. 在主窗口的OnCmdMsg中调用 CCmdManager的CallOnCmdMsg方法

P.S.
这里写的只是MFC消息机制最粗糙的改造,由于时间的关系,这里不做过多的介绍

示例代码,点击这里下载

Add crash report to your application

By Charry 

编写‘零缺陷’的软件是人们追求的目标,然而要做到真正的‘零缺陷’是不太可能的任务,即使的最简单的程序,我们也不敢保证它是一个理想的'bug-free'的产品。bug会贯穿一个产品的每个阶段。在开发阶段我们可以debug我们的代码,但到了后期,当我们把程序release给用户时,如果程序发生的问题,比如Crash,我们应该收集程序Crash的原因,以帮助我们修补程序的bug,现今有很多软件都提供了"Crash Report"的功能,最常见的就是MS的一些产品,在程序崩溃的时候,会出现一个对话框,提示我们程序崩溃了,是否愿意将崩溃时候的一些信息发给MS,以供开发人员调试之用。

且不说用户反馈的信息是否对我们有用,但说崩溃后提示:“程序崩溃,请blah, blah, blah”这样的字样,从某种意义上说就是在增强用户体验,因为比起程序咣当一声倒地身亡,这样起码给用户一个暗示:问题是有解决的途径的。

CodeProject上有篇文章详细的介绍了Crash Report,http://www.codeproject.com/debug/XCrashReportPt1.asp 。不过文章中介绍的内容是针对VC6的,对于VS2005还是有些问题的。我总结了一下,如何给VS2005的工程加入Crash Reprot的支持(如果是VC6的工程,请直接参考上面的链接)。

首先,去http://www.codeproject.com/debug/XCrashReportPt4.asp 下载DEMO程序,我们需要其中的XCrashReport,将它编译后,会生成XCrashReport.EXE。然后将

ExceptionAttacher.cpp
ExceptionHandler.cpp - 需要去除它的预编译头文件开关
ExceptionHandler.h
GetWinVer.cpp
GetWinVer.h
MiniVersion.cpp
MiniVersion.h
CrashFileNames.h

这个几个文件加入到工程中。注意,直接加入工程中并不能编译通过,作者在他的文章中提到了:“The Unicode implementation is not complete or tested.”,好在修改上面的错误也很简单,这里就不赘述了。然后将工程切换到release模式并打开工程的属性。先找到"C/C++->General",把"Debug Information Format"设置为"Program Database(/Zi)";然后找到"Linker->Debugging",把"Generate Debug Info"设置为"Yes(/DEBUG)";最后,找到"Linker->Command Line",增加一个选项"/OPT:REF"。(好像VS2005缺省就是这样的设置)

保存好后,就可以编译您的工程了,把XCrashReport.exe 和你的EXE程序放到一起,这样在程序崩溃的时候,就会自动生成Crash Report。对于VC6编译出来的EXE程序,我们可以按照作者文章中介绍的那样,直接用VC IDE找到引发Crash的代码行,可是在VS2005下,我尝试了N次,始终不行。为了定位出错的代码行,我们还需要再下载一个工具,就是WinDbg。这是MS开发的一个非常有用的DEBUG工具,安装完毕后,将XCrashReport.EXE生成的"CRASH.DMP"的拖到WinDbg中,然后选择"File->Source File Path"设置工程源码的目录,选择"File->Symbol File Path"指向最后一次编译生成的pdb文件所在的目录,接下来在"View"菜单中打开"Call Stack"窗口,最后在"Command"窗口中输入:.ecxr 并回车,WinDbg就会显示Call Stack和引发Crash的代码。

最后补充一点,XCrashReport需要dbghelp.dll,Windows自带的dbghelp.dll可能无法正常使用,最好将原作者的DEMO中的dbghelp.dll一并copy了。还有填写接受report的email的时候,不要简单的写一个email,那样outlook不能正常的发送,得在email前面加上SMTP,例如:SMTP:t...@hotmail.com

提醒一下:一定要把VC/VS2005生成的pdb文件备份好,以及和这个pdb文件对应的代码,如果代码改变了,WinDbg就不能跳到正确的代码行。

完!

Here are some links about design pattern on the web

http://www.dofactory.com/Patterns/Patterns.aspx

http://home.earthlink.net/~huston2/dp/patterns.html

If you're new to Design Pattern, the links listed above may be helpful. the first one is easy to understand. it shows you the skeleton of every pattern and illustatues the details in real-world code. by the way, <<Head First Design Patter>> is also an amazing book for us, it's the best seller in many online bookshops recently.

A class for simulating MSN Messenger popup window

This is a dialog-based class derived from CDialog, it shows or hides a dialog in an animated way, just like MSN Messenger does. it's still a draft, I'll add more features and refactor it later on.

Usage:

  1. Add "AnimateDlg.h" and "AnimateDlg.cpp" in your project.
  2. Add a CDialog-based class in your program.
  3. Replace all 'CDialog' with 'CAnimateDlg' in header file and implementation file.

Known Bugs:

This verstion of CAnimateDlg only works properly when Windows task bar is on the bottom of screen. it'll be fixed in a future version.

To be continued...

Download project

发布一个Google Talk辅助聊天工具

Usage:

  1. 下载GSnap
  2. 如果你有web server,请先配置你的上传图片的脚本,保存为g.xml
  3. 执行GSnap,截图后,图片会自动上传到您的服务器中,并且图片的URL就保存在系统的剪贴板中,你只要在聊天窗口中,paste给您的朋友即可。(虽然和QQ的截图相比还是有些不方便,但总比没有强:)

旧版本的GSnap只能截图,新版本的GSnap加入了将截图上传到http server的功能。目前功能比较单一,但也比较简单,如果您是个网站站长,拥有自己web server,也许这个会给你带来些许用处。

配置文件如下

<gsnap server="charry.org" uploadpath="test" url="test/upload9233832dfkasdf_sdf.asp" port="80" tagname="file1">
<!-- server: your web server's hostname or ip address -->
<!-- uploadpath: the relative upload-path in your web server -->
<!-- url: the path of your uploading script, e.g. upload/upload.php -->
<!-- port: port of web server -->
<!-- tagname: tag name in form -->
</gsnap>

如上面的xml所示:

server表示服务器地址,注意不要带http://
uploadpath表示上传目录,这个地址是相对于服务器根的地址,注意这个目录的权限
url表示上传文件脚本的地址,这个地址是相对于服务器根的地址
port表示服务器的端口
tagname表示上传页面<form />中文件名的标签名

点击这里下载

P.S. 下一版本可能会加入自动上传到知名相册站点的功能,比如Google Picasaweb, Flickr等

MD5 Wrapper class (for file)

最近忙的要死,VB终于要上线了,懒得写东西,随手发一个MD5的包装类,也许有朋友能用的到。

接口如下

CStringA MD5HashFile(CStringA strFilename);

这里没有做向宽字节转化的操作,如果有朋友需要,可以参考前些时候我贴的两个加密Wrapper类中的CMyUtility。

点击这里下载