最近在wince下用VB.net编程,想实现win32的API的调用,从而在一个主应用程序中调用其他的exe应用程序;找了很久,终于找到了大侠的牛文!甚为感谢。一下内容为转载
近日心血来潮,花巨资购进多普达智能手机一台, 系统为微软的Smartphone2003 ,由于对"瘟倒死"操作系统的熟悉,很快已步入正轨,各种图片、程序安装了一大堆后,开始发挥自己的程序特长,试着为爱机写一个资源管理器的简单程序。
我选择的编程工具当然是Visual Basic NET2003,实话说Microsoft .NET Framework SDK 提供的功能实在不感恭维,微软好象又要将所有走上光明大道的.NET程序员拖入API的黑暗时代,指针、句柄、地址操作,最令人恼火就是没有为VB.net程序做WinCE的API 声明(令人怀念VB6的API浏览器)很快我就在如何启动手机中的应用程序的代码上困住了,Net没有为WinCE提供Process 对象这种不付责任的做法害得我好苦啊!
开始时声明时老是将ShellExecuteEx 和CreateProcess这两个熟悉得不能再熟悉的Win32在VB 6中的声明直接粘到程序中,仅是将VB6的long改为NET的int32,结果老是报错。在网上查了N天资料,才发现原来Win CE的核心API都是在一个coredll.dll文件中,使得我差点没气死!还有一点是SHELLEXECUTEINFO结构中的lpFile这种指向字符串常量的指针由于字符的编码问题不能象在Win32编程中简单声明为String类型,所在必须声明为Intptr,这样问题就出现了,使用ShellExecuteEx函数必须使用微软在VB.net中不直接提供的指针操作,所以我只好根据网上的资料改写了一个VB.net对内存指针的封装类,详细代码及解释如下(新建一个模块,将以下代码粘贴进去即可使用):
Imports System.Runtime.InteropServices '引及Net框架中对底层操作支持的命名空间 Public Class clsDAMSMobileMarshal '我写的内存管理类 #Region "与内存有关的API声明" REM 以下是与内存有关的移动设备API Public Declare Function LocalAlloc Lib "coredll.dll" Alias "LocalAlloc" (ByVal wFlags As Int32, _ ByVal wBytes As Int32) As IntPtr Public Declare Function LocalFree Lib "coredll.dll" Alias "LocalFree" (ByVal hMem As Int32) As Int32 Public Declare Function LocalLock Lib "coredll.dll" Alias "LocalLock" (ByVal hMem As Int32) As Int32 Public Declare Function LocalReAlloc Lib "coredll.dll" Alias "LocalReAlloc" (ByVal hMem As IntPtr, _ ByVal wBytes As Int32, ByVal wFlags As Int32) As IntPtr #End Region #Region "API常量声明" Public Const LMEM_FIXED = 0 Public Const LMEM_MOVEABLE = &H2 Public Const LMEM_ZEROINIT = &H40 Public Const LPTR = LMEM_FIXED Or LMEM_ZEROINIT #End Region Public Shared Function fnAllocHLocal(ByVal ni_i32Size As Int32) As IntPtr '申请本地内存,返回一个指向该内存块的指针 Return LocalAlloc(LPTR, ni_i32Size) End Function Public Shared Function fnFreeHLocal(ByRef ni_pLocal As IntPtr) As Int32 REM 释放指定的内存块柄 Dim ti32FunctionReturnValue As Int32 If ni_pLocal.Equals(IntPtr.Zero) = False Then ti32FunctionReturnValue = (LocalFree(ni_pLocal.ToInt32)) If ti32FunctionReturnValue = 0 Then ni_pLocal = IntPtr.Zero End If End If Return (ti32FunctionReturnValue) End Function Public Shared Function fnReAllocHLocal(ByVal ni_pIn As IntPtr, ByVal ni_i32Size As Int32) As IntPtr '对指定的内存块重新定义大小 Return LocalReAlloc(ni_pIn, ni_i32Size, LMEM_MOVEABLE) End Function Public Shared Function fnStringToHLocalUni(ByVal ni_strIn As String) As IntPtr '将指定的字符串复制到一个内存块中,并返回该内存块的指针,这个指针必须使用fnFreeHLocal函数释放 Dim ti32StringBufLength As Int32 Dim tpTempA As IntPtr If Not (ni_strIn Is Nothing) Then If ni_strIn.Length = 0 Then Return IntPtr.Zero Else ti32StringBufLength = (ni_strIn.Length + 1) * 2 ' 包括最后一个中止字符 tpTempA = fnAllocHLocal(ti32StringBufLength) If tpTempA.Equals(IntPtr.Zero) = False Then '申请内存成功 Marshal.Copy(ni_strIn.ToCharArray, 0, tpTempA, ni_strIn.Length) Return tpTempA End If End If End If End Function End Class |
有了这个辅助函数,以下的API调用就简单了,只需正确声明即可,以下是ShellExecuteEx及CreateProcess涵数及其结构的正确声明(同样是将代码粘贴到一个新建模块中即可调用):
imports System.Runtime.InteropServices REM API常数声明------------------------- Public Const SW_SHOWNORMAL = 1 Public Const gcNORMAL_PRIORITY_CLASS = &H20 Public Const gcINFINITE = &HFFFF Public Const WAIT_TIMEOUT = &H102& #Region "Structure SHELLEXECUTEINFO" <StructLayout(LayoutKind.Sequential)> _ Structure SHELLEXECUTEINFO Public cbSize As Int32 Public fMask As Int32 Public hwnd As IntPtr Public lpVerb As IntPtr 'LPCTSTR,这种类型不能声明为string,只可以老老实实声明为Intptr Public lpFile As IntPtr 'LPCTSTR,这种类型不能声明为string,只可以老老实实声明为Intptr Public lpParameters As IntPtr 'LPCTSTR,这种类型不能声明为string,只可以老老实实声明为Intptr Public lpDirectory As IntPtr 'LPCTSTR,这种类型不能声明为string,只可以老老实实声明为Intptr Public nShow As Int32 Public hInstApp As IntPtr 'Optional members Public lpIDList As IntPtr 'LPVOID Public lpClass As IntPtr 'LPCTSTR Public hkeyClass As Int32 Public dwHotKey As Int32 Public hIcon As Int32 Public hProcess As IntPtr Public Sub Dispose() '在调用后释放结构中的内存块 clsDAMSMobileMarshal.fnFreeHLocal(Me.lpVerb) clsDAMSMobileMarshal.fnFreeHLocal(Me.lpFile) clsDAMSMobileMarshal.fnFreeHLocal(Me.lpParameters) clsDAMSMobileMarshal.fnFreeHLocal(Me.lpDirectory) End Sub End Structure #End Region #Region "Structure STARTUPINFO" <StructLayout(LayoutKind.Sequential)> _ Public Structure STARTUPINFO Public cb As Int32 Public lpReserved As IntPtr Public lpDesktop As IntPtr Public lpTitle As IntPtr Public dwX As Int32 Public dwY As Int32 Public dwXSize As Int32 Public dwYSize As Int32 Public dwXCountChars As Int32 Public dwYCountChars As Int32 Public dwFillAttribute As Int32 Public dwFlags As Int32 Public wShowWindow As Int16 Public cbReserved2 As Int16 Public lpReserved2 As Int32 Public hStdInput As Int32 Public hStdOutput As Int32 Public hStdError As Int32 End Structure #End Region #Region "Structure PROCESS_INFORMATION" <StructLayout(LayoutKind.Sequential)> _ Public Structure PROCESS_INFORMATION Public hProcess As IntPtr Public hThread As IntPtr Public dwProcessId As Int32 Public dwThreadId As Int32 End Structure #End Region REM api函数声明--------------------------------- #Region "Function CreateProcess" public Overloads 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, _ ByRef si As STARTUPINFO, _ ByRef pi As PROCESS_INFORMATION _ ) As Integer #End Region #Region "Function CloseHandle" Public Declare Function CloseHandle Lib "CoreDll.dll" (ByVal Handle As IntPtr) As Int32 #End Region #Region "Function WaitForSingleObjectEx" Public Declare Function WaitForSingleObjectEx Lib "coredll.dll" (ByVal hHandle As IntPtr, _ ByVal dwMilliseconds As Int32, _ ByVal bAlertable As Int32 _ ) As Int32 #End Region #Region "Function ShellExecuteEx" Public Declare Function ShellExecuteEx Lib "coredll.dll" (ByRef lpExecInfo As SHELLEXECUTEINFO) As Int32 #End Region |
下面是一个简单示例,启动手机中的计算器程序,步骤如下:
1、启动VS.net2003,选Visual Basic 项目,在右边的列表中选智能设备应用程序,在下一个对话框中针对什么平台选Smartphone,类型选Windows应用程序。
2、将上面介绍的辅助类及API声明的代码做成两个模块。
3、为在form1的代码模块顶部加入如下语句:
Imports System.Runtime.InteropServices |
4、在 Form1的 load事件中加入启动计算器的代码如下:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim tudtShellExecuteInfo As New SHELLEXECUTEINFO Dim tstrExecutePath As String Dim tudtStartupInfo As STARTUPINFO Dim tudtProcessInfo As PROCESS_INFORMATION Dim tlngTempa As Int32 With tudtStartupInfo .cb = Marshal.SizeOf(GetType(STARTUPINFO)) End With With tudtShellExecuteInfo .cbSize = Marshal.SizeOf(GetType(SHELLEXECUTEINFO)) .lpFile = clsDAMSMobileMarshal.fnStringToHLocalUni("\windows\calc.exe") .lpParameters = IntPtr.Zero '不使用参数,如使用参数可按lpFile的方法申请字符串指针 End With tlngTempa = ShellExecuteEx(tudtShellExecuteInfo) '启动程序 tudtShellExecuteInfo.Dispose() If tlngTempa = 0 Then '使用shellexecuteex失败,试图使用createprocess再打开试试 tlngTempa = CreateProcess("\windows\calc.exe", "\windows\calc.exe", IntPtr.Zero, IntPtr.Zero, _ 0, 0, IntPtr.Zero, IntPtr.Zero, tudtStartupInfo, tudtProcessInfo) If tlngTempa <> 0 Then Call CloseHandle(tudtProcessInfo.hThread) Call CloseHandle(tudtProcessInfo.hProcess) End If End If End Sub |
以上就是VB.net中启动Smartphone中的应用程序的常规方法,代码在本人的计算机上测试通过,本人的计算机系统配置为WIN2000 高级服务器版+Visual Studio 2003(完全安装)+Smartphone 2003 SDK(开发Smartphone应用程序必备,在微软的网站可以下载,微软提供的免费软件啊!我喜欢8-),网址如下: http://msdn.microsoft.com/mobility/downloads/sdks/default.aspx)。有问题可以 和我联系: missilecat@163.com。
注:大侠说的模块在VS2005中第一个是添加一个类;第二个是添加一个model。再次实践证明大侠确属牛人!