Posted: December 1, 2008 at 7:54 pm | Tags: class, flash, html, java, javascript, php, web, 测试, 生活, 类
裸照测试, 它是这样的一个测试: 假如说有一张照片, 照的是你正裸体的干一件事,
这个事会让你和你的家人甚至以后的几代人都会感到羞耻. 打个比方, 人兽交. 问问你自己, 身边能有几个人可以信任到能够共同面对这张照片.
如果你像下面我们说的那样, 你可能至多会有两个.
事实更令人沮丧,研究表明大约四分之一的人没有一个可以信赖的朋友.
The Sad Bear 1, by Nedroid
我们发现人们拥有的亲密朋友的数量正在快速的戏剧性的减少,特别是最近的20年里. 为什么会这样?
#1. 我们的生活中缺少让人讨厌的人.
这不是讽刺. 讨厌的人和事能够锻炼你忍耐的性格, 就像面对乙醇或其它难闻的气味. 如果我们尽量的去避免烦恼,那么,我们处理烦恼的能力也就越差.
问
题就出在,我们使用科技手段给自己建立里一个令人恐惧的,四处蔓延的web网络,而这个网络就是要让我们躲避那些让人烦恼的朋友。
不是吗?所有的圣诞采购都在网上进行,这样就避免了和那些胖大嫂为了同一个商品而发生购物车之间的磕磕碰碰。
花5000美元买一个家庭影院系统,这样就可以坐在家里看电影,像在电影院里有个淘气的孩子老踢你的后座的事情再也不会发生了。
天,连租DVD也只需要到Netflix网站上,根本不需要到音像店里和那些笨头笨脑的登记影碟的孩子打交道。
老老实实的在候诊室里等着大夫? 遗憾,与邻座的散发着难闻气味的老人聊天是不可能的。
我们会用iPod听音乐,发短信给朋友或者玩任天堂的DS 把烦恼踢出我们的世界
来自outofbalance.org
如果现在的生活真的没有烦恼,那真的太好了. 但是事实并不是这样 而且永远也不会这样. 只要你有需求,你就要不时的和你不能忍受的人打交道
我们已经慢慢不懂得怎样和陌生人打交道, 不懂得忍受他们刺耳的声音,还有白痴的幽默,不能忍受他们的体味还有吱嘎作响的鞋.
那么,什么才能令你走到外面这个世界,一个你不能控制,一个令你想尖叫的世界.
Oh, yeah. Right in the crotch, buddy.
我们大部分人生活的地方都充满了我们不能容忍的人. 当你是小孩的时候,也许你就会发现,在你小学教室里,装满了很多和你兴趣爱好都不同的同学,而你又不能选择的同学. 也许你会经常感到失望.
但
是,你已经长大了. 还有,如果你是一个忠实的DragonForce(英国某乐队)迷,你可以到他们的论坛上找到一大堆和你一样喜欢这个乐队的人.
或者更可以自己开一个私密的房间,只和你的好友在一齐,其他人都不能进来. 可以告别和兴趣真的不同的人打交道的烦人,无聊,痛苦的过程
都是旧社会的问题,就像以前你要到河边洗衣服,或者要打猎穿兽皮.
问题是友好的解决人与人之间的矛盾,在社会上是很重要的生存之道. 事实上,如果你认真的想一下,你能友好的解决和你有矛盾的人的关系.,这就是所谓的社会. 只要和那些有不同思想,不同性格的人一起生活,合作,通常会存在摩擦.
50年前,你不得不坐在一个拥挤的房间里看一部电影 你根本不能选择,除非你不想看这部电影. 当你买了新车,街上所有的人都会站在你院子里看来看去. 你能肯定有些人里面有不少傻B.
Your parents, circa 1982
但是总的来说,那时的人还是对他们的工作感到高兴,对生活感到满足的. 是因为:他们比现在有更多的朋友.
没错. 虽然他们几乎都是不能根据共同兴趣来选择朋友,(靠,那时候只要是你邻居都是你朋友了啦.),但是他们仍然能有很多很要好的朋友,起码比现代人来说,那时候的人更值得相信.
那
就是说,很明显,
在你和别人发生第一次争吵,当你为争面子说:”他们不听我喜欢的歌,是他们不懂得欣赏.”之后,在某程度上,除了共同兴趣外,你们会感到互相熟悉了,这所
谓不打不相识. 那就是说,毕竟人都是需要社交的. 而那容忍别人和事的能力,就是你能正常生活在这充满和你不同的人的世界里唯一的保证.
否则,你会变另类. 这是有科学根据D…
我有个朋友会讽刺的说,”不用了,谢谢.” 他的意思是,”我宁愿死也不要”.他说最后两个字的时候带讽刺的味道,让你知道他的意思. 当你问他”想不想去看罗伯施奈德最新的电影?”他会说”不用了,谢谢.”
有一天,我们用这样的文字交流:
我”喂,要不要试下剩下的红辣椒,我亲手做的啊.”
他:”不用了,谢谢. ”
我看到就很生气. 我很喜欢我做的红辣椒 我都做了4天了. 是我自己磨的胡椒粉, 那牛肉也很贵,是手打牛肉. 而现在我的好意就让他这样恶毒地拒绝了.
后来我六个月不和他说话. 他之后发了封邮件给我,我看都不看就回了, 还装了个死老鼠在里面.
最后是我太太去找他弄清楚了他那句”不用了,谢谢.”并不是反话,是”真的不用了,很感谢你”,原来他的冰箱已经满了.
The Sad Bear #2, by Nedroid
所以我们是不是需要一个研究结果来告诉我们,你每写一封邮件,里面可能就有百分之四十的内容会令别人误会. 嗯,真的有人研究了.
你
有多少没见过真人的网友? 当你们用文字交流的时候,有百分之四十内容不是真的表达你自己的意思,你觉得你们真的了解对方吗?
有人因为你在留言板,聊天室或者其他什么地方的言论而不喜欢你,是不是你真的那么令人讨厌呢? 又或者,其实是因为这百分之四十的误会.
还有,那些喜欢你的人呢?
我们很多人都在纯粹为了虚构好友量,在My Space 上积累了大批的所谓好友. 但是,问题来了.
当人们面对面交流的时候, 说话的内容占了实际表达意思多少比例? 此外还有身体语言和声音语气. 猜一下。
只有百分之七.其余百分之九十三不是语言,是有研究得出的数据. 是的,我也不知道他们怎么算出来的. 他们有个机器吧. 但是我们不需要它
我的意思是,醒醒吧. 我们很多幽默都是来自于讽刺,而讽刺就是说话的时候语气和内容不相符. 就像我朋友说的:”不用啦,谢谢.”
你不用等一个女孩亲口说她喜欢你 她眼力的火花,她的姿态,她塞你的脸到她胸前的方式,都告诉你,她喜欢你.
这
就是问题的关键. 人类通过这种潜意识去理解别人情绪的能力是很重要的. 如果一个小孩天生没有这个能力,我们会认为他是智力有缺陷的
如果有人的这个能力异常发达的话,我们称之为”魅力”,他极有可能成为电影明星和政治家. 不是因为他们说了什么,而是
他们散发出来的能量,令我们感觉良好.
当我们生活在文字世界里,所有的一切都没了. 还有一个奇怪的理论:由于缺少对别人情绪的理解,我们每看到一句话,都会从我们自己的情绪去理解它. 我之所以以为我那个朋友说的话是讽刺,是因为我那时候心情并不是很好. 带着那种情绪,我是希望能有人给我骂的.
更糟糕的是,如果我继续这样聊下去,我的情绪不会改变. 毕竟,总有人说些我不愿意听的话.
当然我会很沮丧. 是我在和这个世界斗争!
不是这样的,我只是想有人能搭搭我肩膀,赶走我的不好情绪. 这就是我要说的第五点.
没有好朋友最糟糕的地方不是没有生日派对可以玩,不是自己凄凉的对着墙打乒乓球. 都不是,最糟糕的是缺少真正的批评
我
上网的时候,别人叫我”同性恋”叫了几乎104.165次. 我就做了个Excel 表. 还有人叫我”混蛋”,”
鸡头黄鼠狼”,”日骆驼”,”阴道饼”,”吃屎的”,”猪肉刀”,”王八蛋”,”屎口哨”,”雷阴道”,”屁王”,”屎绒”,”瘤精灵”,”无聊”.
(译者:-_-||….)
全
部都没关系,因为没有人真的了解我,能说中我. 我给人侮辱很多,但是给人批评非常少. 千万别搞乱这两个词.
侮辱是指某个讨厌你的人发出的噪音去表达他们的不满. 一只会吠的狗. 批评是指有人想去帮助你,指出你自己都不知道的缺点.
Above: A flamboyant transvestite with about
five times as many friends as the average person
可悲的是,有非常多的人根本没有那样的谈话. 就好像”你知道吗,所有人都生你的气,就因为你昨晚说的话,但是没人敢告诉你,因为大家都害怕你了.”这样的话,这样真诚的话. 那些可怕的 ,笨拙的,悲痛的,不舒服的时刻,只有一些能看穿你的人会告诉你.

