栏目搜索
 
 
 
 
你的位置:首页 > 移动开发 > VB.NET实现Smartphone进程启动 >
 

VB.NET实现Smartphone进程启动

发布者:[本站编辑] | 来源:[]

VB.NET实现Smartphone进程启动_电脑维修资料库

近日花巨资购进多普达智能手机一台, 系统为微软的smartphone2003 ,由于对瘟倒死操作系统的熟悉,很快已步入正轨,各种图片、程序安装了一大堆后,开始发挥自己的程序特长,试着为爱机写一个资源管理器的简单程序。

  由于我一直是vb的拥趸,选择的编程工具当然是时下最火的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)。</t

 
 
  推荐文章
 
     暂无
 
 
  随机资讯
 
     暂无