<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Architecting Life &#187; REALbasic</title>
	<atom:link href="http://xujiwei.com/blog/tags/realbasic/feed/" rel="self" type="application/rss+xml" />
	<link>http://xujiwei.com/blog</link>
	<description>Just do it</description>
	<lastBuildDate>Thu, 05 Apr 2012 17:19:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>iWangWang v0.3.0 Released</title>
		<link>http://xujiwei.com/blog/iwangwang-v0-3-0-released/</link>
		<comments>http://xujiwei.com/blog/iwangwang-v0-3-0-released/#comments</comments>
		<pubDate>Sat, 22 May 2010 17:38:27 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iWangWang]]></category>
		<category><![CDATA[REALbasic]]></category>
		<category><![CDATA[淘宝]]></category>
		<category><![CDATA[阿里旺旺]]></category>

		<guid isPermaLink="false">http://www.xujiwei.com/blog/?p=297</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/iwangwang-v0-3-0-released/" title="iWangWang v0.3.0 Released"></a>在上一个版本发布之后，过了两个月的时间，终于又有一个可以发布的版本了。 使用菜单中的“检查更新”既可完成更新，或者前往 Google Code 下载。 这次的更新如下： [FIX] 修正在 Snow Leopard 中无法在 Dock 上显示未读消息数目的bug [FIX] 修正在网页中点击旺旺图标无法打开聊天窗口的bug [FIX] 修正无法使用“指定发送”和“添加好友”功能的bug [FIX] 修正表情选择框在一些情况下引起程序崩溃的bug [FIX] 修正在聊天窗口中无法指定发送功能无法使用的bug [FIX] 修正一些菜单无法在所有窗口中使用的bug [NEW] 现在可以使用 Ctrl+N 同时运行多个 iWangWang 来实现同时登录多个淘宝帐号 [NEW] 现在如果聊天时消息发送失败，可以重新输入验证码 [NEW] 登录后会恢复上一次退出时主窗口的位置 [NEW] 表情选择框现在可以用 ESC 键关闭 [NOTE] 有bug可以在 http://code.google.com/p/iwangwang/issues/entry &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/iwangwang-v0-3-0-released/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/iwangwang-v0-3-0-released/" title="iWangWang v0.3.0 Released"></a><p>在上一个版本发布之后，过了两个月的时间，终于又有一个可以发布的版本了。</p>

<p>使用菜单中的“<strong>检查更新</strong>”既可完成更新，或者前往 <a href="http://code.google.com/p/iwangwang/downloads/detail?name=iWangWang-0.3.0.zip&amp;can=2&amp;q=#makechanges">Google Code</a> 下载。</p>

<p>这次的更新如下：</p>

<ul>
    <li>[FIX] 修正在 Snow Leopard 中无法在 Dock 上显示未读消息数目的bug</li>
    <li>[FIX] 修正在网页中点击旺旺图标无法打开聊天窗口的bug</li>
    <li>[FIX] 修正无法使用“指定发送”和“添加好友”功能的bug</li>
    <li>[FIX] 修正表情选择框在一些情况下引起程序崩溃的bug</li>
    <li>[FIX] 修正在聊天窗口中无法指定发送功能无法使用的bug</li>
    <li>[FIX] 修正一些菜单无法在所有窗口中使用的bug</li>
    <li>[NEW] 现在可以使用 Ctrl+N 同时运行多个 iWangWang 来实现同时登录多个淘宝帐号</li>
    <li>[NEW] 现在如果聊天时消息发送失败，可以重新输入验证码</li>
    <li>[NEW] 登录后会恢复上一次退出时主窗口的位置</li>
    <li>[NEW] 表情选择框现在可以用 ESC 键关闭</li>
    <li>[NOTE] 有bug可以在 http://code.google.com/p/iwangwang/issues/entry 提出</li>
</ul>

<p>这次主要修正了 iWangWang 在 Snow Leopard 下的兼容性，之前的开发环境是 Leopard，没能在 Snow Leopard 中好好测试。</p>

<p>如果有问题请邮件至 ohdarling88 at gmail dot com，或在 <a href="http://code.google.com/p/iwangwang/issues/entry">http://code.google.com/p/iwangwang/issues/entry</a> 填写问题表单。</p>

<p>Enjoy it.</p>