电子邮件和文字聊天是阻止真诚对话的有效工具. 用文字,当你喜欢的时候可以回复. 你可以评估你的话语. 你可以选择问题来回答.
在网络另一头的人看不到你的脸,看不到你在紧张,察觉不出你在撒谎.
你几乎能控制一切,所以,没人能撕下你的面具,永远看不到你的缺点,永远不知道你所做过的蠢事.
怪癖,丢脸,缺点这些组成友谊关系的重要元素,都消失了.
浏览一下人们在My Space的页面,看看他们为自己而虚构的人物性格. 如果你通过写博客,已经把自己塑造成一个神秘的夜游大师,的确很难让你说出事实你是怎样去那个舞会,还有在舞池上腹泻. 你永远不能做真正的自己, 那是一种非常寂寞的感觉
总的来说……
看这文章的一大部分人都会说,”我就是很沮丧.” 人们都饿坏了! 美国变成了德国纳粹时代. 我父母在看完白痴电视节目后还要继续讨论几个小时. 世界上的人们正在死于一些毫无意义的战争.
但
是,我们是怎样从一个比我们老一辈日子更负面的世界里 发奋? 或者说更老一辈. 回到那个时候,人没有那么长命,婴儿也经常早死.
疾病是更普遍 在那时候,如果你的好友搬走了,你们唯一的联络方法就是一支笔,一张纸和一张邮票.
我们现在有伊拉克,但是老一辈有越南(比伊拉克死多50倍人) ,再老一辈有第二次世界大战(比伊拉克死多1000倍人)
你们的祖父是生长在一个没有空调的时代.
在各个方面,我们自然比以前生活的更好,但是TMD网上的新闻都不知道是真的假的. Why?
你问问你自己,如果有个音乐网站发了篇文章叫” Fall Out Boy是一支不错的乐队”,同一天还有另外一篇文章叫”专家说,Fall Out Boy是TMD一百年里最垃圾的乐队.”你猜哪篇文章会更热门. 第二篇肯定火了. 夸张制造口碑.
你
看了多少写新闻的博客 写这个的人都知道一个事实.
每个网站都像狗和狗打架似的在争流量,(就算他们网站没有广告,他们会觉得多人看就满足了.)所以他们都会选一些奇怪的,激动的事情来写.
其他的网站开始模仿同样的手法. 你愿意的话,你可以在这充满温暖的不流动的的水的池塘里游一整天,(意思是看一天都是垃圾新闻)
Actually, if you count the guy holding the camera, this man
statistically has more friends than most of us do.
只有在这样的环境下,才会有那白痴的911阴谋论出现(说是布什主脑的,由FDNY炸毁大厦,还有那飞机是全息图.) 他们都说,每个说反对的政治家都是希特勒,每个选举都是怪异的启示录. 都是为了让你去阅读
9/11 photos. Circled: Conspiracy
当
然这些在以前是不会发生的. 我们中的一些人会记得那时候电视只有3个台. 没错. 三个. 我现在说的是80后.
所以有些东西都统一了,我们都看同样的新闻,都是同样的观点. 就算这些观点是弱智还有错的,
就算有些新闻是因为违法而不能报道的,我们都能分享它.
完了.
肯定是不会再有”混合媒体”的了,以前我们有异议是因为看了相同的新闻但是有不同的理解,现在我们有异议是因为我们看的是完全不同的奇怪的新闻.
当我们连基本的真相都不能认同的时候,小小的不同就成了很大的矛盾. 那持续的点点优越感带来了不断的紧张和不安.
过去我们人类有很多天生的方法来释放这种焦虑。 但如今…
#7. 我们觉得生活没有价值,因为我们确实没有价值。
去找更多的在线朋友,这有个好处,而且是没人会谈到:
他们对你几乎没有要求。
的确,当他们有失恋时,你可以从感情上帮助他们,安慰他们,甚至能将他们从自杀的冲动中解救出来。 但现实世界的人们会强加你一大堆的讨厌的要求。
浪费你整个下午去给他们修电脑。 跟他们一起去参加葬礼。 用你的车带着他们四处奔走,直到他们从银行哪里拿回自己的东西。
正当你收拾好了要看Discovery频道的 Dirty Jobs
马拉松时,他们不请自来,而且告诉你他们如此的饥饿,直到把你的三明治分给他们一半。
然而在聊天工具里,或论坛里,或魔兽争霸里,你却能有更多的支配权。
问题就在这,由于人类进化的原因,你和社会已经绑在一起了,你需要为他人做些服务。 五千年来,每个都明白这些,然而就在近十年,人们却忘了。
十几岁的孩子就有自杀心理,于是我们争先恐后的去教他们自爱,自尊。
然而,不幸的是,这自尊和自爱只有当做了能让自己喜欢的事情后才会产生。这可不是胡说八道。
如果那个Todd整体待在屋里,喝着百事,一只手玩着视频游泳,另一只手在手淫,我会认为他在做毫无价值的事情,但如果是我在做这些事情,我又会怎么认
为自己呢?
The Sad Bear #3, by Nedroid
你能够把那些导致自卑的黑药丸丢掉吗? 收拾一下盖住眼睛的头发,从电脑面前走开,给你讨厌的人买一个礼物。 给你最大的死对头送张卡片。 给你的父母做顿晚餐。 或者简单做一些东西,一些真实存在的东西。 清扫屋檐下的落叶。 收拾一下快要死的花草。
这
些都是简单易做的事情;人是一个社会性的动物,你出生时身体里就带有一点快乐荷尔蒙激素。当你看到自己的活动产生了实际存在的价值时,它就会释放到你的血
液里。 想想那些十几岁的孩子,在黑屋子里,抱着电脑,把他们的所有生活问题全都幻化到可笑的游戏、戏剧情节里。
为什么他们会在自己的臂膀上能出那些小伤口? 因为这样可以能出疼痛—之后又能愈合—这是他们唯一能刺激体内的多肽激素的方法。
这是疼痛,但至少是真实的。
这种通过一些适度的困难任务来调节压力的办法过去曾是日常生活的一部分,例如猎捕羚羊,摘草莓,攀岩,猎捕熊。 不为别的。
这就是整天坐在办公室工作里会让如此多的人感到难受;我没有获得任何实在的、有形的产出。
在火热的太阳底下花两个月时间盖个建筑,余生里每当你开车经过这个建筑时你都可以说,“看看,我盖的。”这也许就是办公室里的枪击事件比工地里要常见的
多的原因。
这种体力上的,手工劳动获得的快感只有在你关掉计算机,走出家门,重新融入真实社会后才能获得。 这种感觉,这种“我建的”或“我喂养的”或“我种的”或“我制作的这些裤子”的感觉是任何网络给你的东西都不能相比的。
当然,除了这篇文章能给你的
Posted: September 23, 2008 at 4:48 pm | Tags: blog, cache, html, java, linux, server, unix, web, 优化, 平台, 开发, 技术, 测试, 类, 缓存, 芯片
Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用 (比如那个大堂里的地球模型),更在于其有别常规的IT策略……
加利福尼亚州山景城(Mountain View)Google公司(Google,下称Google)总部有一个43号大楼,该建筑的中央大屏幕上显示着一个与Google地球(Google Earth)相仿的世界地图,一个转动的地球上不停地闪动着五颜六色的光点,恍如罗马宫廷的千万烛灯,每一次闪动标志着地球的这个角落一名Google用 户发起了一次新的搜索。
这同时意味着Google又一次满足了人们对未知信息的好奇与渴望。
Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用 (比如那个大堂里的地球模型),更在于其有别常规的IT策略。从人们的常理来看,简单的硬件商品和免费软件是无法构建出一个帝国的,但是Google做到 了。在性能调整后,Google把它们变成一个无可比拟的分布式计算平台,该平台能够支持大规模的搜索和不断涌现的新兴应用。我们原本认为这些应用都是个 人消费级别的,但是Google改变了这一切。现在商业世界也在使用它们,这就令这家搜索公司显得那么与众不同。
GoogleWeb 服务背后的IT架构对无数使用搜索引擎的用户来说也许并不是非常重要,但它是Google几百位致力于把全球信息组织起来,实现“随处可达,随时可用”目 标的工程师们的最核心工作。这就需要一个在覆盖范围和野心上都与Google的商业愿景完全相符的IT蓝图作为支撑。
Google 的经理们一直对公司的IT策略话题保持沉默,他们厌恶谈及特定的厂商或者产品,当被问到他们的服务器和数据中心时,他们总是闭口不谈。但与几位 Google的IT领导一起呆了一天后,我们最终得以揭示该公司的IT是如何运作的,那可不仅仅是一个运行在无数服务器集群上的、表面看来非常简单的搜索 引擎。在其简单的外表下,蕴涵着许多内部研发软件、定制硬件、人工智能,以及对性能的执着追求和打破常规的人力管理模式。
IT理念方面,Google对同行有一条建议:尽量避免那些人人都在使用的系统和软件,以自己的方式做事会更有独特的竞争优势。
“企业文化决定了你的做事方式。”道格拉斯”美林(Douglas Merrill),这位Google工程副总裁和事实上的首席信息官(CIO) 指出,“到了我们这样的发展阶段,企业观念和文化非常与众不同,这也反过来鞭策我们必须要采用与众不同的方式来运行那些他人看来很常规的系统。”
Google 最大的IT优势在于它能建造出既富于性价比(并非廉价)又能承受极高负载的高性能系统。因此IT顾问史蒂芬”阿诺德(Stephen Arnold)指出,Google与竞争对手,如亚马逊网站(Amazon)、电子港湾公司(eBay)、微软公司(Microsoft,下称微软)和雅 虎公司 (Yahoo,下称雅虎)等公司相比,具有更大的成本优势。Google程序员的效率比其他Web公司同行们高出50%~100%,原因是Google已 经开发出了一整套专用于支持大规模并行系统编程的定制软件库。据他估算,其他竞争公司可能要花上四倍的时间才能获得同等的效果。
打造服务器
Google 究竟是怎样做到这点的呢?其中一个手段,美林认为,“是因为我们自己动手打造硬件。”Google并不制造计算机系统,但它根据自己的参数定制硬件,然后 像MTV的节目“靓车打造”(Pimp My Ride)那样自己安装和调整硬件系统。开源程序经理克里斯”迪博纳(Chris DiBona)评论道:“我们很善于购买商业服务器,并且改造他们为我们所用,最后把性能压榨和发挥到极致,以致有时候他们热得像要融化了似的。”
这种亲手打造的方式,来源于Google从车库诞生时与生俱来的节俭风格,更与Google那超大型的系统规模息息相关,良好的习惯一直延续至 今。据说 Google在65个数据中心拥有20万~45万台服务器—这个数目会有偏差(取决于你如何定义服务器和由谁来做这项统计)。但是,不变的是持续上升的趋势。
Google不会去讨论这些资产,因为它认为保密也是一种竞争优势。事实上,Google之所以喜欢开源软件也是因为它的私密性。“如果我们购 买了软件许可或代码许可,人们只要对号入座,就可以猜出Google的IT基础架构。”迪博纳分析说, “使用开源软件,就使我们多了一条把握自己命运的途径。”
Google喜欢规模化的服务器运行方式。当有成百上千台机器时,定制服务器的优势也会成倍增加,效果也会更趋明显。Google正在俄勒冈州 哥伦比亚河边的达勒斯市建造一个占地30亩的数据中心,在那儿它可以获得运算和降温需要的低价水力电力能源(参见边栏《Google数据中心自有一套》)。
Google以“单元”(Cell)的形式组织这些运行 Linux操作系统的服务器,迪博纳把这种形式比喻成互联网服务的“磁盘驱动器”(但别和一直谣传的Google存储服务Gdrive混淆了,“并没有 Gdrive这回事。”一位Google女发言人明确表示。),公司的软件程序都驻扎在这些并不昂贵的电脑机箱里,由程序员决定它们的冗余工作量。这种由 很多单元组成的文件系统代替了商业存储设备;迪博纳表示Google这些单元设备更易于建造和维护,他还暗示他们能处理更大规模的数据。
Google 不会漏过对任何技术细节的关注。多年来,公司的工程师就在研究微处理器的内部工作机制,随着Google规模的持续壮大,必然会用到特别定制和调节过的芯 片。知名工程师路易斯”巴罗索(Luiz Barroso)去年在一篇发表在工业杂志上的论文中证实,近年来Google的主要负荷都由单核设计的系统承担着。但许多服务器端的应用,如 Google搜索索引服务,所需的并行计算在单核芯片的指令级别上执行得并不好。
曾在数据设备公司(Digital Equipment)和康柏公司(Compaq)当过芯片设计师的巴罗索认为,随着AMD公司、英特尔公司(Intel)、太阳计算机系统公司(Sun)开始制造多核芯片,必将会出现越来越多芯片级别的并行计算。
Google 也曾考虑过自己制造计算机芯片,但从业界潮流来看,这个冒险的举动似乎不是很必要。“微处理器的设计非常复杂而且成本昂贵,”运营高级副总裁乌尔斯”霍尔 茨勒(Urs Holzle)表示。Google宁愿与芯片制造商合作,让他们去理解自己的应用并设计适合的芯片。这是一种客户建议式的设计,其关注点在于总体吞吐量、 效能,以及耗电比,而不是看单线程的峰值性能。霍尔茨勒表示,“这也是最近多核CPU的设计潮流与未来方向。”
裁缝般地定制软件
为了能尽量压榨硬件性能,Google开发了相当数量的定制软件。创新产品主要包括用于简化处理和创建大规模数据集的编程模型 MapReduce;用于存储和管理大规模数据的系统BigTable;分析分布式运算环境中大规模数据集的解释编程语言Sawzall;用于数据密集型 应用的分布式文件系统的 “Google文件系统”(Google File System);还有为处理分布式系统队列分组和任务调度的“Google工作队列”(Google Workqueue)。
正是从Sawzall这些工具里体现出Google对计算效率的执著关注。并不是每家公司都能从底层去解决效率问题,但是对Google来说, 为常规关系型数据库无法容纳的大规模数据集专门设计一种编程语言是完全合理的。即使其他编程工具可以解决问题,Google的工程师们仍然会为了追求效率 而另外开发一套定制方案。Google工程师认为,Sawzall能与C++中的MapReduce相媲美,而且它更容易编写一些。
Google 对效率的关注使它不可能对标准Linux内核感到满意;Google会根据自己的需要运行修改过的内核版本。通过调整Linux的底层性能,Google 工程师们在提高了整体系统可靠性的基础上,还一并解决了数据损坏和数据瓶颈等一系列棘手问题。对内核的修改也使Google的计算机集群系统因为通信效率 的提高而运行得更快。
当然,Google偶尔也会出现系统故障,情况一旦发生,无数的用户就会受到影响了。三年前一次持续30分钟的系统故障使20%的搜索流量受到影响。
Google 开发了自己的网站服务器却没有使用开源的Apache服务器,尽管它在网站服务器的市场占有率超过60%。迪博纳认为,Google的网站服务器可以运行 在更多数量的主机上,对Google站点上内容庞大又彼此互相依赖的应用程序来说,这种服务器的负载均衡能力远比Apache的能力更高。同时,在用标准 公共网关接口(CGI)访问数据库动态网页方面,Google服务器的编程难度要比 Apache更高,但是最终运行速度却更快。“如果我们能够压榨出10%~20%的性能,我们就可以节省出更多系统资源、电量和人力了。”迪博纳在总结中指出。
Google还设计了自己的客户关系管理(CRM)系统用于支持自己基于竞价和点击的互联网广告收费业务。但对是否需要设计自己的工具,Google的态度也不是一成不变的。比如在财会软件上,它就使用了甲骨文公司(Oracle)的Financials软件。
美林拿着一只叉子举例说明现成的产品也可以带来价值。但在有些场合现成的软件产品就不一定适用了。“我们的文化在各个层面对我们的运作都有深远影响,”他表示,“所以我们不想让购买所得的工具改变我们的工作方式和文化层面。”
Google’s BigTable 原理 (翻译)
题记:google 的成功除了一个个出色的创意外,还因为有 Jeff Dean 这样的软件架构天才。
—— 编者
官方的 Google Reader blog 中有对BigTable 的解释。这是Google 内部开发的一个用来处理大数据量的系统。这种系统适合处理半结构化的数据比如 RSS 数据源。 以下发言 是 Andrew Hitchcock 在 2005 年10月18号 基于: Google 的工程师 Jeff Dean 在华盛顿大学的一次谈话 (Creative Commons License).
首先,BigTable 从 2004 年初就开始研发了,到现在为止已经用了将近8个月。(2005年2月)目前大概有100个左右的服务使用BigTable,比如: Print,Search History,Maps和 Orkut。根据Google的一贯做法,内部开发的BigTable是为跑在廉价的PC机上设计的。BigTable 让Google在提供新服务时的运行成本降低,最大限度地利用了计算能力。
BigTable 是建立在 GFS ,Scheduler ,Lock Service 和 MapReduce 之上的。
每个Table都是一个多维的稀疏图 sparse map。Table 由行和列组成,并且每个存储单元 cell 都有一个时间戳。在不同的时间对同一个存储单元cell有多份拷贝,这样就可以记录数据的变动情况。在他的例子中,行是URLs ,列可以定义一个名字,比如:contents。Contents 字段就可以存储文件的数据。或者列名是:”language”,可以存储一个“EN”的语言代码字符串。
为了管理巨大的Table,把Table根据行分割,这些分割后的数据统称为:Tablets。每 个Tablets大概有 100-200 MB,每个机器存储100个左右的 Tablets。底层的架构是:GFS。由于GFS是一种分布式的文件系统,采用Tablets的机制后,可以获得很好的负载均衡。比如:可以把经常响应 的表移动到其他空闲机器上,然后快速重建。
Tablets在系统中的存储方式是不可修改的 immutable 的SSTables,一台机器一个日志文件。当系统的内存满后,系统会压缩一些Tablets。由于Jeff在论述这点的时候说的很快,所以我没有时间把听到的都记录下来,因此下面是一个大概的说明:
压缩分为:主要和次要的两部分。次要的压缩仅仅包括几个Tablets,而主要的压缩时关于整个系统的压缩。主压缩有回收硬盘空间的功能。Tablets的位置实际上是存储在几个特殊的BigTable的存储单元cell中。看起来这是一个三层的系统。
客户端有一个指向METAO的Tablets的指针。如果METAO的Tablets被频繁使用,那个这台机器就会放弃其他的tablets专门支持 METAO这个Tablets。METAO tablets 保持着所有的META1的tablets的记录。这些tablets中包含着查找tablets的实际位置。(老实说翻译到这里,我也不太明白。)在这个系统中不存在大的瓶颈,因为被频繁调用的数据已经被提前获得并进行了缓存。
现在我们返回到对列的说明:列是类似下面的形式: family:optional_qualifier。在他的例子中,行:www.search-analysis.com 也许有列:”contents:其中包含html页面的代码。 “ anchor:cnn.com/news” 中包含着 相对应的url,”anchor:www.search-analysis.com/” 包含着链接的文字部分。列中包含着类型信息。
(翻译到这里我要插一句,以前我看过一个关于万能数据库的文章,当时很激动,就联系了作者,现在回想起来,或许google的 bigtable 才是更好的方案,切不说分布式的特性,就是这种建华的表结构就很有用处。)
注意这里说的是列信息,而不是列类型。列的信息是如下信息,一般是:属性/规则。 比如:保存n份数据的拷贝或者保存数据n天长等等。当 tablets 重新建立的时候,就运用上面的规则,剔出不符合条件的记录。由于设计上的原因,列本身的创建是很容易的,但是跟列相关的功能确实非常复杂的,比如上文提到 的 类型和规则信息等。为了优化读取速度,列的功能被分割然后以组的方式存储在所建索引的机器上。这些被分割后的组作用于 列 ,然后被分割成不同的 SSTables。这种方式可以提高系统的性能,因为小的,频繁读取的列可以被单独存储,和那些大的不经常访问的列隔离开来。
在一台机器上的所有的 tablets 共享一个log,在一个包含1亿的tablets的集群中,这将会导致非常多的文件被打开和写操作。新的log块经常被创建,一般是64M大小,这个GFS的块大小相等。当一个机器down掉后,控制机器就会重新发布他的log块到其他机器上继续进行处理。这台机器重建tablets然后询问控制机器处理结构的存储位置,然后直接对重建后的数据进行处理。这个系统中有很多冗余数据,因此在系统中大量使用了压缩技术。
Dean 对压缩的部分说的很快,我没有完全记下来,所以我还是说个大概吧:压缩前先寻找相似的 \行,列,和时间数据。
他们使用不同版本的: BMDiff 和 Zippy 技术。
BMDiff 提供给他们非常快的写速度: 100MB/s – 1000MB/s 。Zippy 是和 LZW 类似的。Zippy 并不像 LZW 或者 gzip 那样压缩比高,但是他处理速度非常快。
Dean 还给了一个关于压缩 web 蜘蛛数据的例子。这个例子的蜘蛛 包含 2.1B 的页面,行按照以下的方式命名:“com.cnn.www/index.html:http”.在未压缩前的web page 页面大小是:45.1 TB ,压缩后的大小是:4.2 TB , 只是原来的 9.2%。Links 数据压缩到原来的 13.9% , 链接文本数据压缩到原来的 12.7%。
Continue Reading
Posted: January 14, 2008 at 1:24 pm | Tags: blog, boot, cache, class, flash, html, java, linux, loader, mtk, ror, Spreadtrum, windows, 串口, 内幕, 开发, 技术, 测试, 类
级别: 初级
詹荣开 (zhanrk@sohu.com), Linux爱好者
2003 年 12 月 01 日
本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念、软件设计的主要任务以及结构框架等内容。
1. 引言
在专用的嵌入式板子运行 GNU/Linux 系统已经变得越来越流行。一个嵌入式 Linux 系统从软件的角度看通常可以分为四个层次:
1. 引导加载程序。包括固化在固件(firmware)中的 boot 代码(可选),和 Boot Loader 两大部分。
2. Linux 内核。特定于嵌入式板子的定制内核以及内核的启动参数。
3. 文件系统。包括根文件系统和建立于 Flash 内存设备之上文件系统。通常用 ram disk 来作为 root fs。
4. 用户应用程序。特定于用户的应用程序。有时在用户应用程序和内核层之间可能还会包括一个嵌入式图形用户界面。常用的嵌入式 GUI 有:MicroWindows 和 MiniGUI 懂。
引导加载程序是系统加电后运行的第一段软件代码。回忆一下 PC 的体系结构我们可以知道,PC 机中的引导加载程序由 BIOS(其本质就是一段固件程序)和位于硬盘 MBR 中的 OS Boot Loader(比如,LILO 和 GRUB 等)一起组成。BIOS 在完成硬件检测和资源分配后,将硬盘 MBR 中的 Boot Loader 读到系统的 RAM 中,然后将控制权交给 OS Boot Loader。Boot Loader 的主要运行任务就是将内核映象从硬盘上读到 RAM 中,然后跳转到内核的入口点去运行,也即开始启动操作系统。
而在嵌入式系统中,通常并没有像 BIOS 那样的固件程序(注,有的嵌入式 CPU 也会内嵌一段短小的启动程序),因此整个系统的加载启动任务就完全由 Boot Loader 来完成。比如在一个基于 ARM7TDMI core 的嵌入式系统中,系统在上电或复位时通常都从地址 0×00000000 处开始执行,而在这个地址处安排的通常就是系统的 Boot Loader 程序。
本文将从 Boot Loader 的概念、Boot Loader 的主要任务、Boot Loader 的框架结构以及 Boot Loader 的安装等四个方面来讨论嵌入式系统的 Boot Loader。
2. Boot Loader 的概念
简单地说,Boot Loader 就是在操作系统内核运行之前运行的一段小程序。通过这段小程序,我们可以初始化硬件设备、建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,以便为最终调用操作系统内核准备好正确的环境。
通常,Boot Loader 是严重地依赖于硬件而实现的,特别是在嵌入式世界。因此,在嵌入式世界里建立一个通用的 Boot Loader 几乎是不可能的。尽管如此,我们仍然可以对 Boot Loader 归纳出一些通用的概念来,以指导用户特定的 Boot Loader 设计与实现。
1. Boot Loader 所支持的 CPU 和嵌入式板
每种不同的 CPU 体系结构都有不同的 Boot Loader。有些 Boot Loader 也支持多种体系结构的 CPU,比如 U-Boot 就同时支持 ARM 体系结构和MIPS 体系结构。除了依赖于 CPU 的体系结构外,Boot Loader 实际上也依赖于具体的嵌入式板级设备的配置。这也就是说,对于两块不同的嵌入式板而言,即使它们是基于同一种 CPU 而构建的,要想让运行在一块板子上的 Boot Loader 程序也能运行在另一块板子上,通常也都需要修改 Boot Loader 的源程序。
2. Boot Loader 的安装媒介(Installation Medium)
系统加电或复位后,所有的 CPU 通常都从某个由 CPU 制造商预先安排的地址上取指令。比如,基于 ARM7TDMI core 的 CPU 在复位时通常都从地址 0×00000000 取它的第一条指令。而基于 CPU 构建的嵌入式系统通常都有某种类型的固态存储设备(比如:ROM、EEPROM 或 FLASH 等)被映射到这个预先安排的地址上。因此在系统加电后,CPU 将首先执行 Boot Loader 程序。
下图1就是一个同时装有 Boot Loader、内核的启动参数、内核映像和根文件系统映像的固态存储设备的典型空间分配结构图。
图1 固态存储设备的典型空间分配结构

