Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1930833
  • 博文数量: 699
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 4970
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 13:45
文章分类

全部博文(699)

文章存档

2011年(1)

2008年(698)

我的朋友

分类:

2008-10-15 13:53:39

本文作者将在论坛VB/VB.NET版回答读者的相关问题

  近日心血来潮,花巨资购进多普达智能手机一台, 系统为微软的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"
_
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"
_
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"
_
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,类型选应用程序。

  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("\\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

【责编:snow】

--------------------next---------------------
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"
_
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"
_
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"
_
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

--------------------next---------------------
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("\\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
--------------------next---------------------

阅读(385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~