<p>Xu Jiwei</p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/iwangwang-v0-3-0-released/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>iWangWang v0.1.2 for Mac 发布</title>
		<link>http://xujiwei.com/blog/iwangwang-v0-1-released/</link>
		<comments>http://xujiwei.com/blog/iwangwang-v0-1-released/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 06:07:57 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iWangWang]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[REALbasic]]></category>
		<category><![CDATA[旺旺]]></category>

		<guid isPermaLink="false">http://www.xujiwei.com/blog/?p=259</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/iwangwang-v0-1-released/" title="iWangWang v0.1.2 for Mac 发布"></a>iWangWang 是一个在 Mac OS X 下使用的 Web 旺旺的客户端，使用 Mac 用户在淘宝购物时也可以方便地与卖家沟通，不需要每次打开 Web 旺旺，以及弥补 Web 旺旺不支持联系人列表及聊天记录的缺陷，并且目前只支持淘宝网帐号登录。 iWangWang 使用 REALbasic 编写，因为是基于 Web 旺旺的，所以 Web 旺旺不支持的功能 iWangWang 也没有实现，包括但不限于旺旺群、自定义表情、文件传送，并且和某一个联系人第一次聊天时，需要输入验证码。 注意： 这纯粹是我个人出于兴趣而制作的一个程序，与公司无关。 因为使用非公开接口，协议通过黑盒分析所得，因此有可能会由于 Web 旺旺修改通信协议而导致程序无法使用。 由于缺乏艺术细胞，因此程序界面仿照 MacQQ，如果觉得有内容侵犯了您的权利，请及时联系我修改，谢谢 :) 因为淘宝帐号涉及到金钱，所以如果不放心请不要使用这个程序 :) 如果有任何问题或建议，请联系 ohdarling88 # gmail dot com，或者点击菜单“文件》填写使用反馈”来填写反馈内容。 &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/iwangwang-v0-1-released/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/iwangwang-v0-1-released/" title="iWangWang v0.1.2 for Mac 发布"></a><p>iWangWang 是一个<strong>在 Mac OS X 下使用的 Web 旺旺的客户端</strong>，使用 Mac 用户在<a href="http://www.taobao.com" target="_blank">淘宝</a>购物时也可以方便地与卖家沟通，不需要每次打开 Web 旺旺，以及弥补 Web 旺旺不支持联系人列表及聊天记录的缺陷，并且目前<strong>只支持淘宝网帐号登录。</strong></p>

<p>iWangWang 使用 REALbasic 编写，因为是基于 Web 旺旺的，所以 Web 旺旺不支持的功能 iWangWang 也没有实现，包括但不限于旺旺群、自定义表情、文件传送，并且和某一个联系人第一次聊天时，需要输入验证码。</p>

<p>注意：</p>

<ol>
    <li>这纯粹是我个人出于兴趣而制作的一个程序，与公司无关。</li>
    <li>因为使用非公开接口，协议通过黑盒分析所得，因此有可能会由于 Web 旺旺修改通信协议而导致程序无法使用。</li>
    <li>由于缺乏艺术细胞，因此程序界面仿照 MacQQ，如果觉得有内容侵犯了您的权利，请及时联系我修改，谢谢 :)</li>
    <li>因为淘宝帐号涉及到金钱，所以如果不放心请不要使用这个程序 :)</li>
</ol>

<p>如果有任何问题或建议，请联系 ohdarling88 # gmail dot com，或者点击菜单“文件》填写使用反馈”来填写反馈内容。</p>

<p><span style="color: red;"><strong>14:40 Update 更新下载链接，修正了接收带有自定义表情的消息时会导致程序崩溃的bug，如果已经下载，可以直接点击菜单“iWangWang &gt; 检查更新”</strong></span></p>

<p>下载地址：<a href="http://iwangwang.googlecode.com/files/iWangWang-v0.1.2.zip">http://iwangwang.googlecode.com/files/iWangWang-v0.1.2.zip</a></p>

<p><a href="http://www.xujiwei.com/blog/wp-content/uploads/2010/03/iwangwang.png"><img class="alignnone size-full wp-image-260" title="iwangwang" src="http://xujiwei.com/blog/wp-content/uploads/2010/03/iwangwang.png" alt="" width="330" height="552" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/iwangwang-v0-1-released/feed/</wfw:commentRss>
		<slash:comments>70</slash:comments>
		</item>
		<item>
		<title>在 REALbasic 中注册 AppleEvent</title>
		<link>http://xujiwei.com/blog/register-appleevent-in-realbasic/</link>
		<comments>http://xujiwei.com/blog/register-appleevent-in-realbasic/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 06:37:19 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Mac OS & iOS]]></category>
		<category><![CDATA[AppleEvent]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[REALbasic]]></category>

		<guid isPermaLink="false">http://www.xujiwei.com/blog/?p=228</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/" title="在 REALbasic 中注册 AppleEvent"></a>之前为了注册一个自定义协议，需要通过注册 AppleEvent 来实现，在 Objective-C 中，可以很方便的使用 NSAppleEventManager 来注册 AppleEvent 句柄，但是在 REALbaisc 中，是没有办法直接去调用 NSAppleEventManager 的，所以需要通过声明然后调用 C API 来实现相应的功能。 与 NSAppleEventManager 中功能相对应的 C API 有 AEInstallEventHandler, NewAEEventHandlerUPP 等，通过这些 API 我们也可以在 REALbasic 中来注册 AppleEvent 了，再配合 Info.plist 中的 URLScheme 声明，即可实现 URL 自定义协议处理句柄。 #if TargetCarbon soft &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/register-appleevent-in-realbasic/" title="在 REALbasic 中注册 AppleEvent"></a><p>之前为了注册一个自定义协议，需要通过注册 AppleEvent 来实现，在 Objective-C 中，可以很方便的使用 NSAppleEventManager 来注册 AppleEvent 句柄，但是在 REALbaisc 中，是没有办法直接去调用 NSAppleEventManager 的，所以需要通过声明然后调用 C API 来实现相应的功能。</p>

