Coder Social home page Coder Social logo

Comments (3)

d0vgan avatar d0vgan commented on June 8, 2024

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

from nppexec.

chris03-dev avatar chris03-dev commented on June 8, 2024

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.

from nppexec.

d0vgan avatar d0vgan commented on June 8, 2024

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[]).

from nppexec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.