科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道用VB.net实现Smartphone中进程启动

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

     我选择的编程工具当然是Visual Basic NET2003,实话说Microsoft .NET Framework SDK 提供的功能实在不感恭维,微软好象又要将所有走上光明大道的.NET程序员拖入API的黑暗时代。

作者:builder.com.cn 2007年9月29日

关键字: 编程

  • 评论
  • 分享微博
  • 分享邮件
  
  我选择的编程工具当然是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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章