2012-06-13 63 views
2

好的,所以我需要打開一個.txt文件,該文件將在與程序相同的文件中創建。在C中使用ShellExecute()打開.txt的正確方法是什麼

我想使用ShellExecute();做到這一點,我已經做了大量的研究,我只是無法得到正確的語法主要是因爲我不知道如何處理參數「HWND」

我看了here的答案,並得到了所有除了在HWND

放什麼

這裏的信息是我需要使用的代碼:

ShellExecute(0,"open","c:\\debug.txt",NULL,NULL,1); 

感謝您的幫助問,如果你不能確定什麼,我說什麼! :)

這是我使用來測試功能的程序:

#include "DAL.h" 
//DAL.h added to Testing file to make compiling easier 
//Created to test show_debug() 
int main(void) 
{ 
    int test1,test2,final; 

    puts("Enter 2 numbers to add (2,2)"); 
    scanf("%d,%d",&test1,&test2); 

    log_debug(test1); 
    log_debug(test2); 

    view_debug(); 

    final= test1+test2; 
    printf("%d\n",final); 

    log_debug(final); 

    return(0); 
} 

view_debug();是包含ShellExecute的函數

void view_debug(void)//WIP 
//Opens the debug.txt in notepad 
{ 
    LoadLibrary("shell32.dll"); 
    ShellExecute(0,"open","c:\\debug.txt",NULL,NULL,1); 
} 

這是log_debug();

int log_debug(int test_variable) 
//This function simply tests the programmers desired veriable & displays it for help in keeping track of a veriables value(integer). 
//The function has support for upto 1 variable for testing 
{ 
    time_t now; 
    time(&now); 

    FILE *debug; //Creates file to write debug info 

    debug=fopen("debug.txt", "a+"); 
    fprintf(debug,"DEBUG %.24s: <%d>\n", ctime(&now),test_variable); 
    //TODO: Allow more than one variable 

    fclose(debug); 

    return(0); 
} 

該文件由函數log_debug()創建;它確實有效,但必須手動打開,因爲ShellExecute不起作用。

全部來源Here.

+1

在view_debug LoadLibrary調用多餘的,沒有必要,因爲你已經與SHELL32.LIB鏈接。 – selbie

回答

5

這應該爲你工作:

#include <windows.h> 
#include <ShellApi.h> 

void view_debug(const char* pszFileName) 
{ 
    ShellExecuteA(GetDesktopWindow(),"open",pszFileName,NULL,NULL,SW_SHOW); 
} 

int main() 
{ 
    view_debug("c:\\debug.txt"); 
} 

如果它不工作,則有可能有兩個或三個原因:

  1. 您使用程序代碼創建了debug.txt文件,但由於您沒有關閉文件句柄(例如,這取決於你用log_debug打開文件的方式:fclose(),CloseHandle(),close()等等),或者因爲你打開的文件沒有FILE_SHARE_READ標誌。

  2. 您實際上沒有權限從c:\驅動器的根目錄讀取數據。非管理帳戶通常是這樣。

  3. c:\ debug.txt實際上並不像您認爲的那樣存在。

1

正如你鏈接到頁面表示:

該值可以是NULL如果操作不與 窗口相關聯。

你可能要指定一個父窗口的理由是,如果您的應用程序顯示一個窗口,你可能會想你的窗口,成爲該API的ShellExecute可能會顯示任何消息框父。如果你說NULL,那麼ShellExecute會將它的消息框顯示爲頂級窗口,所以用戶可能想知道什麼應用程序正在顯示該框。

1

通常NULL就夠了。從ShellExecute文檔:

HWND [中,可選]

Type: HWND 

A handle to the parent window used for displaying a UI or error messages. 
This value can be NULL if the operation is not associated with a window. 
+0

這就是爲什麼程序不打開文件... – Bevilacqua

+0

檢查返回值。如果你已經有了,如果你不明白髮生了什麼問題,請發佈錯誤代碼)。發佈整個代碼。確保文件存在。你在'shell32.dll'中鏈接了嗎?還是你在使用'LoadLibrary(「shell32.dll」)?另外,您可能需要通過'CoInitialize'初始化COM。 – dirkgently

+0

檢查源文件的原始文章...我添加了loadlibrary,但我不熟悉coinitialize。 – Bevilacqua

0

MSDN上ShellExecute function syntax

HINSTANCE ShellExecute(
    _In_opt_ HWND hwnd, 
    _In_opt_ LPCTSTR lpOperation, 
    _In_  LPCTSTR lpFile, 
    _In_opt_ LPCTSTR lpParameters, 
    _In_opt_ LPCTSTR lpDirectory, 
    _In_  INT  nShowCmd 
); 

你可以嘗試這樣的。 你可以用你的文本文件路徑("c:\\debug.txt")的參數打開"notepad"

ShellExecute(0,"open", "notepad", "c:\\debug.txt", NULL, SW_SHOW); 
+0

我有點麻煩理解你的答案。這更接近你的意思嗎?如果我錯了,請[編輯]您的文章進行改進或完全從[修訂歷史記錄](http://stackoverflow.com/posts/42945593/revisions)展開更改。 –

相關問題