<p>与 NSAppleEventManager 中功能相对应的 C API 有 AEInstallEventHandler, NewAEEventHandlerUPP 等，通过这些 API 我们也可以在 REALbasic 中来注册 AppleEvent 了，再配合 Info.plist 中的 URLScheme 声明，即可实现 URL 自定义协议处理句柄。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">#if TargetCarbon
    soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> AEInstallEventHandler Lib CarbonLib ( _
    theAEEventClass <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    theAEEventID <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    handler <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    handlerRefcon <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    isSysHandler <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    Soft <span style="color: #151B8D; font-weight: bold;">Declare</span> <span style="color: #E56717; font-weight: bold;">Function</span> NewAEEventHandlerUPP Lib CarbonLib (userRoutine <span style="color: #151B8D; font-weight: bold;">as</span> Ptr) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">Static</span> CallbackUPP <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 0
    <span style="color: #8D38C9; font-weight: bold;">If</span> CallbackUPP = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
      <span style="color: #151B8D; font-weight: bold;">dim</span> m <span style="color: #151B8D; font-weight: bold;">as</span> MemoryBlock =  AddressOf ForwardCarbonAEEventToObject
      <span style="color: #8D38C9; font-weight: bold;">If</span> m <span style="color: #8D38C9; font-weight: bold;">is</span> nil <span style="color: #8D38C9; font-weight: bold;">then</span>
        Return
      <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
      CallbackUPP = NewAEEventHandlerUPP(m)
    <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">dim</span> v <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Variant</span> = me
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">dim</span> OSError <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = AEInstallEventHandler( _
    OSTypeToUInt(kInternetEventClass), _
    OSTypeToUInt(kAEGetURL), _
    CallbackUPP, _
    v.Hash, <span style="color: #00C2FF; font-weight: bold;">false</span>)
&nbsp;
    msgbox str(OSError)
#endif</pre></div></div>


<p>先使用 NewAEEventHandlerUPP 来生成一个 AppleEvent 回调函数的句柄，然后调用 AEInstallEventHandler 来注册一个共享函数 ForwardCarbonAEEventToObject 为 AppleEvent 事件处理句柄。</p>

<p>AEInstallEventHandler 所需的 AEEventClass 和 AEEventID 都是一个 4 字节的整型，但是通常我们在调用的时候，是用的一个 4 字符的字符串，因此需要一个函数来将 4 字符转换为 4 字节的整形。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">// code from ToolbarSearchField by The ZAZ Studios
// http://www.thezaz.com/opensource/realbasic/macosx/searchfield/
<span style="color: #151B8D; font-weight: bold;">static</span> m <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #E56717; font-weight: bold;">new</span> MemoryBlock(4)
m.LittleEndian = <span style="color: #00C2FF; font-weight: bold;">false</span>
m.StringValue(0, 4) = s
return m.UInt32Value(0)</pre></div></div>


