Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

windows - What is the correct way to use ShellExecute() in C to open a .txt

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

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"

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

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
",final);

  log_debug(final);

  return(0);
}

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

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>
", ctime(&now),test_variable);
    //TODO: Allow more than one variable

    fclose(debug);

    return(0);
}

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

Full Source Here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work for you:

#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. 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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...