www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A writefln issue or a Thread issue?

reply Sam Hu <samhudotsamhu gmail.com> writes:
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
Sep 05 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
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.
Sep 05 2009
parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKokgaT9LetA9XoXwRArHUAJ4yTRoDqyrPlcCk5cdCZ7Yw2+iTSACg0u9w 3tpmYa8ftFEVErLMInIkb1k= =RgrL -----END PGP SIGNATURE-----
Sep 05 2009
parent Sam <samhudotsamhu gmail.com> writes:
Thank you for your help.It works now except one extra dos windows pop ups.
Sep 06 2009