在C中使用ShellExecute()打开.txt的正确方法是什么

Bev*_*qua 2 c windows parameters hwnd

好吧,所以我需要打开一个.txt文件,该文件将在与程序相同的文件中创建.

我想使用ShellExecute(); 要做到这一点,我已经做了很多研究,我似乎无法得到正确的语法,因为我不知道如何处理参数"HWND"

在这里寻找答案并得到了所有信息,除了要放入HWND的内容

以下是我需要使用的代码:

ShellExecute(0,"open","c:\\debug.txt",NULL,NULL,1);
Run Code Online (Sandbox Code Playgroud)

如果你不确定我在说什么,谢谢你的帮助!:)

这是我用来测试函数的程序:

  #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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

这是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);
}
Run Code Online (Sandbox Code Playgroud)

该文件由函数log_debug()创建; 它确实有效但必须手动打开,因为ShellExecute不起作用.

完全来源.

sel*_*bie 6

这应该适合你:

#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");
}
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,那么可能有两三个原因:

  1. 您使用程序代码创建了debug.txt,但文件仍然被锁定,因为您没有关闭文件句柄(例如,取决于您使用log_debug打开文件的方式:fclose(),CloseHandle(),close(),等...)或因为您打开了没有FILE_SHARE_READ标志的文件.

  2. 您实际上没有权限从c:\驱动器的根目录中读取.对于非管理员帐户,这通常是正确的.

  3. c:\ debug.txt实际上并不像你认为的那样存在.