3. 用来控制 Boot Loader 的设备或机制
主机和目标机之间一般通过串口建立连接,Boot Loader 软件在执行时通常会通过串口来进行 I/O,比如:输出打印信息到串口,从串口读取用户控制字符等。
4. Boot Loader 的启动过程是单阶段(Single Stage)还是多阶段(Multi-Stage)
通常多阶段的 Boot Loader 能提供更为复杂的功能,以及更好的可移植性。从固态存储设备上启动的 Boot Loader 大多都是 2 阶段的启动过程,也即启动过程可以分为 stage 1 和 stage 2 两部分。而至于在 stage 1 和 stage 2 具体完成哪些任务将在下面讨论。
5. Boot Loader 的操作模式 (Operation Mode)
大多数 Boot Loader 都包含两种不同的操作模式:”启动加载”模式和”下载”模式,这种区别仅对于开发人员才有意义。但从最终用户的角度看,Boot Loader 的作用就是用来加载操作系统,而并不存在所谓的启动加载模式与下载工作模式的区别。
启动加载(Boot loading)模式:这种模式也称为”自主”(Autonomous)模式。也即 Boot Loader 从目标机上的某个固态存储设备上将操作系统加载到 RAM 中运行,整个过程并没有用户的介入。这种模式是 Boot Loader 的正常工作模式,因此在嵌入式产品发布的时侯,Boot Loader 显然必须工作在这种模式下。
下载(Downloading)模式:在这种模式下,目标机上的 Boot Loader 将通过串口连接或网络连接等通信手段从主机(Host)下载文件,比如:下载内核映像和根文件系统映像等。从主机下载的文件通常首先被 Boot Loader 保存到目标机的 RAM 中,然后再被 Boot Loader 写到目标机上的FLASH 类固态存储设备中。Boot Loader 的这种模式通常在第一次安装内核与根文件系统时被使用;此外,以后的系统更新也会使用 Boot Loader 的这种工作模式。工作于这种模式下的 Boot Loader 通常都会向它的终端用户提供一个简单的命令行接口。
像 Blob 或 U-Boot 等这样功能强大的 Boot Loader 通常同时支持这两种工作模式,而且允许用户在这两种工作模式之间进行切换。比如,Blob 在启动时处于正常的启动加载模式,但是它会延时 10 秒等待终端用户按下任意键而将 blob 切换到下载模式。如果在 10 秒内没有用户按键,则 blob 继续启动 Linux 内核。
6. BootLoader 与主机之间进行文件传输所用的通信设备及协议
最常见的情况就是,目标机上的 Boot Loader 通过串口与主机之间进行文件传输,传输协议通常是 xmodem/ymodem/zmodem 协议中的一种。但是,串口传输的速度是有限的,因此通过以太网连接并借助 TFTP 协议来下载文件是个更好的选择。
此外,在论及这个话题时,主机方所用的软件也要考虑。比如,在通过以太网连接和 TFTP 协议来下载文件时,主机方必须有一个软件用来的提供 TFTP 服务。
在讨论了 BootLoader 的上述概念后,下面我们来具体看看 BootLoader 的应该完成哪些任务。
3. Boot Loader 的主要任务与典型结构框架
在继续本节的讨论之前,首先我们做一个假定,那就是:假定内核映像与根文件系统映像都被加载到 RAM 中运行。之所以提出这样一个假设前提是因为,在嵌入式系统中内核映像与根文件系统映像也可以直接在 ROM 或 Flash 这样的固态存储设备中直接运行。但这种做法无疑是以运行速度的牺牲为代价的。
从操作系统的角度看,Boot Loader 的总目标就是正确地调用内核来执行。
另外,由于 Boot Loader 的实现依赖于 CPU 的体系结构,因此大多数 Boot Loader 都分为 stage1 和 stage2 两大部分。依赖于 CPU 体系结构的代码,比如设备初始化代码等,通常都放在 stage1 中,而且通常都用汇编语言来实现,以达到短小精悍的目的。而 stage2 则通常用C语言来实现,这样可以实现给复杂的功能,而且代码会具有更好的可读性和可移植性。
Boot Loader 的 stage1 通常包括以下步骤(以执行的先后顺序):
- 硬件设备初始化。
- 为加载 Boot Loader 的 stage2 准备 RAM 空间。
- 拷贝 Boot Loader 的 stage2 到 RAM 空间中。
- 设置好堆栈。
- 跳转到 stage2 的 C 入口点。
Boot Loader 的 stage2 通常包括以下步骤(以执行的先后顺序):
- 初始化本阶段要使用到的硬件设备。
- 检测系统内存映射(memory map)。
- 将 kernel 映像和根文件系统映像从 flash 上读到 RAM 空间中。
- 为内核设置启动参数。
- 调用内核。
3.1 Boot Loader 的 stage1
3.1.1 基本的硬件初始化
这是 Boot Loader 一开始就执行的操作,其目的是为 stage2 的执行以及随后的 kernel 的执行准备好一些基本的硬件环境。它通常包括以下步骤(以执行的先后顺序):
1. 屏蔽所有的中断。为中断提供服务通常是 OS 设备驱动程序的责任,因此在 Boot Loader 的执行全过程中可以不必响应任何中断。中断屏蔽可以通过写 CPU 的中断屏蔽寄存器或状态寄存器(比如 ARM 的 CPSR 寄存器)来完成。
2. 设置 CPU 的速度和时钟频率。
3. RAM 初始化。包括正确地设置系统的内存控制器的功能寄存器以及各内存库控制寄存器等。
4. 初始化 LED。典型地,通过 GPIO 来驱动 LED,其目的是表明系统的状态是 OK 还是 Error。如果板子上没有 LED,那么也可以通过初始化 UART 向串口打印 Boot Loader 的 Logo 字符信息来完成这一点。
5. 关闭 CPU 内部指令/数据 cache。
3.1.2 为加载 stage2 准备 RAM 空间
为了获得更快的执行速度,通常把 stage2 加载到 RAM 空间中来执行,因此必须为加载 Boot Loader 的 stage2 准备好一段可用的 RAM 空间范围。
由于 stage2 通常是 C 语言执行代码,因此在考虑空间大小时,除了 stage2 可执行映象的大小外,还必须把堆栈空间也考虑进来。此外,空间大小最好是 memory page 大小(通常是 4KB)的倍数。一般而言,1M 的 RAM 空间已经足够了。具体的地址范围可以任意安排,比如 blob 就将它的 stage2 可执行映像安排到从系统 RAM 起始地址 0xc0200000 开始的 1M 空间内执行。但是,将 stage2 安排到整个 RAM 空间的最顶 1MB(也即(RamEnd-1MB) – RamEnd)是一种值得推荐的方法。
为了后面的叙述方便,这里把所安排的 RAM 空间范围的大小记为:stage2_size(字节),把起始地址和终止地址分别记为:stage2_start 和 stage2_end(这两个地址均以 4 字节边界对齐)。因此:
stage2_end=stage2_start+stage2_size
|
另外,还必须确保所安排的地址范围的的确确是可读写的 RAM 空间,因此,必须对你所安排的地址范围进行测试。具体的测试方法可以采用类似于 blob 的方法,也即:以 memory page 为被测试单位,测试每个 memory page 开始的两个字是否是可读写的。为了后面叙述的方便,我们记这个检测算法为:test_mempage,其具体步骤如下:
1. 先保存 memory page 一开始两个字的内容。
2. 向这两个字中写入任意的数字。比如:向第一个字写入 0×55,第 2 个字写入 0xaa。
3. 然后,立即将这两个字的内容读回。显然,我们读到的内容应该分别是 0×55 和 0xaa。如果不是,则说明这个 memory page 所占据的地址范围不是一段有效的 RAM 空间。
4. 再向这两个字中写入任意的数字。比如:向第一个字写入 0xaa,第 2 个字中写入 0×55。
5. 然后,立即将这两个字的内容立即读回。显然,我们读到的内容应该分别是 0xaa 和 0×55。如果不是,则说明这个 memory page 所占据的地址范围不是一段有效的 RAM 空间。
6. 恢复这两个字的原始内容。测试完毕。
为了得到一段干净的 RAM 空间范围,我们也可以将所安排的 RAM 空间范围进行清零操作。
3.1.3 拷贝 stage2 到 RAM 中
拷贝时要确定两点:(1) stage2 的可执行映象在固态存储设备的存放起始地址和终止地址;(2) RAM 空间的起始地址。
3.1.4 设置堆栈指针 sp
堆栈指针的设置是为了执行 C 语言代码作好准备。通常我们可以把 sp 的值设置为(stage2_end-4),也即在 3.1.2 节所安排的那个 1MB 的 RAM 空间的最顶端(堆栈向下生长)。
此外,在设置堆栈指针 sp 之前,也可以关闭 led 灯,以提示用户我们准备跳转到 stage2。
经过上述这些执行步骤后,系统的物理内存布局应该如下图2所示。
3.1.5 跳转到 stage2 的 C 入口点
在上述一切都就绪后,就可以跳转到 Boot Loader 的 stage2 去执行了。比如,在 ARM 系统中,这可以通过修改 PC 寄存器为合适的地址来实现。
图2 bootloader 的 stage2 可执行映象刚被拷贝到 RAM 空间时的系统内存布局

