Visual C 中呼叫其他應用程式的方法(函式)

2021-04-16 04:40:26 字數 4283 閱讀 9959

本文所有程式在visual studio.net 2003 c 環境下編寫,編譯過程沒有錯誤。現在都使用visual studio 2005了。不知道還能編譯通過不

三個sdk函式winexec,shellexecute,createprocess實現呼叫其他程式的方法

visual c中呼叫其他應用程式的方法(函式)

三個sdk函式:winexec,shellexecute

,createprocess

可以實現呼叫其他程式的要求,其中以winexec最為簡單,shellexecute比winexec靈活一些,createprocess最為複雜。

winexec

兩個引數,前乙個指定路徑,後乙個指定顯示方式。

shellexecute

可以指定工作目錄,並且還可以尋找檔案的關聯直接開啟不用載入與檔案關聯的應用程式,shellexecute還可以開啟網頁,啟動相應的郵件關聯傳送郵件等等。

createprocess

一共有十個引數,不過大部分都可以用null代替,它可以指定程序的安全屬性,繼承資訊,類的優先順序等等。如果我們要得到足夠多的關於新的程序的資訊,控制新的程序的細節屬性,若要達到這些目的,我們就需要使用createprocess函式了。

三個sdk函式(winexec、shellexec、crateprocess)的語法:

這個函式最簡單,只有兩個引數,原型如下:

uint winexec(

lpcstr lpcmdline,// 命令路徑

uint ucmdshow// 顯示方式

);

使用方法如下:

winexec("notepad.exe", sw_show); // 開啟記事本

winexec("d://program files//test//test.exe",sw_showmaximized); // 以最大化的方式開啟test.exe

需要注意的是若用sw_showmaxmized

方式去載入乙個無最大化按鈕的程式,譬如neterm

,calc

等等,就不會出現正常的

窗體,但是已經被加到任務列表裡了。

原型如下:

hinstance shellexecute(

hwnd hwnd,//父視窗控制代碼

lpctstr lpoperation,//操作, 開啟方式"edit","explore","open","find","print","null"

lpctstr lpfile,//檔名,前面可加路徑

lpctstr lpparameters,//引數

lpctstr lpdirectory,//預設資料夾

int nshowcmd//顯示方式

);

使用方法如下:

shellexecute(null,"open","c://test.txt",null,null,sw_shownormal); // 開啟c:/test.txt 檔案

shellexecute(null, "open", "http://www.google.com", null, null, sw_shownormal); // 開啟網頁www.google.com

shellexecute(null,"explore", "d://c ",null,null,sw_shownormal); // 開啟目錄d:/c

shellexecute(null,"print","c://test.txt",null,null, sw_hide); // 列印檔案c:/test.txt

shellexecute

不支援定向輸出。

原型如下:

bool createprocess(

lptstr lpcommandline,// 引數行

//下面兩個引數描述了所建立的程序和執行緒的安全屬性,如果為null則使用預設的安全屬性

lpsecurity_attributes lpprocessattributes,// process security attributes

lpsecurity_attributes lpthreadattributes,// thread security attributes

bool binherithandles,// 繼承標誌

dword dwcreationflags,// 建立標誌

lpvoid lpenvironment,// 環境變數

lpctstr lpcurrentdirectory,// 執行該程序的初始目錄

lpstartupinfo lpstartupinfo,// 用於在建立子程序時設定各種屬性

lpprocess_information lpprocessinformation//用於在程序建立後接受相關資訊

);

使用方法如下:

process_information pi;

startupinfo si;

memset(&si,0,sizeof(si));

si.cb=sizeof(si);

si.wshowwindow=sw_show;

si.dwflags=startf_useshowwindow;

bool fret=createprocess("d://putty.exe",null,null,false,null,null,null,null,&si,&pi);

可以看出,通過上面的幾個不同的方法,都可以實現在應用程式中開啟其他應用程式的目的,其中有些方法可能會麻煩一點,所以就需要我們根據不同的目的去選擇最適合自己的方法去實現自己的目的!

關於三個sdk函式:winexec,shellexecute

,createprocess

的其他注意事項:

1、定義標頭檔案

在標頭檔案stdafx.h中必須定義以下兩個標頭檔案:

#include //可替換為windows.h

#include

如果定義了標頭檔案#include的話就不必定義#include 了,"windows.h" 不光是包含了"shellapi.h",它還定義了許多資料型別,如果沒有這些資料型別,shellapi.h本身會出錯。

2、定義路徑

c 中所表示的路徑要用" // "而不是平常所用的" / ",所以以上三個函式表示路徑都為:

disk://directory//...//file name

winexec("d://program files//test//test.exe",sw_showmaximized);

shellexecute(null,"open","c://test.txt",null,null,sw_shownormal);

bool fret=createprocess("d://putty.exe",null,null,false,null,null,null,null,&si,&pi);

Visual C 中呼叫Windows服務初探

這裡把執行緒的優先順序設到最低,這樣不會耗用過多的系統效能。這個執行緒物件使用threadfunc作為執行緒函式,因此將這個執行緒函式補充完整 public static void threadfunc 安裝完後能過系統的服務管理器你就可以看到你的服務了,只要點選啟動就可以把它啟動,把時間向前改乙個...

iOS呼叫其他應用 的寫法

一 呼叫系統應用 1 呼叫其它應用的方法 1 呼叫 自帶mail 2 呼叫 phone 3 呼叫 sms 4 呼叫自帶 瀏覽器 safari 5 呼叫 remote 二 呼叫自己開發的應用 1 在plist檔案中,註冊對外介面 滑鼠右擊information property list 然後從列表中...

C 中呼叫其他應用程式的方法

winexec,shellexecute,createprocesswinexec lpcstr lpcmdline,命令路徑 uint ucmdshow 顯示方式 使用方法如下 winexec notepad.exe sw show 開啟記事本 winexec d program filestes...