3

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\n",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>\n", 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.

Bevilacqua
  • 465
  • 3
  • 8
  • 19
  • 1
    That loadlibrary call in view_debug superfluous and not needed since you are already linking with shell32.lib. – selbie Jun 13 '12 at 07:04

4 Answers4

8

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.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Interestingly enough, your code works even without the #include . Is it there for a reason? – Dean P Aug 21 '18 at 21:58
  • `` includes `` in the absence of `WIN32_LEAN_AND_MEAN`. That might not have been the case in earlier versions of the Windows SDK. Or the default Visual Studio projects back then may have defined the lean_and_mean macro in stdafx.h. I can't remember. – selbie Aug 22 '18 at 00:15
1

As stated in the page that you linked to:

This value can be NULL if the operation is not associated with a window.

The reason you might want to specify a parent window is that if your application is displaying a window, you might want your window to be the parent of any message boxes that the ShellExecute API might display. If you say NULL then ShellExecute will display its message boxes as top level windows, so the user might wonder what application is displaying the box.

Windows programmer
  • 7,871
  • 1
  • 22
  • 23
1

Usually NULL suffices. From ShellExecute documentation:

hwnd [in, optional]

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.
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • Check the return value. If you already have, post the error code if you're unable to understand what is going wrong). Post the whole code. Make sure the file exists. Did you link in `shell32.dll` or are you using `LoadLibrary( "shell32.dll" )`? Also, you will probably need to initialize COM via `CoInitialize`. – dirkgently Jun 13 '12 at 02:54
  • Check the original post for source... i added loadlibrary but i am unfamiliar with coinitialize. – Bevilacqua Jun 13 '12 at 03:08
  • @Bevilacqua: Did you check if the return value is > 32? Also, call `CoInitialize( NULL );` before the `ShellExecute` call and then `CoUninitialize()`; – dirkgently Jun 13 '12 at 04:20
  • when i attempt to call CoInitialize(NULL); i get the error undefined reference to `CoInitialize@4'| – Bevilacqua Jun 13 '12 at 04:41
  • You need to add `Ole32.lib` to your project settings > linker > input > additional dependencies. You can add `shell32.dll` there as well and get rid of the `LoadLibrary` call too. – dirkgently Jun 13 '12 at 04:48
  • alright it works when i compile the library but when i try it on my test program i get error undefined reference to `CoInitialize@4' and error undefined reference to `CoInitialize@0' – Bevilacqua Jun 13 '12 at 05:33
  • You need to add the `.lib` files to your test program project property settings. – dirkgently Jun 13 '12 at 05:51
  • already done i am keeping it LoadLibrary considering it will be reused (its a library) – Bevilacqua Jun 13 '12 at 05:58
1

The ShellExecute function syntax on MSDN:

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

You can try like this. You could open "notepad" with parameter of your text file path ("c:\\debug.txt"):

ShellExecute(0,"open", "notepad", "c:\\debug.txt", NULL, SW_SHOW);
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Kebron
  • 23
  • 4
  • I had a little trouble understanding your answer. Is this closer to what you meant? If I got it wrong, please [edit] your post to make improvements or unroll the changes completely from the [revision history](http://stackoverflow.com/posts/42945593/revisions). – Nisse Engström Mar 22 '17 at 09:50