什么是使用的ShellExecute(c)中打开一个.txt的正确方法 [英] What is the correct way to use ShellExecute() in C to open a .txt

查看:342
本文介绍了什么是使用的ShellExecute(c)中打开一个.txt的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Alright so i need to open a .txt file that will be created in the same file as the program.

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

I would like to use ShellExecute(); to do this and i have done a lot of research on it and i just cant seem to get the syntax correct mostly because i dont know what to do with the parameter "HWND"

我看了这里的答案,得到了除要放什么东西在HWND的所有信息

I looked here for the answers and got all the info except what to put in HWND

这里是我需要使用code:

Here is how i need the code used:

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

您的帮助感谢提前问,如果你不能确定什么,我说什么! :)

Thanks advance for the help ask if you are unsure what i am talking about! :)

这是我用来测试功能的程序:

This is the program that i use to test the function:

  #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的功能

view_debug(); is the function that includes 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();

This is 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的不工作必须手动打开。

The file is created by the function log_debug(); and it does work but must be opened manually because ShellExecute does not work.

全部来源这里

推荐答案

这应该为你工作:

#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");
}

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

If it doesn't work, then there are likely two or three reasons:


  1. 您与您的程序code创建DEBUG.TXT,但文件仍处于锁定状态,因为你没有关闭该文件句柄(例如:这取决于你如何使用打开LOG_DEBUG文件:FCLOSE() ,CloseHandle的(),close()方法,等...),或者因为你打开的文件没有FILE_SHARE_READ标志。

  1. You created the debug.txt with your program code, but the file is still locked because you didn't close the file handle (e.g. depending on how you opened the file with log_debug: fclose(), CloseHandle(), close(), etc...) or because you opened the file without the FILE_SHARE_READ flag.

您实际上并没有权限从C的根目录为:\\驱动器。这通常为true非管理员帐户。

You don't actually have permissions to read from the root of the c:\ drive. Which is usually true for non-admin accounts.

C:\\ DEBUG.TXT实际上并不存在像你想象的那样

c:\debug.txt doesn't actually exist like you think it does.

这篇关于什么是使用的ShellExecute(c)中打开一个.txt的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