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
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);
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:
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.
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 doesn't actually exist like you think it does.
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.
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.