分享
 
 
 

用VB.net实现Smartphone中进程启动

王朝c#·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

我选择的编程工具当然是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、将上面介绍的辅助类及AP

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有