<p>在 ForwardCartonAEEventToObject 里，参数 theEvent 和 replyEvent 都量个整形，为了从这两个参数里拿到数据，还需要使用 AEGetParamPtr 来从 AppleEvent 中拿到数据。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> AEGetParamPtr lib CarbonLib ( _
    theAppleEvent <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, theAEKeyword <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    desiredType <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, <span style="color: #151B8D; font-weight: bold;">byref</span> actualType <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    dataPtr <span style="color: #151B8D; font-weight: bold;">as</span> Ptr, maximumSize <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, _
    <span style="color: #151B8D; font-weight: bold;">byref</span> actualSize <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span></pre></div></div>


<p>当然还有一系列的 AEGetDataDesc、AEGetDescSize 等函数可以，具体可以查 Xocde 随带的库文档。</p>

<p>关于注册自定义协议，可以参考<a href="http://stackoverflow.com/questions/49510/how-do-you-set-your-cocoa-application-as-the-default-web-browser">这篇文章</a>。</p>

<p>通过 Core Foundation 中的一些 C API，在 REALbasic 也可以完成一些平台相关的工作，虽然麻烦了些：）</p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/register-appleevent-in-realbasic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>REALbasic 中使用结构体作为 Win32 API 的参数及使用 Win32 API 停止服务</title>
		<link>http://xujiwei.com/blog/using-win32-api-in-realbasic/</link>
		<comments>http://xujiwei.com/blog/using-win32-api-in-realbasic/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 15:47:42 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[REALbasic]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=65</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/using-win32-api-in-realbasic/" title="REALbasic 中使用结构体作为 Win32 API 的参数及使用 Win32 API 停止服务"></a>在之前，我使用 ShellExecute 这个 API 来执行命令（《REALbasic 中使用 ShellExecute 执行命令》），然后通过这个方法来停止某个服务，但是今天想加服务运行状态检测，这样就可以在服务没有运行的情况下不再询问用户是否需要停止某个服务。 为了省事，我一开始决定同样使用 cmd 去执行一个命令，将服务状态输出到一个临时文件中，再通过读取这个临时文件，查找特征字符串来判断服务是否运行： // 伪代码 ShellExecute(&#34;cmd.exe /c sc query service_name &#38;gt; tmpfile&#34;) dim serviceStatus as string serviceStatus = TextInputStream.Open(tmpfile).ReadAll() if instr(serviceStatus, &#34;STOPPED&#34;) &#38;lt; 1 then // 提示用户是否停止服务 end if 但是这样做不太靠谱，因为 sc 这个命令执行是要时间的，而 &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/using-win32-api-in-realbasic/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/using-win32-api-in-realbasic/" title="REALbasic 中使用结构体作为 Win32 API 的参数及使用 Win32 API 停止服务"></a><p>在之前，我使用 ShellExecute 这个 API 来执行命令（《REALbasic 中使用 ShellExecute 执行命令》），然后通过这个方法来停止某个服务，但是今天想加服务运行状态检测，这样就可以在服务没有运行的情况下不再询问用户是否需要停止某个服务。</p>

<p>为了省事，我一开始决定同样使用 cmd 去执行一个命令，将服务状态输出到一个临时文件中，再通过读取这个临时文件，查找特征字符串来判断服务是否运行：</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">// 伪代码
ShellExecute(<span style="color: #800000;">&quot;cmd.exe /c sc query service_name &amp;gt; tmpfile&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">dim</span> serviceStatus <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">string</span>
serviceStatus = TextInputStream.<span style="color: #151B8D; font-weight: bold;">Open</span>(tmpfile).ReadAll()
<span style="color: #8D38C9; font-weight: bold;">if</span> instr(serviceStatus, <span style="color: #800000;">&quot;STOPPED&quot;</span>) &amp;lt; 1 <span style="color: #8D38C9; font-weight: bold;">then</span>
  // 提示用户是否停止服务
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>


<p>但是这样做不太靠谱，因为 sc 这个命令执行是要时间的，而 ShellExecute 是异步的，这就导致了在调用 ShellExecute 执行完 sc query 之后，临时文件 tmpfile 里并不是马上就有服务状态的内容了。</p>

<p>为了防止这个情况，我再加了一个临时文件内容的检测，如果为空的话，sleep 100ms，再继续读取，如果超过 10 次仍没有内容，直接当作服务正在运行来对待。</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">// 伪代码
ShellExecute(<span style="color: #800000;">&quot;cmd.exe /c sc query service_name &amp;gt; tmpfile&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">dim</span> serviceStatus <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">string</span>
<span style="color: #151B8D; font-weight: bold;">dim</span> tryCount <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">integer</span>
<span style="color: #8D38C9; font-weight: bold;">while</span> tryCount &amp;lt; 10 <span style="color: #8D38C9; font-weight: bold;">and</span> serviceStatus = <span style="color: #800000;">&quot;&quot;</span>
  tryCount = tryCount + 1
  App.SleepCurrentThread(100)
  serviceStatus = TextInputStream.<span style="color: #151B8D; font-weight: bold;">Open</span>(tmpfile).ReadAll()
&nbsp;
wend
<span style="color: #8D38C9; font-weight: bold;">if</span> instr(serviceStatus, <span style="color: #800000;">&quot;STOPPED&quot;</span>) &amp;lt; 1 <span style="color: #8D38C9; font-weight: bold;">then</span>
  // 提示用户是否停止服务
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>


<p>但是这样仍然不能百分百保证正确，于是想到可不可以继续用 win32 api 来做这些，Google 了一下，找到一篇博客[1]，看了之后发现如果只需要停止服务的话，还是蛮简单的，决定使用 win32 api 来实现我想要的功能了。</p>

<p>在停止服务这个过程中需要用到的 win32 api 有：OpenSCManagerW、OpenServiceW、QueryServiceStatus、ControlService，还有一个结构体：SERVICE_STATUS。</p>

<p>win32 api 的参数都很好对应，数值、句柄类型的参数直接使用 Integer 即可，字符串类型使用 CString，而 SERVICE_STATUS 这个结构体也可以直接通过工程菜单添加。</p>

<p>在传参的时候需要注意两点：</p>

<ol>
    <li><strong>如果是字符串类型的参数并且调用的接口是 Unicode 类型的，那么需要在传入参数之前，将参数值转换为 Unicode 编码。可以通过 ConvertEncoding(text, Encodings.UTF16) 来转换。</strong></li>
    <li><strong>如果参数是结构体，那么参数前的传参方式就要写 ByRef，也就是按引用传值，字符串和数值类型的参数可以不写或者写 ByVal。</strong></li>
</ol>

<p>以下是 4 个 win32 api 的 REALbasic 定义：</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> OpenSCManagerW Lib <span style="color: #800000;">&quot;advapi32.dll&quot;</span> _
(<span style="color: #151B8D; font-weight: bold;">ByVal</span> lpMachineName <span style="color: #151B8D; font-weight: bold;">As</span> CString, <span style="color: #151B8D; font-weight: bold;">ByVal</span> lpDatabaseName <span style="color: #151B8D; font-weight: bold;">As</span> CString, _
<span style="color: #151B8D; font-weight: bold;">ByVal</span> dwDesiredAccess <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> OpenServiceW lib <span style="color: #800000;">&quot;advapi32.dll&quot;</span> _
(<span style="color: #151B8D; font-weight: bold;">ByVal</span> hSCManager <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> lpServiceName <span style="color: #151B8D; font-weight: bold;">As</span> CString, _
<span style="color: #151B8D; font-weight: bold;">ByVal</span> dwDesiredAccess <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> QueryServiceStatus lib <span style="color: #800000;">&quot;advapi32.dll&quot;</span> _
(<span style="color: #151B8D; font-weight: bold;">byval</span> hService <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">integer</span>, <span style="color: #151B8D; font-weight: bold;">byref</span> lpServiceStatus <span style="color: #151B8D; font-weight: bold;">as</span> SERVICE_STATUS) _
<span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">boolean</span>
&nbsp;
soft <span style="color: #151B8D; font-weight: bold;">declare</span> <span style="color: #E56717; font-weight: bold;">function</span> ControlService lib <span style="color: #800000;">&quot;advapi32.dll&quot;</span> _
(<span style="color: #151B8D; font-weight: bold;">byval</span> hService <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">integer</span>, <span style="color: #151B8D; font-weight: bold;">byval</span> dwControl <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">integer</span>, _
<span style="color: #151B8D; font-weight: bold;">byref</span> lpServiceStatus <span style="color: #151B8D; font-weight: bold;">as</span> SERVICE_STATUS) <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">boolean</span></pre></div></div>


<p>有了 api 定义之后，就可以直接根据上面提到的 vc++ 流程来停止某个服务了。</p>

<p>完整的服务操作 api 可以参阅微软的 MSDN。</p>

<p>这样看来，REALbasic 要写出支持 Windows 7 特性的应用程序也不是难事：）</p>

<p><strong>参数资料</strong></p>

<ol>
    <li><a rel="external" href="http://blog.csdn.net/huangchonghai/archive/2007/04/25/1583602.aspx">VC++启动和停止服务</a>, huangchonghai</li>
    <li><a rel="external" href="http://msdn.microsoft.com/en-us/library/ms684939(VS.85).aspx">QueryServiceStatus</a>, MSDN</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/using-win32-api-in-realbasic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chromium Updater for Mac</title>
		<link>http://xujiwei.com/blog/chromium-updater-for-mac/</link>
		<comments>http://xujiwei.com/blog/chromium-updater-for-mac/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 03:12:08 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Chromium]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[REALbasic]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=68</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/chromium-updater-for-mac/" title="Chromium Updater for Mac"></a>为了随时更新到 Chromium 的最新 nightly build，又不想每次去 Chromium 的网站下，还有解压，移动，麻烦……为了省事，直接用 REALbasic 写了一个 Chromium Updater，专门用来更新 Chromium。目前只支持 Mac :) 详细说明 下载（使用右键下载）：ChromiumUpdater (3.5M)]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/chromium-updater-for-mac/" title="Chromium Updater for Mac"></a><p><img class="alignright" src="http://apps.xujiwei.com/wp-content/uploads/2009/11/ChromiumUpdater_128x128.png" alt="图片附件" width="128" height="128" />为了随时更新到 Chromium 的最新 nightly build，又不想每次去 Chromium 的网站下，还有解压，移动，麻烦……为了省事，直接用 REALbasic 写了一个 Chromium Updater，专门用来更新 Chromium。目前只支持 Mac :)</p>

<p><a rel="external" href="http://apps.xujiwei.com/utilites/chromiumupdater">详细说明</a></p>

<p>下载（使用右键下载）：<a rel="external" href="http://apps.ohdarling.info/ChromiumUpdater/ChromiumUpdater.zip">ChromiumUpdater</a> (3.5M)</p>

<p><img src="http://apps.xujiwei.com/wp-content/uploads/2009/11/ChromiumUpdater-screentshot.png" alt="图片附件" /></p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/chromium-updater-for-mac/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>关于 REALbasic 中使用 AutoDiscovery 时发生错误 40 的问题</title>
		<link>http://xujiwei.com/blog/%e5%85%b3%e4%ba%8e-realbasic-%e4%b8%ad%e4%bd%bf%e7%94%a8-autodiscovery-%e6%97%b6%e5%8f%91%e7%94%9f%e9%94%99%e8%af%af-40-%e7%9a%84%e9%97%ae%e9%a2%98/</link>
		<comments>http://xujiwei.com/blog/%e5%85%b3%e4%ba%8e-realbasic-%e4%b8%ad%e4%bd%bf%e7%94%a8-autodiscovery-%e6%97%b6%e5%8f%91%e7%94%9f%e9%94%99%e8%af%af-40-%e7%9a%84%e9%97%ae%e9%a2%98/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 16:12:14 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[AutoDiscovery]]></category>
		<category><![CDATA[REALbasic]]></category>
		<category><![CDATA[UDP]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=70</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/%e5%85%b3%e4%ba%8e-realbasic-%e4%b8%ad%e4%bd%bf%e7%94%a8-autodiscovery-%e6%97%b6%e5%8f%91%e7%94%9f%e9%94%99%e8%af%af-40-%e7%9a%84%e9%97%ae%e9%a2%98/" title="关于 REALbasic 中使用 AutoDiscovery 时发生错误 40 的问题"></a>今天在使用 AutoDiscovery 发送数据时，发生了错误码为 40 的错误，直接在 UDPSocket 的属性列表里找了一下，没有找到对应的错误码。 在网上搜了一下，找到这篇帖子，里面提到在发送大量数据时，UDPSocket 就会报错误，并且这个错误没有在文档中提到，因此这应该是一个系统级别的错误。 MonkeybreadSoftware 提到了 unix 的错误码定义： #define    EMSGSIZE    40     /* Message too long */ 联想到在程序中是在今天开始有问题的，而且今天添加了一大堆数据，看来的确是同于 UDP 消息过大造成的错误 40。 在网上找了找，使用 UDP 发送消息时，报文的大小最好不要超过 MTU，否则会就容易丢包。我在测试时是使用的 127.0.0.1，包大小为 12.5K，照理说应该是直接走 loopback 而不需要走路由的，就算超过 MTU 也应该能发送，不清楚是不是因为 OS 内部实现机制的问题。 为了彻底解决这个问题，最后还是采用了 TCP 来传递大量数据，只使用 UDP 来传递一些控制信息。]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/%e5%85%b3%e4%ba%8e-realbasic-%e4%b8%ad%e4%bd%bf%e7%94%a8-autodiscovery-%e6%97%b6%e5%8f%91%e7%94%9f%e9%94%99%e8%af%af-40-%e7%9a%84%e9%97%ae%e9%a2%98/" title="关于 REALbasic 中使用 AutoDiscovery 时发生错误 40 的问题"></a><p>今天在使用 AutoDiscovery 发送数据时，发生了错误码为 40 的错误，直接在 UDPSocket 的属性列表里找了一下，没有找到对应的错误码。</p>

<p>在网上搜了一下，找到<a rel="external" href="http://forums.realsoftware.com/viewtopic.php?f=2&amp;t=25418">这篇帖子</a>，里面提到在发送大量数据时，UDPSocket 就会报错误，并且这个错误没有在文档中提到，因此这应该是一个系统级别的错误。</p>

<p>MonkeybreadSoftware 提到了 unix 的错误码定义：</p>

<blockquote>#define    EMSGSIZE    40     /* Message too long */</blockquote>

<p>联想到在程序中是在今天开始有问题的，而且今天添加了一大堆数据，看来的确是同于 UDP 消息过大造成的错误 40。</p>

<p>在网上找了找，使用 UDP 发送消息时，报文的大小最好不要超过 <a rel="external" href="http://en.wikipedia.org/wiki/Maximum_transmission_unit">MTU</a>，否则会就容易丢包。我在测试时是使用的 127.0.0.1，包大小为 12.5K，照理说应该是直接走 loopback 而不需要走路由的，就算超过 MTU 也应该能发送，不清楚是不是因为 OS 内部实现机制的问题。</p>

<p>为了彻底解决这个问题，最后还是采用了 TCP 来传递大量数据，只使用 UDP 来传递一些控制信息。</p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/%e5%85%b3%e4%ba%8e-realbasic-%e4%b8%ad%e4%bd%bf%e7%94%a8-autodiscovery-%e6%97%b6%e5%8f%91%e7%94%9f%e9%94%99%e8%af%af-40-%e7%9a%84%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>REALbasic 中使用 ShellExecute 执行命令</title>
		<link>http://xujiwei.com/blog/using-shellexecute-in-realbasic/</link>
		<comments>http://xujiwei.com/blog/using-shellexecute-in-realbasic/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 13:50:40 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[REALbasic]]></category>
		<category><![CDATA[ShellExecute]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=72</guid>
		<description><![CDATA[<a href="http://xujiwei.com/blog/using-shellexecute-in-realbasic/" title="REALbasic 中使用 ShellExecute 执行命令"></a>在 REALbasic 中，如果需要执行 cmd 命令，可以直接使用 Shell 类，但是这样的话，编译成 Windows 程序时会额外需要一个 Shell.dll 的动态链接库，这对于我这样的 1exe 爱好者是不能忍受的。但是对于 Mac OS X 和 Linux 的生成目标来说，是不存在这个问题的，因为 Mac OS X 的应用程序本身就是一个文件夹，而 Linux 的目标不会生成额外的链接库。因此，需要针对 Windows 进行特殊处理。于是在网上搜索解决方案，找到了 VB 中执行程序的几种方法： 1. 使用 CreateProcess 通过 CreateProcess 以及使用管道，可以执行外部程序并获取输出，但是这个方法过于烦琐，并且我也不需要外部进程执行完毕后的输出结果，因此不采用。 2. 使用 Shell 方法 VB 里有一个 Shell 方法，但是在 RB 中并没有，所以此路不通。 3. 使用 ShellExecute 这个方法同样是一个系统 API，可以直接通过 RB 的 declare 声明并调用它，在测试之后，使用 declare 来使用系统 API 不会生成额外的 dll，正是我需要的。 首先在 RB 的某个模块中添加一个方法 ShellExecute，用来封装对系统 API 的请求： function ShellExecute(hWnd as Integer, lpOperation as String, _ lpFile as String, lpParameters as String, lpDirectory as String, nShowCmd as Integer) 注意，在 RB 中添加方法时，参数列表中的 _ 需要去掉，这里是为了排版的需要而加上的。 这个 API 是定义在 shell32.dll 中的，在 ShellExecute 方法中先需要声明： soft declare function ShellExecuteA Lib &#8221;shell32.dll&#8221; _ (ByVal hWnd As Integer, ByVal lpOperation As CString, _ ByVal lpFile As CString, ByVal lpParameters As CString, _ ByVal lpDirectory As CString, ByVal nShowCmd As Integer) As Integer soft declare function ShellExecuteW Lib &#8221;shell32.dll&#8221; _ (ByVal hWnd As Integer, ByVal lpOperation As CString, _ ByVal lpFile As CString, ByVal lpParameters As CString, _ ByVal lpDirectory As CString, ByVal nShowCmd As Integer) As Integer 然后来调用这个 API： dim ret as Integer Try ret = ShellExecuteW(hWnd, ConvertEncoding(lpOperation, Encodings.UTF16), _ ConvertEncoding(lpFile, Encodings.UTF16), _ ConvertEncoding(lpParameters, Encodings.UTF16), _ ConvertEncoding(lpDirectory, &#8230;<p class="read-more"><a href="http://xujiwei.com/blog/using-shellexecute-in-realbasic/">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<a href="http://xujiwei.com/blog/using-shellexecute-in-realbasic/" title="REALbasic 中使用 ShellExecute 执行命令"></a><p>在 REALbasic 中，如果需要执行 cmd 命令，可以直接使用 Shell 类，但是这样的话，编译成 Windows 程序时会额外需要一个 Shell.dll 的动态链接库，这对于我这样的 1exe 爱好者是不能忍受的。但是对于 Mac OS X 和 Linux 的生成目标来说，是不存在这个问题的，因为 Mac OS X 的应用程序本身就是一个文件夹，而 Linux 的目标不会生成额外的链接库。因此，需要针对 Windows 进行特殊处理。于是在网上搜索解决方案，找到了 VB 中执行程序的几种方法：</p>

<p><strong>1. 使用 CreateProcess</strong></p>

<p>通过 CreateProcess 以及使用管道，可以执行外部程序并获取输出，但是这个方法过于烦琐，并且我也不需要外部进程执行完毕后的输出结果，因此不采用。</p>

<p><strong>2. 使用 Shell 方法</strong></p>

<p>VB 里有一个 Shell 方法，但是在 RB 中并没有，所以此路不通。</p>

<p><strong>3. 使用 ShellExecute</strong></p>

<p>这个方法同样是一个系统 API，可以直接通过 RB 的 declare 声明并调用它，在测试之后，使用 declare 来使用系统 API 不会生成额外的 dll，正是我需要的。</p>

<p>首先在 RB 的某个模块中添加一个方法 ShellExecute，用来封装对系统 API 的请求：</p>

<blockquote>function ShellExecute(hWnd as Integer, lpOperation as String, _
lpFile as String, lpParameters as String, lpDirectory as String, nShowCmd as Integer)</blockquote>

<p>注意，在 RB 中添加方法时，参数列表中的 _ 需要去掉，这里是为了排版的需要而加上的。</p>

<p>这个 API 是定义在 shell32.dll 中的，在 ShellExecute 方法中先需要声明：</p>

<blockquote>soft declare function ShellExecuteA Lib &#8221;shell32.dll&#8221; _
(ByVal hWnd As Integer, ByVal lpOperation As CString, _
ByVal lpFile As CString, ByVal lpParameters As CString, _
ByVal lpDirectory As CString, ByVal nShowCmd As Integer) As Integer

soft declare function ShellExecuteW Lib &#8221;shell32.dll&#8221; _
(ByVal hWnd As Integer, ByVal lpOperation As CString, _
ByVal lpFile As CString, ByVal lpParameters As CString, _
ByVal lpDirectory As CString, ByVal nShowCmd As Integer) As Integer</blockquote>

<p>然后来调用这个 API：</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">  <span style="color: #151B8D; font-weight: bold;">dim</span> ret <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    Try
      ret = ShellExecuteW(hWnd, ConvertEncoding(lpOperation, Encodings.UTF16), _
      ConvertEncoding(lpFile, Encodings.UTF16), _
      ConvertEncoding(lpParameters, Encodings.UTF16), _
      ConvertEncoding(lpDirectory, Encodings.UTF16), nShowCmd)
    Catch
      ret = ShellExecuteA(hWnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
    <span style="color: #8D38C9; font-weight: bold;">End</span>
    return ret</pre></div></div>


<p>把 ShellExecuteW 放在 Try &#8230; Catch 中是为了兼容的需要，因为像 Windows 98 这样比较老的系统中，系统内部编码还不是 Unicode，还没有 ShellExecuteW 这样结尾是 W 的 API。</p>

<p>另外，因为 RB 内部是使用的 UTF-8 编码，而系统 API ShellExecuteW 使用的编码是 Unicode，所以参数在传递前需要进行转码，将字符串参数值转换为 Unicode 编码，否则在执行一个明明正确的命令时，会出现“找不到文件”的错误。</p>

<p>好了，这样就可以使用 ShellExecute 方法来执行外部程序了，例如：</p>


<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">  #if TargetWin32
    <span style="color: #151B8D; font-weight: bold;">dim</span> ret <span style="color: #151B8D; font-weight: bold;">as</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    ret = Win32Helper.ShellExecute(hWnd, <span style="color: #800000;">&quot;open&quot;</span>, <span style="color: #800000;">&quot;cmd.exe&quot;</span>, _
    <span style="color: #800000;">&quot;/c shutdown -t 10&quot;</span>, <span style="color: #800000;">&quot;&quot;</span>, 0)
  #endif</pre></div></div>


<p>上面的代码就调用 cmd.exe 执行了命令 shutdown -s -t 10，也就是 10 秒后关机。</p>

<p>其实这个方法也可以用来打开文档或者网址，具体的用法可以去 MSDN 找一找。</p>

<p>当然，如果不在乎生成的 Windows 目标还有额外的 dll 的话，完全可以使用 RB 自带的 Shell 类，功能也强上不少。</p>
]]></content:encoded>
			<wfw:commentRss>http://xujiwei.com/blog/using-shellexecute-in-realbasic/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

