Thread overview
A writefln issue or a Thread issue?
Sep 05, 2009
Sam Hu
Sep 05, 2009
Sam Hu
Sep 05, 2009
div0
Sep 07, 2009
Sam
September 05, 2009
Given below code:

module testWin32Process;

import win32.windows;//get from:http://www.dsource.org/projects/bindings/wiki/WindowsApi


import std.string;
import std.conv;
import std.stdio;
import core.stdc.stdlib;


string startupProcess()
{
	STARTUPINFO si;
	si.cb=si.sizeof;

	PROCESS_INFORMATION pi;
	char* szCommandLine=cast(char*)toStringz("cmd"/*"notepad testWin32Process.d"*/);
	si.dwFlags=STARTF_USESHOWWINDOW;
	si.wShowWindow=true;


	int bRet=CreateProcess(
		null,
		szCommandLine,
		null,
		null,
		false,
		CREATE_NEW_CONSOLE,
		null,
		null,
		&si,
		&pi);
	if(bRet)
	{
		CloseHandle(pi.hThread);

		return std.string.format("New process ID:%d\n"
			"Host process ID:%d\n",
			pi.dwProcessId,
			pi.dwThreadId);

	}
	else
	{

		return "Do not know what happend.";//just want to check
	}

}
int main(string[] args)
{
	writefln("%s\n",toStringz(startupProcess)); // prints blank!!!

	MessageBox(null,toStringz(startupProcess),toStringz("result?"),0);//prints contents as expected!!

	system("pause");
	return 0;
}

As commented,were I use writefln(...),it prints blank;were I use MessageBox,it prints well as expected:

New Process ID:2216
Host Process ID:2456

Could anybody here figure me out what the problem is?Thanks in advance.

Regards,
Sam
September 05, 2009
Fyi:
1.with DMD2032 under windows xp;
2.Tried printf,write,writeln,writef,writefln but all the same result:blank DOS console under current exe path;
3.In c/c++ it opens total 2 DOS windows which works properly: the main one and the one created by CREATE_NEW_CONSOLE ;but in D it opens total 3 DOS consoles,the main one and 2 blank consoles which were created by CREATE_NEW_CONSOLE,both under current exe path.
September 05, 2009
Sam Hu wrote:
> Fyi:
> 1.with DMD2032 under windows xp;
> 2.Tried printf,write,writeln,writef,writefln but all the same result:blank DOS console under current exe path;
> 3.In c/c++ it opens total 2 DOS windows which works properly: the main one and the one created by CREATE_NEW_CONSOLE ;but in D it opens total 3 DOS consoles,the main one and 2 blank consoles which were created by CREATE_NEW_CONSOLE,both under current exe path.


writefln("%s\n",toStringz(startupProcess)); // prints blank!!!

the toStringz in the writefln is wrong.
You've already returned a string from startupProcess.

The toStringz returns a char* which writefln is printing
as a pointer, so you are seeing something like '974f40' being printed
instead of the message you where expecting.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
September 07, 2009
Thank you for your help.It works now except one extra dos windows pop ups.