<?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; Win32</title>
	<atom:link href="http://xujiwei.com/blog/tags/win32/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>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>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>

