d0vgan / nppexec

NppExec (plugin for Notepad++)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Win32 'WriteConsole' doesn't output any text

chris03-dev opened this issue · comments

Notepad++: v8.1.4 (64 bit)
NppExec: v0.7
Compiler: gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

Hello! I was trying out some Win32 functions in C, when I found out that WriteConsole doesn't work on the "NppExec Console". However, both Command Line and Powershell show the output from the executable.

A fix may not be totally necessary, since a workaround npp_exec $(NAME_PART).exe is present and could be adjusted to my preference, but it would be nice if this was acknowledged, and I would like to learn your perspective on this.

My snippet of C code:

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
{
	// Get command line arguments
	char *args = GetCommandLine();
	
	// Get output handle and other stuff
	HANDLE HStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	long unsigned int *IndexStdOut = 0;
	
	// Output 'hello world' string
	if (HStdOut != NULL && HStdOut != INVALID_HANDLE_VALUE)
	{
		WriteConsole(HStdOut, args,          strlen(args),          IndexStdOut, NULL);
		WriteConsole(HStdOut, "\n",          1,                     IndexStdOut, NULL);
		WriteConsole(HStdOut, "Hello World", strlen("Hello World"), IndexStdOut, NULL);
	}
	
    return 0;
}

My NppExec script:

npp_save
cd $(CURRENT_DIRECTORY)
gcc -Wall -O3 -std=c99 -s "$(FILE_NAME)" -o "$(NAME_PART).exe"
$(NAME_PART).exe

Screenshots:
image
image

You need to call FlushFileBuffers(HStdOut) after the last WriteConsole.
See more technical details here: #38

Tried that just now, but the NppExec console output still doesn't work. Other consoles work as usual.
I just put the FlushFileBuffers(HStdOut) directly after WriteConsole(HStdOut, "Hello World", strlen("Hello World"), IndexStdOut, NULL).

Also, the issue that you have provided is similar, but not identical to the issue I have. Their program produces output on exit, while my program doesn't produce any output at all.

When I compile this code into an executable:

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
{
	// Get command line arguments
	char *args = GetCommandLine();
	
	// Get output handle and other stuff
	HANDLE HStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD dwCharsWritten = 0;
	
	// Output 'hello world' string
	if (HStdOut != NULL && HStdOut != INVALID_HANDLE_VALUE)
	{
		WriteConsole(HStdOut, args,          strlen(args),          &dwCharsWritten, NULL);
		WriteConsole(HStdOut, "\n",          1,                     &dwCharsWritten, NULL);
		WriteConsole(HStdOut, "Hello World", strlen("Hello World"), &dwCharsWritten, NULL);
        FlushFileBuffers(HStdOut);
	}
    else
    {
        MessageBox(NULL, "HStdOut is invalid!", "WinMain", MB_OK);
    }
	
    return 0;
}

and run this executable from command prompt, I am getting a message box "HStdOut is invalid!".
I believe it is because the entry point WinMain creates a GUI application (not a console application!), and the attempt to GetStdHandle in a GUI application can not return anything useful because a GUI application does not have any console window.
To fix it, you may call AllocConsole() at the beginning of your program and then FreeConsole() at the end of your program. This, however, will always create a new console window even when your program is started from some existing console window such as command prompt.
The proper way to reuse the existing console window (e.g. command prompt's console window) is to replace int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) with int main() or with int main(int argc, char* argv[]).