【问题标题】:What is the correct way to use ShellExecute() in C to open a .txt在 C 中使用 ShellExecute() 打开 .txt 的正确方法是什么
【发布时间】:2012-06-13 02:38:04
【问题描述】:

好的,所以我需要打开一个 .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.

【问题讨论】:

  • view_debug 中的 loadlibrary 调用是多余的,不需要,因为您已经与 shell32.lib 链接。

标签: c windows parameters hwnd


【解决方案1】:

这应该适合你:

#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 实际上并不像您想象的那样存在。

【讨论】:

  • 有趣的是,即使没有#include ,您的代码也能正常工作。这是有原因的吗?
  • &lt;windows.h&gt; 包括&lt;shellapi.h&gt;,但没有WIN32_LEAN_AND_MEAN。在早期版本的 Windows SDK 中可能并非如此。或者当时的默认 Visual Studio 项目可能已经在 stdafx.h 中定义了 lean_and_mean 宏。我不记得了。
【解决方案2】:

如您链接到的页面所述:

如果操作不与 窗口。

您可能希望指定父窗口的原因是,如果您的应用程序正在显示一个窗口,您可能希望您的窗口成为 ShellExecute API 可能显示的任何消息框的父窗口。如果您说 NULL,那么 ShellExecute 会将其消息框显示为顶级窗口,因此用户可能想知道哪个应用程序正在显示该框。

【讨论】:

    【解决方案3】:

    通常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.
    

    【讨论】:

    • 检查返回值。如果您已经有,如果您无法理解出了什么问题,请发布错误代码)。发布整个代码。确保文件存在。您是链接到shell32.dll 还是使用LoadLibrary( "shell32.dll" )?此外,您可能需要通过 CoInitialize 初始化 COM。
    • 检查原始帖子的来源...我添加了loadlibrary,但我不熟悉coinitialize。
    • @Bevilacqua:你检查返回值是否> 32?另外,在调用ShellExecute 之前调用CoInitialize( NULL );,然后调用CoUninitialize()
    • 当我尝试调用 CoInitialize(NULL);我得到错误 undefined reference to `CoInitialize@4'|
    • 您需要将Ole32.lib添加到您的项目设置>链接器>输入>附加依赖项。您也可以在此处添加 shell32.dll 并摆脱 LoadLibrary 调用。
    【解决方案4】:

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

    【讨论】:

    • 我在理解您的回答时遇到了一些麻烦。这更接近你的意思吗?如果我弄错了,请edit您的帖子进行改进或完全从revision history展开更改。
    猜你喜欢
    • 2020-10-24
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    相关资源
    最近更新 更多