3.2 Boot Loader 的 stage2
正如前面所说,stage2 的代码通常用 C 语言来实现,以便于实现更复杂的功能和取得更好的代码可读性和可移植性。但是与普通 C 语言应用程序不同的是,在编译和链接 boot loader 这样的程序时,我们不能使用 glibc 库中的任何支持函数。其原因是显而易见的。这就给我们带来一个问题,那就是从那里跳转进 main() 函数呢?直接把 main() 函数的起始地址作为整个 stage2 执行映像的入口点或许是最直接的想法。但是这样做有两个缺点:1)无法通过main() 函数传递函数参数;2)无法处理 main() 函数返回的情况。一种更为巧妙的方法是利用 trampoline(弹簧床)的概念。也即,用汇编语言写一段trampoline 小程序,并将这段 trampoline 小程序来作为 stage2 可执行映象的执行入口点。然后我们可以在 trampoline 汇编小程序中用 CPU 跳转指令跳入 main() 函数中去执行;而当 main() 函数返回时,CPU 执行路径显然再次回到我们的 trampoline 程序。简而言之,这种方法的思想就是:用这段 trampoline 小程序来作为 main() 函数的外部包裹(external wrapper)。
下面给出一个简单的 trampoline 程序示例(来自blob):
.text .globl _trampoline _trampoline: bl main /* if main ever returns we just call it again */ b _trampoline
|
可以看出,当 main() 函数返回后,我们又用一条跳转指令重新执行 trampoline 程序――当然也就重新执行 main() 函数,这也就是 trampoline(弹簧床)一词的意思所在。
3.2.1初始化本阶段要使用到的硬件设备
这通常包括:(1)初始化至少一个串口,以便和终端用户进行 I/O 输出信息;(2)初始化计时器等。
在初始化这些设备之前,也可以重新把 LED 灯点亮,以表明我们已经进入 main() 函数执行。
设备初始化完成后,可以输出一些打印信息,程序名字字符串、版本号等。
3.2.2 检测系统的内存映射(memory map)
所谓内存映射就是指在整个 4GB 物理地址空间中有哪些地址范围被分配用来寻址系统的 RAM 单元。比如,在 SA-1100 CPU 中,从 0xC000,0000 开始的 512M 地址空间被用作系统的 RAM 地址空间,而在 Samsung S3C44B0X CPU 中,从 0x0c00,0000 到 0×1000,0000 之间的 64M 地址空间被用作系统的 RAM 地址空间。虽然 CPU 通常预留出一大段足够的地址空间给系统 RAM,但是在搭建具体的嵌入式系统时却不一定会实现 CPU 预留的全部 RAM 地址空间。也就是说,具体的嵌入式系统往往只把 CPU 预留的全部 RAM 地址空间中的一部分映射到 RAM 单元上,而让剩下的那部分预留 RAM 地址空间处于未使用状态。 由于上述这个事实,因此 Boot Loader 的 stage2 必须在它想干点什么 (比如,将存储在 flash 上的内核映像读到 RAM 空间中) 之前检测整个系统的内存映射情况,也即它必须知道 CPU 预留的全部 RAM 地址空间中的哪些被真正映射到 RAM 地址单元,哪些是处于 “unused” 状态的。
(1) 内存映射的描述
可以用如下数据结构来描述 RAM 地址空间中的一段连续(continuous)的地址范围:
typedef struct memory_area_struct { u32 start; /* the base address of the memory region */ u32 size; /* the byte number of the memory region */ int used; } memory_area_t;
|
这段 RAM 地址空间中的连续地址范围可以处于两种状态之一:(1)used=1,则说明这段连续的地址范围已被实现,也即真正地被映射到 RAM 单元上。(2)used=0,则说明这段连续的地址范围并未被系统所实现,而是处于未使用状态。
基于上述 memory_area_t 数据结构,整个 CPU 预留的 RAM 地址空间可以用一个 memory_area_t 类型的数组来表示,如下所示:
memory_area_t memory_map[NUM_MEM_AREAS] = { [0 ... (NUM_MEM_AREAS - 1)] = { .start = 0, .size = 0, .used = 0 }, };
|
(2) 内存映射的检测
下面我们给出一个可用来检测整个 RAM 地址空间内存映射情况的简单而有效的算法:
/* 数组初始化 */ for(i = 0; i < NUM_MEM_AREAS; i++) memory_map[i].used = 0; /* first write a 0 to all memory locations */ for(addr = MEM_START; addr < MEM_END; addr += PAGE_SIZE) * (u32 *)addr = 0; for(i = 0, addr = MEM_START; addr < MEM_END; addr += PAGE_SIZE) { /* * 检测从基地址 MEM_START+i*PAGE_SIZE 开始,大小为 * PAGE_SIZE 的地址空间是否是有效的RAM地址空间。 */ 调用3.1.2节中的算法test_mempage(); if ( current memory page isnot a valid ram page) { /* no RAM here */ if(memory_map[i].used ) i++; continue; } /* * 当前页已经是一个被映射到 RAM 的有效地址范围 * 但是还要看看当前页是否只是 4GB 地址空间中某个地址页的别名? */ if(* (u32 *)addr != 0) { /* alias? */ /* 这个内存页是 4GB 地址空间中某个地址页的别名 */ if ( memory_map[i].used ) i++; continue; } /* * 当前页已经是一个被映射到 RAM 的有效地址范围 * 而且它也不是 4GB 地址空间中某个地址页的别名。 */ if (memory_map[i].used == 0) { memory_map[i].start = addr; memory_map[i].size = PAGE_SIZE; memory_map[i].used = 1; } else { memory_map[i].size += PAGE_SIZE; } } /* end of for (…) */
|
在用上述算法检测完系统的内存映射情况后,Boot Loader 也可以将内存映射的详细信息打印到串口。
3.2.3 加载内核映像和根文件系统映像
(1) 规划内存占用的布局
这里包括两个方面:(1)内核映像所占用的内存范围;(2)根文件系统所占用的内存范围。在规划内存占用的布局时,主要考虑基地址和映像的大小两个方面。
对于内核映像,一般将其拷贝到从(MEM_START+0×8000) 这个基地址开始的大约1MB大小的内存范围内(嵌入式 Linux 的内核一般都不操过 1MB)。为什么要把从 MEM_START 到 MEM_START+0×8000 这段 32KB 大小的内存空出来呢?这是因为 Linux 内核要在这段内存中放置一些全局数据结构,如:启动参数和内核页表等信息。
而对于根文件系统映像,则一般将其拷贝到 MEM_START+0×0010,0000 开始的地方。如果用 Ramdisk 作为根文件系统映像,则其解压后的大小一般是1MB。
(2)从 Flash 上拷贝
由于像 ARM 这样的嵌入式 CPU 通常都是在统一的内存地址空间中寻址 Flash 等固态存储设备的,因此从 Flash 上读取数据与从 RAM 单元中读取数据并没有什么不同。用一个简单的循环就可以完成从 Flash 设备上拷贝映像的工作:
while(count) { *dest++ = *src++; /* they are all aligned with word boundary */ count -= 4; /* byte number */ };
|
3.2.4 设置内核的启动参数
应该说,在将内核映像和根文件系统映像拷贝到 RAM 空间中后,就可以准备启动 Linux 内核了。但是在调用内核之前,应该作一步准备工作,即:设置 Linux 内核的启动参数。
Linux 2.4.x 以后的内核都期望以标记列表(tagged list)的形式来传递启动参数。启动参数标记列表以标记 ATAG_CORE 开始,以标记 ATAG_NONE 结束。每个标记由标识被传递参数的 tag_header 结构以及随后的参数值数据结构来组成。数据结构 tag 和 tag_header 定义在 Linux 内核源码的include/asm/setup.h 头文件中:
/* The list ends with an ATAG_NONE node. */ #define ATAG_NONE 0x00000000 struct tag_header { u32 size; /* 注意,这里size是字数为单位的 */ u32 tag; }; …… struct tag { struct tag_header hdr; union { struct tag_core core; struct tag_mem32 mem; struct tag_videotext videotext; struct tag_ramdisk ramdisk; struct tag_initrd initrd; struct tag_serialnr serialnr; struct tag_revision revision; struct tag_videolfb videolfb; struct tag_cmdline cmdline; /* * Acorn specific */ struct tag_acorn acorn; /* * DC21285 specific */ struct tag_memclk memclk; } u; };
|
在嵌入式 Linux 系统中,通常需要由 Boot Loader 设置的常见启动参数有:ATAG_CORE、ATAG_MEM、ATAG_CMDLINE、ATAG_RAMDISK、ATAG_INITRD等。
比如,设置 ATAG_CORE 的代码如下:
params = (struct tag *)BOOT_PARAMS; params->hdr.tag = ATAG_CORE; params->hdr.size = tag_size(tag_core); params->u.core.flags = 0; params->u.core.pagesize = 0; params->u.core.rootdev = 0; params = tag_next(params);
|
其中,BOOT_PARAMS 表示内核启动参数在内存中的起始基地址,指针 params 是一个 struct tag 类型的指针。宏 tag_next() 将以指向当前标记的指针为参数,计算紧临当前标记的下一个标记的起始地址。注意,内核的根文件系统所在的设备ID就是在这里设置的。
下面是设置内存映射情况的示例代码:
for(i = 0; i < NUM_MEM_AREAS; i++) { if(memory_map[i].used) { params->hdr.tag = ATAG_MEM; params->hdr.size = tag_size(tag_mem32); params->u.mem.start = memory_map[i].start; params->u.mem.size = memory_map[i].size; params = tag_next(params); } }
|
可以看出,在 memory_map[]数组中,每一个有效的内存段都对应一个 ATAG_MEM 参数标记。
Linux 内核在启动时可以以命令行参数的形式来接收信息,利用这一点我们可以向内核提供那些内核不能自己检测的硬件参数信息,或者重载(override)内核自己检测到的信息。比如,我们用这样一个命令行参数字符串”console=ttyS0,115200n8″来通知内核以 ttyS0 作为控制台,且串口采用 “115200bps、无奇偶校验、8位数据位”这样的设置。下面是一段设置调用内核命令行参数字符串的示例代码:
char *p; /* eat leading white space */ for(p = commandline; *p == ' '; p++) ; /* skip non-existent command lines so the kernel will still * use its default command line. */ if(*p == '\0') return; params->hdr.tag = ATAG_CMDLINE; params->hdr.size = (sizeof(struct tag_header) + strlen(p) + 1 + 4) >> 2; strcpy(params->u.cmdline.cmdline, p); params = tag_next(params);
|
请注意在上述代码中,设置 tag_header 的大小时,必须包括字符串的终止符’\0′,此外还要将字节数向上圆整4个字节,因为 tag_header 结构中的size 成员表示的是字数。
下面是设置 ATAG_INITRD 的示例代码,它告诉内核在 RAM 中的什么地方可以找到 initrd 映象(压缩格式)以及它的大小:
params->hdr.tag = ATAG_INITRD2; params->hdr.size = tag_size(tag_initrd); params->u.initrd.start = RAMDISK_RAM_BASE; params->u.initrd.size = INITRD_LEN; params = tag_next(params);
|
下面是设置 ATAG_RAMDISK 的示例代码,它告诉内核解压后的 Ramdisk 有多大(单位是KB):
params->hdr.tag = ATAG_RAMDISK; params->hdr.size = tag_size(tag_ramdisk); params->u.ramdisk.start = 0; params->u.ramdisk.size = RAMDISK_SIZE; /* 请注意,单位是KB */ params->u.ramdisk.flags = 1; /* automatically load ramdisk */ params = tag_next(params);
|
最后,设置 ATAG_NONE 标记,结束整个启动参数列表:
static void setup_end_tag(void) { params->hdr.tag = ATAG_NONE; params->hdr.size = 0; }
|
3.2.5 调用内核
Boot Loader 调用 Linux 内核的方法是直接跳转到内核的第一条指令处,也即直接跳转到 MEM_START+0×8000 地址处。在跳转时,下列条件要满足:
1. CPU 寄存器的设置:
- R0=0;
- R1=机器类型 ID;关于 Machine Type Number,可以参见 linux/arch/arm/tools/mach-types。
- R2=启动参数标记列表在 RAM 中起始基地址;
2. CPU 模式:
- 必须禁止中断(IRQs和FIQs);
- CPU 必须 SVC 模式;
3. Cache 和 MMU 的设置:
- MMU 必须关闭;
- 指令 Cache 可以打开也可以关闭;
- 数据 Cache 必须关闭;
如果用 C 语言,可以像下列示例代码这样来调用内核:
void (*theKernel)(int zero, int arch, u32 params_addr) = (void (*)(int, int, u32))KERNEL_RAM_BASE; …… theKernel(0, ARCH_NUMBER, (u32) kernel_params_start);
|
注意,theKernel()函数调用应该永远不返回的。如果这个调用返回,则说明出错。
4. 关于串口终端
在 boot loader 程序的设计与实现中,没有什么能够比从串口终端正确地收到打印信息能更令人激动了。此外,向串口终端打印信息也是一个非常重要而又有效的调试手段。但是,我们经常会碰到串口终端显示乱码或根本没有显示的问题。造成这个问题主要有两种原因:(1) boot loader 对串口的初始化设置不正确。(2) 运行在 host 端的终端仿真程序对串口的设置不正确,这包括:波特率、奇偶校验、数据位和停止位等方面的设置。
此外,有时也会碰到这样的问题,那就是:在 boot loader 的运行过程中我们可以正确地向串口终端输出信息,但当 boot loader 启动内核后却无法看到内核的启动输出信息。对这一问题的原因可以从以下几个方面来考虑:
(1) 首先请确认你的内核在编译时配置了对串口终端的支持,并配置了正确的串口驱动程序。
(2) 你的 boot loader 对串口的初始化设置可能会和内核对串口的初始化设置不一致。此外,对于诸如 s3c44b0x 这样的 CPU,CPU 时钟频率的设置也会影响串口,因此如果 boot loader 和内核对其 CPU 时钟频率的设置不一致,也会使串口终端无法正确显示信息。
(3) 最后,还要确认 boot loader 所用的内核基地址必须和内核映像在编译时所用的运行基地址一致,尤其是对于 uClinux 而言。假设你的内核映像在编译时用的基地址是 0xc0008000,但你的 boot loader 却将它加载到 0xc0010000 处去执行,那么内核映像当然不能正确地执行了。
5. 结束语
Boot Loader 的设计与实现是一个非常复杂的过程。如果不能从串口收到那激动人心的”uncompressing linux……………… done, booting the kernel……”内核启动信息,恐怕谁也不能说:”嗨,我的 boot loader 已经成功地转起来了!”。
关于作者
Posted: March 24, 2007 at 9:50 pm | Tags: class, java, javascript, server, shell, web, windows
转至http://www.codeproject.com
Introduction
Having recieved a number of requests for a tutorial of sorts on developing Internet Explorer Toolbars with the RBDeskband and CWindowImpl wizards that I created, I have come up with a simple sample toolbar which can be used as a reference when developing your own toolbars or explorer bars. The tutorial will walk you through the stages of developing a toolbar for IE that is very similar to the Address bar that is already present in IE. I wanted to do a tutorial that would provide a realistic sample and would produce an end result that could be used by others after the tutorial was finish. So, the tutorial is going to show you how to develop an IE toolbar to get stock quote information from The Motley Fool website. So with that, let us get started.
Prequisites
This tutorial assumes that you already know how to program in C++ and know some information about ATL and COM. To work through this tutorial, you will need the following installed on your development machine:
- Visual C++6 installed
- RBDeskBand ATL Object Wizard (Version 2.0) [get it here]
- CWindowImpl ATL Object Wizard [get it here]
The Framework
The IE toolbar consists of a COM component supporting IDeskband and a few other necessary interfaces for which IE looks for when loading registered toolbars, explorer bars and deskbands. The RBDeskband ATL Object Wizard provides most of the framework for this article. What we will need to do is create our project, a new COM object to house our toolbar, and a few CWindowImpl classes using the CWindowImpl ATL Object Wizard. Then connecting these parts together we will produce the IE toolbar in the picture at the top of the article. Visually the toolbar consists of an editbox and a toolbar with one button on it. In actuality the toolbar consists of the fore mentioned and a non visible window that is used to reflect messages to the Toolbar window, which will process or forward messges to itself and the edit box.
Creating The Shell
We will not work through the steps in creating the shell for our toolbar.
Creating The Project
- If you have not done so already, start Visual C++6.
- Then, from the File menu select New menu item; the New Dialog pops up.
- In the New Dialog, select the Projects tab, if not already selected.
- Select ATL COM AppWizard from the list view, if not already selected.
- In the Project name, type "MotleyFool". See Figure 1.
- Click the OK Button.
Figure 1. New Dialog.
- The ATL COM AppWizard will kick in.
- Clicking the Finish Button, accepting the default AppWizard attributes. See Figure 2.
- The New Project Information Dialog will present itself requesting confirmation of your project settings.
- Click the OK Button.
Figure 2. ATL COM AppWizard.
Creating The DeskBand Object
Now that we have our project container we need to add our IDeskBand derived component so that the DLL actually exposes something.
- From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked.
- In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed.
- Next select the DeskBand item from the Objects list.
- Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3.
- On the Names property page, type "StockBar" into the Short Name field. See Figure 4.
- Select the DeskBand ATL Object Wizard property page
- Check the Internet Explorer Toolbar checkbox. See Figure 5.
- Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our DeskBand’s base implementation.
Figure 3. ATL Object Wizard.
Figure 4. ATL Object Wizard Properties – Names.
Figure 5. ATL Object Wizard Properties – DeskBand ATL Object Wizard
Now our project has the DeskBand implementation that we will modify to produce the toolbar pictured at the top of the article. First we will create the window classes we will need and then come back to the Desbkand object and update it to use our window classes.
Creating The Window Classes
So back in the Framework section we said that we would need three window classes. One for the Edit Box, one for the toolbar, and one for message reflection back to the toolbar. Let us now create these window classes.
The Edit Window
We need to create a derived class from the standard EDIT button window class because we are going to be adding methods to our class to help support functionality of the toolbar. This is one reason why we cannot use a CContainedWindow object directly.
- From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked.
- In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed.
- Next select the CWindowImpl item from the Objects list.
- Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3.
- On the Names property page, type "EditQuote" into the Short Name field.
- Select the CWindowImpl property page. See Figure 6.
- Select the SUPERCLASS radio button from the DECLAR_WND_* group.
- In the Window Class Name field, type "EDITQUOTE".
- In the Original Class Name list, select the EDIT listbox item. See Figure 7.
- Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation.
Figure 6. ATL Object Wizard Properties – Names.
Figure 7. ATL Object Wizard Properties – CWindowImpl.
The Toolbar Window
We need to create a derived class from the standard TOOLBARCLASSNAME window class because we are going to be adding methods to our class to help support functionality of the toolbar. It will also be the parent for the edit box and the window which the IE host will request from our DeskBand.
- From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked.
- In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed.
- Next select the CWindowImpl item from the Objects list.
- Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3.
- On the Names property page, type "MFToolbar" into the Short Name field.
- Select the CWindowImpl property page. See Figure 8.
- Select the SUPERCLASS radio button from the DECLAR_WND_* group.
- In the Window Class Name field, type "MOTLEYFOOLTOOLBAR".
- In the Original Class Name list, select the TOOLBARCLASSNAME listbox item. See Figure 9.
- Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation.
Figure 8. ATL Object Wizard Properties – Names.
Figure 9. ATL Object Wizard Properties – CWindowImpl.
The Reflection Window
We need to create a reflection window. It’s just a CWindowImpl window implmented class. We are going to be adding a small bit of functionality just to create the toolbar object and be able to access the toolbar member from our deskband class.
- From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked.
- In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed.
- Next select the CWindowImpl item from the Objects list.
- Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3.
- On the Names property page, type "ReflectionWnd" into the Short Name field. See Figure 10.
- We will not change any of the CWindowImpl property page values this time.
- Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation.
Figure 10. ATL Object Wizard Properties – Names.
Adding The Details
Now that we have our window classes available we can add our functionality for our toolbar to the appropriate window classes. Let us start with the deepest window class and work our way back out.
The EditQuote Details
For the EditQuote implementation, we need to be able to process keystrokes from the user and let the host that created our deskband object know our edit box has focus. To accomplish the first part, we need to look ahead and see that our DeskBand object will be implementing the IInputObject interface. So the host will query for that interface and know that we want to recieve messages and be given the chance to recieve focus. When the host sends our band messages to process they come through the IInputObject::TranslateAccelerator method. Our DeskBand will implement this method and it is best if our edit box, which will process the message, copy the TranslateAcceleratorIO method definition so our deskband can forward the message easily through a logical method call.
Figure 11. FileView Pane.
In the FileView pane (See Figure 11), double click the EditQuote.h item under Header Files. This will open the header file in the editing area. We now need to define the method definition for TranslateAcceleratorIO. To do this, add below the virtual CEditQuote destructor the following line of code:
STDMETHOD(TranslateAcceleratorIO)(LPMSG lpMsg);
Now Open the EditQuote.cpp source file and add the implementation of TranslateAcceleratorIO to the file
STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){ TranslateMessage(lpMsg); DispatchMessage(lpMsg); return S_OK;}
Now our DeskBand implementation can call this message and the edit box will process the key strokes properly. But wait, our edit box should notify the toolbar to load the quote details entered if the key stroke is the enter key. For this, we will need to define a message id and send that message to the parent window to process. In the EditQuote.h header file below the include statement, add the definition of our message id as shown below in bold.
#include <commctrl.h>const int WM_GETQUOTE = WM_USER + 1024;
In our EditQuote.cpp file we will add code to our TranslateAcceleratorIO method to process the enter key. Add the code below in bold to the EditQuote.cpp file.
STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){ int nVirtKey = (int)(lpMsg->wParam); if (VK_RETURN == nVirtKey) { lpMsg->wParam = 0; ::PostMessage(GetParent(), WM_GETQUOTE, 0, 0); return S_OK; } TranslateMessage(lpMsg); DispatchMessage(lpMsg); return S_OK;}
Now our edit box will notify the parent when the user presses the enter key so that the parent can retrieve the requested ticker symbol details, this part will be implemented when we get to the toolbar details.
The first part of the Edit boxes implementation is finished. Now we need to be able for the edit box to have the deskband notify the host that we have focus or that we don’t have focus any longer. To do this we will need to add a method for the deskband to pass us it’s address so that we can call a method of the deskband class. These next steps will involve adding code to the CEditQuote class and to our Deskband class implementation.
Open the EditQuote.h file and add a forward reference to the CStockBar class so that we can defined our methods and members in our class header without knowing the implementation details of our deskband class, add the line in bold.
#include <commctrl.h>const int WM_GETQUOTE = WM_USER + 1024;class CStockBar;
For our class to notify the host that our deskband has focus, we need to add a message handler for EN_SETFOCUS. Add the command code handler code below in bold to your EditQuote.h file.
BEGIN_MSG_MAP(CEditQuote) COMMAND_CODE_HANDLER(EN_SETFOCUS, OnSetFocus) END_MSG_MAP()
Then add the method definition for OnSetFocus to the header file below the commented out handler prototypes as follows below in bold.
LRESULT OnSetFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
Before we implement the OnSetFocus method, we need to define a method for our deskband to tells of it’s address and to retain that address for later use. Add the following lines of code to your EditQuote.h file below the TranslateAcceleratorIO definition.
void SetBand(CStockBar* pBand);private: CStockBar* m_pBand;
Now we can move to your EditQuote.cpp source file and implement the message handler, the SetBand method, and update the TranslateAcceleratorIO method for focus change. At the top of the EditQuote.cpp file add the following includes to the include list as shown below in bold.
#include "stdafx.h"#include "EditQuote.h"#include "MotleyFool.h"#include "StockBar.h"
Now when we can use the methods of the CStockBar class in our code. Add to the end of the constructor, the initalization of m_pBand. Don’t forget the colon operator.
CEditQuote::CEditQuote(): m_pBand(NULL){}
Next we will add the SetBand implementation to our CEditQuote class. Notice that since it is not a com object we don’t call AddRef or Release on it. It’s just a pointer to the class and when it’s destroyed our CEditQuote instance will also be destroyed. We could have also done this inline in our header file.
void CEditQuote::SetBand(CStockBar* pBand){ m_pBand = pBand;}
Next we need to add our message handler for our EN_SETFOCUS message. Add the code below to the end of the EditQuote.cpp source file.
LRESULT CEditQuote::OnSetFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled){ if (m_pBand) m_pBand->FocusChange(TRUE); return 0;}
We have one more section of code to add to our CEditQuote implementation then we can move to our CStockBar class to define and implement the FocusChange method. Add the following code to the CEditQuote TranslateAcceleratorIO method as shown in bold. We add this code so the host knows that we are no longer needing messages.
STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){ int nVirtKey = (int)(lpMsg->wParam); if (VK_RETURN == nVirtKey) { lpMsg->wParam = 0; ::PostMessage(GetParent(), WM_GETQUOTE, 0, 0); return S_OK; } else if (WM_KEYDOWN == lpMsg->message && nVirtKey == VK_TAB) { if (m_pBand) m_pBand->FocusChange(FALSE); return S_FALSE; } TranslateMessage(lpMsg); DispatchMessage(lpMsg); return S_OK;}
Open the StockBar.h header file and add the definition of FocusChange to it as shown below in bold.
public: void FocusChange(BOOL bHaveFocus);
Now open the StockBar.cpp source file and add the implementation of FocusChange to it at the bottom.
void CStockBar::FocusChange(BOOL bHaveFocus){ if (m_pSite) { IUnknown* pUnk = NULL; if (SUCCEEDED(QueryInterface(IID_IUnknown, (LPVOID*)&pUnk)) && pUnk != NULL) { m_pSite->OnFocusChangeIS(pUnk, bHaveFocus); pUnk->Release(); pUnk = NULL; } }}
We have finished off the work needed for the edit box to work properly in our toolbar. Now we need to build our toolbar up so that it has a button and contains our edit box. Then we will add the nessecities to our reflection window and update our IDeskBand to provide the correct information to our host. We are almost there. If you were to compile the project and run it, it would except that the band would look like the following in figure X.
The MFToolbar Details
For the implementation of the MFToolbar window, we need to be able to have it do the following things. It must be able to process the WM_GETQUOTE message from the EditQuote window, communicate with the web browser in which the toolbar is located, create the buttons and place the child windows on itself, forward messages to the EditQuote child window and size itself appropriately to the users actions.
So, the first thing we should do since our toolbar is going to contain an instance of CEditQuote is include the header file for the CEditQuote class. We will do this by opening the MFToolbar.h file and inserting the include statement for the CEditQuote class as shown in bold below.
#include <commctrl.h>#include "EditQuote.h"
Next we need to add a member to our toolbar class for the CEditQuote class. We will do this by adding a private section to the end of our class and defining a member variable as shown below in bold.
CMFToolbar(); virtual ~CMFToolbar();private: CEditQuote m_EditWnd;
Now that we have our member defined for our EditQuote window, we need to forward window messages to it so that keyboard inputs are processed appropriately. We do this by updating the toolbar message map to chain messages to our member as shown below in bold.
BEGIN_MSG_MAP(CMFToolbar) CHAIN_MSG_MAP_MEMBER(m_EditWnd) END_MSG_MAP()
Looking forward, our deskband will need to get the EditQuote member to deterimine if it has focus and also to make it function. We could just expose the EditQuote member directly by having made it a public member instead of private, but by making it private we can expose a method that will expose our member giving us flexibility later to modify the class if the need should arise. So to expose the EditQuote member, we will add a fuction to our toolbar class to return the reference to the EditQuote member. In the toolbar header file, add the method definition and implementation below in bold to it.
CMFToolbar(); virtual ~CMFToolbar(); inline CEditQuote& GetEditBox() {return m_EditWnd;};
Now we will create our toolbar window. Our toolbar consists of the EditQuote box and a button with an icon and text on it. To house the icon, our toolbar will need an image list handle to send to the toolbar window. So we need to add a few things to our toolbar header file before we go and implement the toolbar’s creation. The first thing we will add is the member variable for our image list. Add the line in bold below to your toolbar header file.
private: CEditQuote m_EditWnd; HIMAGELIST m_hImageList;
Then we will add a message handler to our toolbar’s message map and define the message handlers function definition to our header file and the follow lines of code in bold to your header file.
BEGIN_MSG_MAP(CMFToolbar) CHAIN_MSG_MAP_MEMBER(m_EditWnd) MESSAGE_HANDLER(WM_CREATE, OnCreate) END_MSG_MAP() LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
Before we can implement our toolbar’s creation, we need to create a icon resource that our toolbar button will use next to its text. So go to the resource view and add a new icon to the project resources. You can do this by right clicking on "MotleyFool resources" and selecting "Insert…" from the context menu. In the Insert Resource dialog box, select Icon from the Resource type list and click the New button. This will insert a blank icon resource into your project. Rename the icon’s resource ID by right clicking on the icon resource in the resource view and selecting the properties menu item from the context menu. Change the id to IDI_MOTLEY. Then draw or graciously borrow an icon from The Motley Fool to use on the toolbar. I graciously borrowed the icon from their website and adapted it into the icon.
Now we can implement it our toolbars creation. Open the MFToolbar source file and implement the details of the toolbar creation as described below.
First we need to include the project resource file so we can use the icon ID in our code. Add the line in bold below to our toolbar’s source file as shown.
#include "stdafx.h"#include "resource.h"#include "MFToolbar.h"
Next we need to update our constructor implementation. We need to initialize our handle to the image list by setting it to NULL. Don’t forget the colon.
CMFToolbar::CMFToolbar(): m_hImageList(NULL){}
Next we need to update our destructor, it should destroy the image list and destroy the window if it has not yet been destroyed.
CMFToolbar::~CMFToolbar(){ ImageList_Destroy(m_hImageList); if (IsWindow()) DestroyWindow();}
Before we can implement our toolbar’s creation, we need to add a resource symbol to our project for the toolbar button’s ID. We could just use a #define statement at the top of the source file, but for cleanliness and since we are already including the resource.h file, we will add it to our resource file. Go to the "View" menu and select "Resource Symbols" menu item. Click the "New" button on the Resource Symbols dialog. Then enter a name of "IDM_GETQUOTE" and click OK. Then close the Resource Symbols dialog.
Now we can create our toolbar, we defined the OnCreate method in our header file and need to now implement it. Add the following function and its implementation to the end of the toolbar source file.
Collapse
LRESULT CMFToolbar::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ SendMessage(m_hWnd, TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_MIXEDBUTTONS); SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); SendMessage(m_hWnd, TB_SETMAXTEXTROWS, 1, 0L); TCHAR* pCaption = _T("Get Quote"); int iIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, 0,(LPARAM)pCaption); HICON hMotley = LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_MOTLEY)); m_hImageList = ImageList_Create(16,16, ILC_COLOR16, 1, 0); int iImageIndex = ImageList_AddIcon(m_hImageList, hMotley); DestroyIcon(hMotley); ::SendMessage(m_hWnd, TB_SETIMAGELIST, 0, (LPARAM)m_hImageList); TBBUTTON Button; ZeroMemory((void*)&Button, sizeof(TBBUTTON)); Button.idCommand = IDM_GETQUOTE; Button.fsState = TBSTATE_ENABLED; Button.fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT; Button.dwData = 0; Button.iString = iIndex; Button.iBitmap = 0; ::SendMessage(m_hWnd, TB_INSERTBUTTON, 0, (LPARAM)&Button); RECT rect = {0,0,0,0}; m_EditWnd.Create(m_hWnd, rect, NULL, WS_CHILD|WS_VISIBLE, WS_EX_CLIENTEDGE); m_EditWnd.SetFont(static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))); return 0;}
If you try to compile at this point, you will see that there are unresolved externals for the image list method calls. We need to add a library to the project. To do this select the "Project|Settings" menu item. On the Project Settings dialog, Select All Configurations from the "Settings For" combo box. Then select the "Link" tab and append to the "Object/Library modules" edit box "comctl32.lib". Then click OK. If you compile the project now, it will compile successfully and the image list unresolved externals will disappear.
We still have a few things we need to do to the Toolbar window. It needs to process Command messages, resopnd to WM_GETQUOTE messages, and resize itself. Let’s conquer the latter first.
To orgainze the tooblar correctly, we should have the toolbar responsd to WM_SIZE messages. To do this, we will add to our tooblar header file a message handler for the WM_SIZE message and add a function definition for OnSize which WM_SIZE messages will be sent to. Open our toolbar header file and add the lines in bold below to it as shown.
BEGIN_MSG_MAP(CMFToolbar) CHAIN_MSG_MAP_MEMBER(m_EditWnd) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_SIZE, OnSize) END_MSG_MAP() LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
Now we need to implement our OnSize function. Open the toolbar source file and add the function implementation below to the end of the file.
Collapse
LRESULT CMFToolbar::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ RECT wndRect, btnRect; GetClientRect(&wndRect); ::SendMessage(m_hWnd, TB_GETITEMRECT, 0, (LPARAM)&btnRect); wndRect.right -= (btnRect.right - btnRect.left); SendMessage(TB_SETINDENT, wndRect.right - wndRect.left); wndRect.right -= 3; m_EditWnd.MoveWindow(&wndRect, FALSE); return 0;}
We still need to respond to user input for the toolbar button and from the edit box when the user presses the enter key. What we want the toolbar to do is tell the web browser host to navigate to the motely fool website and retrieve the stock quotes requested. First we need for the Deskband object to tell our toolbar window what the web browser instance is so that the toolbar window can communicate with it. To do this, we will add a private member variable and a public method in which the deskband can set the web browser instance. Our window will then use the member variable set to tell the web browser where to navigate and what to retrieve.
To do this, open the toolbar header file and add the lines in bold to the file.
CMFToolbar(); virtual ~CMFToolbar(); inline CEditQuote& GetEditBox() {return m_EditWnd;}; void SetBrowser(IWebBrowser2* pBrowser);private: CEditQuote m_EditWnd; HIMAGELIST m_hImageList; IWebBrowser2* m_pBrowser;
Now, open the toolbar source file. We will update the constructor and initialize our member variable to null. Then we will update the toolbar destructor and release the member variable if it hasn’t been. Then we will implement the SetBrowser method.
Initialize the web browser member variable.
CMFToolbar::CMFToolbar(): m_hImageList(NULL), m_pBrowser(NULL){}
Release the web browser object if held.
CMFToolbar::~CMFToolbar(){ ImageList_Destroy(m_hImageList); SetBrowser(NULL); if (IsWindow()) DestroyWindow();}
Implement SetBrowser()
void CMFToolbar::SetBrowser(IWebBrowser2* pBrowser){ if (m_pBrowser) m_pBrowser->Release(); m_pBrowser = pBrowser; if (m_pBrowser) m_pBrowser->AddRef();}
If you try and compile the project, you will notice that IWebBrowser2 is undefine in our header file. This is because we need to update our stdafx.h header file to include system files that define IWebBrowser2. To do this, open stdafx.h and add the following lines in bold to the file, then recompile.
extern CComModule _Module;#include <atlcom.h>#include <atlwin.h>#include <shlguid.h>#include <shlobj.h>
Now we can add message handlers for WM_COMMAND and WM_GETQUOTE to our toolbar class to handle the toolbar button being pressed and the enter key being pressed in the edit box by the user. To do this, we will need to add to our toolbar header file message handlers and function definitions for WM_COMMAND and WM_GETQUOTE. We will also need to add a private method which both will call if they need to to preform the same functionality (better than repeating code that does the same thing). So let’s add the message handlers to the header file.
Collapse
BEGIN_MSG_MAP(CMFToolbar) CHAIN_MSG_MAP_MEMBER(m_EditWnd) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_SIZE, OnSize) MESSAGE_HANDLER(WM_COMMAND, OnCommand) MESSAGE_HANDLER(WM_GETQUOTE, OnGetQuote) END_MSG_MAP() LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnGetQuote(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
Now we can add the function delcaration for our GetQuote privaet method.
private: CEditQuote m_EditWnd; HIMAGELIST m_hImageList; IWebBrowser2* m_pBrowser; void GetQuote();
Now let’s switch to our source file and implement our message handler functions and the GetQuote method. Add the code below to the end of the toolbar source file.
Collapse
LRESULT CMFToolbar::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ if (!HIWORD(wParam)) { long lSite = LOWORD(wParam); if ( lSite == IDM_GETQUOTE) { GetQuote(); return 0; } } return -1;}LRESULT CMFToolbar::OnGetQuote(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ GetQuote(); return 0;}void CMFToolbar::GetQuote(){ if (m_pBrowser) { VARIANT vEmpty; VariantInit(&vEmpty); m_pBrowser->Stop(); _bstr_t bsSite; if (m_EditWnd.GetWindowTextLength()) { BSTR bstrTickers = NULL; m_EditWnd.GetWindowText(&bstrTickers); bsSite = "http: bsSite += bstrTickers; SysFreeString(bstrTickers); } else bsSite = "http: m_pBrowser->Navigate(bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty); }}
If you try to compile, you will notice that _bstr_t is undefined. That is because the class is defined in comdef.h. We need to add this to our stdafx.h header file so that we can use it as well as any other class in our project (which we will need to for IInputObject). Open the stdafx.h header file and add the lines in bold to the file as indicated.
#include <shlobj.h>#include <comdef.h>
Our implementation of the toolbar window is complete. Now we can move on to the Reflection window which creates our toolbars and forwards command messages to it.
The Reflection Window Details
For the Reflection Window, it’s only purpose is to create the toolbar window (which it doesn’t need to really do, but by doing so eases message forwarding) and forward messages to it. The reflection window is not visible, it’s just a layer added so that message from the toolbar get to the toolbar. If we didn’t have this window present, toolbar messages would get sent to the parent window (which we do not control) and we would never get them. This is not good since we need to respond to WM_COMMAND messages from the toolbar. Thus the need for the reflection window. So let’s create the toolbar window and the message forwarding for it.
Open the ReflectionWnd.h header file. We will need to include the toolbar header file and add a private member variable to our reflection window to create it and to pass it to the message chain. First things first, add the include statement below so we can use the CMFToolbar class.
#include <commctrl.h>#include "MFToolbar.h"
Next add a private member variable to the end of the reflection window class for the toolbar as shown below.
CReflectionWnd(); virtual ~CReflectionWnd();private: CMFToolbar m_ToolbarWnd;
Next update the reflection window message map to forward messages to the toolbar window as shown below.
BEGIN_MSG_MAP(CReflectionWnd) CHAIN_MSG_MAP_MEMBER(m_ToolbarWnd) END_MSG_MAP()
We will also need a public function for our deskband class to get at our toolbar window. We will do the same as we did with the EditQuote window by providing a function to get at the member variable indirectly. Add the line of code in bold below to the header file as indicated.
CReflectionWnd(); virtual ~CReflectionWnd(); inline CMFToolbar& GetToolBar() { return m_ToolbarWnd;};
Lastly, we need to create the toolbar window and will do so in the WM_CREATE message handler for our reflection window. Add the code below in bold to the reflection window header file. Then we will implement the OnCreate method in the source file.
BEGIN_MSG_MAP(CReflectionWnd) MESSAGE_HANDLER(WM_CREATE, OnCreate) CHAIN_MSG_MAP_MEMBER(m_ToolbarWnd) END_MSG_MAP() LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
Now open the ReflectionWnd.cpp source file and add the implementation of OnCreate to its end.
const DWORD DEFAULT_TOOLBAR_STYLE = WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | WS_TABSTOP | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | TBSTYLE_LIST | TBSTYLE_CUSTOMERASE | TBSTYLE_WRAPABLE | CCS_TOP | CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE;LRESULT CReflectionWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){ RECT rect; GetClientRect(&rect); m_ToolbarWnd.Create(m_hWnd, rect, NULL, DEFAULT_TOOLBAR_STYLE); return 0;}
You will notice that we defined a constant for the toolbar style. This was done to make the code more readable.
The only thing left to do to the reflection window code is update the destructor to destory the window if it’s still present.
CReflectionWnd::~CReflectionWnd(){ if (IsWindow()) DestroyWindow();}
Finishing Off The Deskband Toolbar
All that’s left is for our deskband to create the toolbar window, have the host use the toolbar window, remove the unused IPersistStream implementation, implement IInputObject (for focus control) and do some code tweaking. So lets wrap this toolbar up. Open the StockBar.h header file. Remove the following lines of code that are in bold since we moved them to our stdafx.h for our other classes to also use.
#include "resource.h" // main symbols#include <shlguid.h>#include <shlobj.h>
Next remove the following line from the class delcaration.
public IPersistStream,
The top of the class declaration should now look like this,
class ATL_NO_VTABLE CStockBar : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CStockBar, &CLSID_StockBar>, public IDeskBand, public IObjectWithSite, public IDispatchImpl<IStockBar, &IID_IStockBar, &LIBID_MOTLEYFOOLLib>{
Next scroll down to the COM Map and remove the following two lines of code,
COM_INTERFACE_ENTRY(IPersist) COM_INTERFACE_ENTRY(IPersistStream)
Your COM Map should now look like this,
BEGIN_COM_MAP(CStockBar) COM_INTERFACE_ENTRY(IStockBar) COM_INTERFACE_ENTRY(IOleWindow) COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow) COM_INTERFACE_ENTRY(IObjectWithSite) COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand) COM_INTERFACE_ENTRY(IDispatch)END_COM_MAP()
Scroll down the header file further and remove the sections of function declarations for IPersist and IPersistStream, which includes the following lines.
public: STDMETHOD(GetClassID)(CLSID *pClassID);public: STDMETHOD(IsDirty)(void); STDMETHOD(Load)(IStream *pStm); STDMETHOD(Save)(IStream *pStm, BOOL fClearDirty); STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize);
There should now be nothing related to IPersist or IPersistStream between the IDockingWindow and IStockBar function declaration sections.
Now we need to remove the IPersist and IPersistStream function implementations from the StockBar.cpp source file. Find the GetClassID, IsDirty, Load, Save, and GetSizeMax function implementations and remove them from the file. They should be directly above the FocusChange Method we added earlier.
Now we can start adding code to our deskband. Open the stockbar.h header file and add the include for the reflection window class as shown below in bold.
#include "resource.h" // main symbols#include "ReflectionWnd.h"
Next, find the protected section at the end of the file and replace the line
HWND m_hWnd;
with
CReflectionWnd m_ReflectWnd;
If you try to compile, you will find that it will be unsuccessful since we have removed m_hWnd and have not removed all occurances of m_hWnd from the class source file. We will replace all occurances with more appropriate code for our Reflection window and toolbar window. Open the StockBar.cpp source file, Remove the following line from the class constructor
m_hWnd(NULL),
your class constructor should now look as follows with a SetBand call added,
CStockBar::CStockBar(): m_dwBandID(0), m_dwViewMode(0), m_bShow(FALSE), m_bEnterHelpMode(FALSE), m_hWndParent(NULL), m_pSite(NULL){ m_ReflectWnd.GetToolBar().GetEditBox().SetBand(this);}
Next, update the RegisterAndCreateWindow function by replacing the temporary m_hWnd construction with the Reflection Window construction. Your RegisterAndCreateWindow implementation should look as follows:
BOOL CStockBar::RegisterAndCreateWindow(){ RECT rect; ::GetClientRect(m_hWndParent, &rect); m_ReflectWnd.Create(m_hWndParent, rect, NULL, WS_CHILD); return m_ReflectWnd.GetToolBar().IsWindow();}
As we scroll through the code, we should fix some other things. A toolbar has a default height of 22 so we need to update our GetBandInfo method so that all the y measurements are 22 not 20. We also want our Integral sizing to be non sizeable so we need to set the DBIM_INTEGRAL x and y values to 0. While we are at it, we should fix the title of our toolbar so it says "The Motley Fool". You’ll find this constant defined near the top of the source file, update it now.
We will now update the GetWindow call so that the toolbar window handle is returned and not the invalid m_hWnd variable. Update your GetWindow method so it looks as follows,
STDMETHODIMP CStockBar::GetWindow(HWND* phwnd){ HRESULT hr = S_OK; if (NULL == phwnd) { hr = E_INVALIDARG; } else { *phwnd = m_ReflectWnd.GetToolBar().m_hWnd; } return hr;}
Now we need to update teh CloseDW method so that it does not destory our window buy hide it. We do this much like the MFC CToolbar class does, the class destructor will destroy the window. Your CloseDW method should look as follows,
STDMETHODIMP CStockBar::CloseDW(unsigned long dwReserved){ ShowDW(FALSE); return S_OK;}
Working our way through our class implementation, we will update the next method that needs updating. Update the ShowDW class to show or hide the toolbar window which the host is using. Your ShowDW class should look as follows,
STDMETHODIMP CStockBar::ShowDW(BOOL fShow){ m_bShow = fShow; m_ReflectWnd.GetToolBar().ShowWindow(m_bShow ? SW_SHOW : SW_HIDE); return S_OK;}
We are almost done modifying the CStockBar class we need to do one last bit of updating. We need to modify the SetSite implementation to release the browser window that the toolbar may be using if we have a IInputObjectSite object and we need to query the OleCommandTarget’s ServiceProvider for the IWebBrowser2 interface which we will then set to our toolbar. The modified parts of the SetSite method implementation are below in bold.
Collapse
STDMETHODIMP CStockBar::SetSite(IUnknown* pUnkSite){ if(m_pSite) { m_ReflectWnd.GetToolBar().SetBrowser(NULL); m_pSite->Release(); m_pSite = NULL; } if(pUnkSite) { IOleWindow *pOleWindow = NULL; m_hWndParent = NULL; if(SUCCEEDED(pUnkSite->QueryInterface(IID_IOleWindow, (LPVOID*)&pOleWindow))) { pOleWindow->GetWindow(&m_hWndParent); pOleWindow->Release(); } if(!::IsWindow(m_hWndParent)) return E_FAIL; if(!RegisterAndCreateWindow()) return E_FAIL; if(FAILED(pUnkSite->QueryInterface(IID_IInputObjectSite, (LPVOID*)&m_pSite))) { return E_FAIL; } IWebBrowser2* s_pFrameWB = NULL; IOleCommandTarget* pCmdTarget = NULL; HRESULT hr = pUnkSite->QueryInterface(IID_IOleCommandTarget, (LPVOID*)&pCmdTarget); if (SUCCEEDED(hr)) { IServiceProvider* pSP; hr = pCmdTarget->QueryInterface(IID_IServiceProvider, (LPVOID*)&pSP); pCmdTarget->Release(); if (SUCCEEDED(hr)) { hr = pSP->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID*)&s_pFrameWB); pSP->Release(); _ASSERT(s_pFrameWB); m_ReflectWnd.GetToolBar().SetBrowser(s_pFrameWB); s_pFrameWB->Release(); } } } return S_OK;}
If you try to compile and use the toolbar right now, it will function partially. Tabbing and input control will not work correctly since we have yet to implement IInputObject for our deskband. Let’s do that now since it’s the last bit of code we will write for our simple deskband. You may also notice that the Toolbars context menu and View|Toolbars menus still say CStockBar Class. we will fix this problem in the Finishing Touches section below.
IInputObject Implementation
To get tabbing and input control to work correctly for any deskband is quite simple. You need but to implement IInputObject. The host will query our deskband to see if this interface is implemented and if it is will call the methods to see if we require input focus and let us also process messages from the user through the host. To do this, open the stockbar.h header file. To the stockbar class declaration add the line below in bold,
class ATL_NO_VTABLE CStockBar : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CStockBar, &CLSID_StockBar>, public IDeskBand, public IObjectWithSite, public IInputObject, public IDispatchImpl<IStockBar, &IID_IStockBar, &LIBID_MOTLEYFOOLLib>{
Next scroll down to the COM Map and add an entry for IInputObject as shown below in bold,
BEGIN_COM_MAP(CStockBar) COM_INTERFACE_ENTRY(IStockBar) COM_INTERFACE_ENTRY(IInputObject) COM_INTERFACE_ENTRY(IOleWindow) COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow) COM_INTERFACE_ENTRY(IObjectWithSite) COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand) COM_INTERFACE_ENTRY(IDispatch)END_COM_MAP()
Next add the following section of function declarations to your header file, I placed mine before the IStockBar section.
public: STDMETHOD(HasFocusIO)(void); STDMETHOD(TranslateAcceleratorIO)(LPMSG lpMsg); STDMETHOD(UIActivateIO)(BOOL fActivate, LPMSG lpMsg);
All that remains is to implement these three functions. Add the function implementations below to the end of the stockbar.cpp source file.
Collapse
STDMETHODIMP CStockBar::HasFocusIO(void){ if (m_ReflectWnd.GetToolBar().m_hWnd == ::GetFocus()) return S_OK; if (m_ReflectWnd.GetToolBar().GetEditBox().m_hWnd == ::GetFocus()) return S_OK; return S_FALSE;}STDMETHODIMP CStockBar::TranslateAcceleratorIO(LPMSG lpMsg){ return m_ReflectWnd.GetToolBar().GetEditBox().TranslateAcceleratorIO(lpMsg);}STDMETHODIMP CStockBar::UIActivateIO(BOOL fActivate, LPMSG lpMsg){ if(fActivate) { m_ReflectWnd.GetToolBar().GetEditBox().SetFocus(); } return S_OK;}
Our toolbar is functionaly done, compile, run it and see. It works as described and is fairly simple. Let’s put some finishing UI touches on it for IE and our users to use.
Finishing Touches
The are only 2 finishing touches to make, One is to fix the context menu text. The other is to add button support to the main IE toolbar. Let’s do them in order.
To fix the context menu text problem, open the StockBar.rgs project file and change all occurances of "StockBar Class" to "The Motley Fool Quotes". Compile it, run it, and see. While you only need to change one of them, it’s nicer if they all match.
Now let’s add button support for our toolbar. Update the stockbar.rgs file contents by appending the text below to it’s contents.
Collapse
HKLM{ Software { Microsoft { 'Internet Explorer' { Extensions { ForceRemove {A26ABCF0-1C8F-46e7-A67C-0489DC21B9CC} = s 'The Motley Fool Quotes' { val BandClsid = s '{A6790AA5-C6C7-4BCF-A46D-0FDAC4EA90EB}' val ButtonText = s 'The Motley Fool' val Clsid = s '{E0DD6CAB-2D10-11D2-8F1A-0000F87ABD16}' val 'Default Visible' = s 'Yes' val 'Hot Icon' = s '%MODULE%,425' val Icon = s '%MODULE%,425' val MenuStatusBar = s 'The Motley Fool Stock Quote Toolbar' val MenuText = s 'The Motley Fool' } } } } }}
The replace the 425 with the id from resource.h of IDI_MOTLEY. Also replace the BandClsid value with the GUID of our toolbar, the above values represent the source code for the article. Now compile the toolbar again. Then start IE and right click on the Standard Buttons toolbar, Select "Customize" from the context menu. Scroll down the Available toolbar buttons listbox and find "The Motley Fool" item, See Figure 12. Select it and click the Add button in the middle of the dialog. The item will move to the right as in Figure 13. Click the Close Button. You’ll see the button added as shown in the before figure 14 and after figure 15.
Figure 12. CustomizeToolbar – Available Toolbar Buttons.
Figure 13. Customize Toolbar – Current Toolbar Buttons.
Figure 14. IE Standard Buttons – Before.
Figure 15. IE Standard Buttons – After.
Conclusion
While this tutorial is long hopefully the explaination was clear. From writing this tutorial it is easy to see that the RBDeskband ATL Object Wizard has some room for improvement but provided enough of a base for us to develop our simple example. In the end you can see that the toolbar we created is much like the Address bar. The differences lie in how MS implemented theirs versus how I implemented mine. As always feedback is welcome. Enjoy.
About Erik Thompson
Site Builder |
Erik lives in Redmond, Washington. He works as a Senior Software Engineer specializing in C++, COM, ATL and the middle-tier and now .NET. When he isn’t coding for work, he can be found trying to extend Internet Explorer with yet another Desk band or simplifying his development process with ATL Object Wizards.
He spends his free time snowboarding, mountain biking, reading, dining out at those hidden finds.
Click here to view Erik Thompson’s online profile.
|
Other popular ATL articles:
|