在.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);