用.Net Compact Framework 1.1開發時開外部程式

之前有學弟為了在一台有Win CE環境的PDA上開發應用程式傷腦筋

原因是Visual Studio 2005提供.Net Compact Framework 2.0 無法發佈在比較舊版的Win CE上,

所以就使用.Net Compact Framework 1.1在開發,然後就出現了一個問題,

要呼叫外部程式的話,去MSDN找現有函式只有2.0的才有,

1.1的就要用我下面說的東西了~

PS.我在Google上找 VB 範例,能直接用不會有錯的都不在前面的搜尋結果,讓我花了不少功夫~

開發環境

  • Microsoft Visual Studio 2005
  • 語言:VB

程式功能

  • 在.Net Compact Framework 1.1的環境下,使用VB程式呼叫外部的程式
  • 等外部程式結束後,才繼續往下一行程式執行

開發要點

開一個模組,來放下面的程式

Module MyProcessCreater



 Declare Function CreateProcess Lib "CoreDll.dll" (ByVal imageName As String, ByVal cmdLine As String, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, ByVal boolInheritHandles As Int32, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpszCurrentDir As IntPtr, ByVal si As Byte(), ByVal pi As ProcessInfo) As Integer



 Declare Function WaitForSingleObject Lib "CoreDll.dll" (ByVal Handle As IntPtr, ByVal Wait As Int32) As Int32



 Declare Function GetLastError Lib "CoreDll.dll" () As Int32



 Declare Function CloseHandle Lib "CoreDll.dll" (ByVal Handle As IntPtr) As Int32





 Public Class ProcessInfo

     Public hProcess As IntPtr

     Public hThread As IntPtr

     Public ProcessId As Int32

     Public ThreadId As Int32

 End Class 'ProcessInfo





 Public Function CreateProcess(ByVal ExeName As String, ByVal CmdLine As String, ByVal pi As ProcessInfo) As Boolean

     Dim INFINITE As Int32

     INFINITE = &HFFFFFFFF



     Dim WAIT_OBJECT_0 As Int32 = 0

     Dim result As Int32



     If pi Is Nothing Then

         pi = New ProcessInfo

     End If

     Dim si(128) As Byte



     result = CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) '呼叫外部程式

     If 0 = result Then

         Return False

     End If

     result = WaitForSingleObject(pi.hProcess, INFINITE) '等外部程式結束

     CloseHandle(pi.hThread)

     CloseHandle(pi.hProcess)

     If WAIT_OBJECT_0 <> result Then

         Return False

     End If

     Return True

 End Function



End Module 

然後在要呼叫的地方加入以下程式

CreateProcess("要呼叫的程式路徑", "", Nothing)

第一個參數:"要呼叫的程式路徑" 如"\Windows\iexplore.exe"

第二個參數:是外部程式用的參數

第三個參數:我也不清處怎麼用

參考資料

'方法1
returnID = Shell("D:\run.txt", vbNormalFocus)

'方法2
System.Diagnostics.Process.Start ("D:\run.txt")

'方法3
Dim myProcess As Process = System.Diagnostics.Process.Start
("D:\run.txt")
MessageBox.Show(myProcess.ProcessName)

'方法4
Dim psInfo As New _
System.Diagnostics.ProcessStartInfo _
("D:\run.txt")
psInfo.WindowStyle = _
System.Diagnostics.ProcessWindowStyle.Normal
Dim myProcess As Process = _
System.Diagnostics.Process.Start(psInfo)

'方法5
Dim myProcess As System.Diagnostics.Process = _
new System.Diagnostics.Process()
myProcess.StartInfo.FileName = _
"D:\run.txt"
myProcess.StartInfo.WindowStyle = _
System.Diagnostics.ProcessWindowStyle.Normal
myProcess.Start

3 意見:

chen 提到...

請教,您是如何將VS2005 設定為使用Framework1.1。目前遇到這個問題,不過小的不才,不會設定。

chen 提到...

請教,您是如何將VS2005 設定為使用Framework1.1。目前遇到廠商是用VS2003 VB開發Win CE 程式。不過目前公司只有VS2005可以使用。想說可不可以利用您的方法,來便來維護程式,謝謝!

倫倫3號 提到...

這方法應該不是用在互相支援用的~我這篇是2006年寫的~都不記得當初在做什麼了...

這裡是關於技術的手札~

也歡迎大家到

倫與貓的足跡



到噗浪來

關心一下我唷!
by 倫
 
Copyright 2009 倫倫3號Beta-Log All rights reserved.
Blogger Templates created by Deluxe Templates
Wordpress Theme by EZwpthemes