在.net Compact Framework中用一个程序调用另一个程序!其中用到三个API 这三个API的原型可以在 .NET Compact Framework SDK 帮助中查到! 希望对大家有用
代码如下
using System.Runtime.InteropServices;
using System.Text;
class SHELLEXECUTEEX
{
public UInt32 cbSize;
public UInt32 fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public IntPtr lpFile;
public IntPtr lpParameters;
public IntPtr lpDirectory;
public int nShow;
public IntPtr hInstApp;
// 以下的成员目前没用,可选
public IntPtr lpIDList;
public IntPtr lpClass;
public IntPtr hkeyClass;
public UInt32 dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
[DllImport("coredll")]
extern static int ShellExecuteEx(ref SHELLEXECUTEEX ex ); //注意,原函数要的是指针!所以必为ref
[DllImport("coredll")]
extern static IntPtr LocalAlloc( int flags, int size );
[DllImport("coredll")]
extern static void LocalFree( IntPtr ptr );
// 如何使用的代码在这里!
string docname = @"/windows/default.htm";
int nSize = docname.Length * 2 + 2;
IntPtr pData = LocalAlloc(0x40, nSize);
Marshal.Copy(Encoding.Unicode.GetBytes(docname), 0, pData, nSize - 2);
SHELLEXECUTEEX see = new SHELLEXECUTEEX();
see.cbSize = 60;
see.dwHotKey = 0;
see.fMask = 0;
see.hIcon = IntPtr.Zero;
see.hInstApp = IntPtr.Zero;
see.hProcess = IntPtr.Zero;;
see.lpClass = IntPtr.Zero;
see.lpDirectory = IntPtr.Zero;
see.lpIDList = IntPtr.Zero;
see.lpParameters = IntPtr.Zero;
see.lpVerb = IntPtr.Zero;
see.nShow = 0;
see.lpFile = pData;
ShellExecuteEx(ref see);
LocalFree(pData);