TProcess console output probs with wmic

BlitzMax Forums/BlitzMax Beginners Area/TProcess console output probs with wmic

kenshin(Posted 2009) [#1]
Okay, Ive got this code:
Process:TProcess = CreateProcess( "wmic /?" )

While Process.Status()

	If Process.err.ReadAvail() Or Process.pipe.ReadAvail() Then

		pipedata:String = Process.pipe.ReadLine().Trim()
		errordata:String = Process.err.ReadLine().Trim()

		If pipedata <> "" Then
		
			Print pipedata
		
		EndIf
		
		If errordata <> "" Then
		
			Print errordata
		
		EndIf
	
	EndIf
	
Wend

which runs fine on Win 7, but hangs on XP. However, if you type "wmic /?" at the XP command line then the correct data is output to the console. If I replace this line:
Process:TProcess = CreateProcess( "wmic /?" )

with
Process:TProcess = CreateProcess( "ipconfig /?" )

then it works fine on XP and 7, but this doesn't help me as I need to get the results of the "wmic /?" console output in XP.

I've tried a few different things, such as removing the error checking and trying the HIDECONSOLE flag in CreateProcess, but it always hangs on XP while working fine on Win7. I've tried a few of the variations of pipe handling examples that are around the forum. They work great, until I want the wmic output. Then it's the same old hang again, but only under XP. All work fine on Win7, and I'm assuming Vista.

I think that the console output for ipconfig and wmic differ in some way, but I'm not sure how, or why it hangs.

Has anyone got any ideas about this one?

I've spent all morning on this bug and pretty much tried everything I can think of to no avail.


kenshin(Posted 2009) [#2]
This similar PureBasic code works fine in both XP and Win7 and returns the wmic console output correctly.
  wmic = RunProgram( "wmic", "/?", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide )
  Output$ = ""

  If wmic  
 
    While ProgramRunning( wmic )
 
      Output$ + ReadProgramString( wmic ) + Chr( 13 )

    Wend
 
  EndIf
 
  MessageRequester( "Output", Output$ )

I just need to figure out how to do the same as this in BlitzMax.


kenshin(Posted 2009) [#3]
After countless hours of trying, I've come to the conclusion that it is not possible to get the WinXP wmic output using BlitzMax. I've used various methods including the system_ command, but the result is either a hang or an empty return. i.e. none of the data that wmic would normally be returning.

The only workaround that I've found is to have BlitzMax call the above PureBasic code as a dll, and then have the PureBasic dll return the wmic output back to BlitzMax in a string. It's messy, but it does work on both XP and Vista.

I was looking at tidying up the System Info section in my program by not using any external dll call's, but I can't find any solution that works on both operating systems using just BMax code. As for the reasons why the problem occurs, I've got no idea. It looks like the BMax/XP/wmic combination is not a happy one.


TaskMaster(Posted 2009) [#4]
Did you try supplying the whole path tot he file?


plash(Posted 2009) [#5]
Did you try supplying the whole path tot he file?
If the path is not valid, the Process variable would be null, and should throw an exception.


xlsior(Posted 2009) [#6]
One thing I noticed on my computer is that wmic /? can be really *slow*.
The first time I tried running it on the command line, it took a full ten seconds before I saw any output.

Could it perhaps be a time-out issue that you're running into?


plash(Posted 2009) [#7]
When I do 'wmic /?' in a command prompt, it outputs the information until it has used up a whole screen of space, and then says "Press any key to continue, or press the ESCAPE key to stop"; after pressing a key, it continues until it has shown all the commands.


kenshin(Posted 2009) [#8]
Thanks heaps for the ideas.

wmic /? can be really *slow*.


I thought about this as I have seen that some of the wmic alias can take a while for Windows to get, but then why does the PureBasic code work? I've waited for at least 10 minutes, but the BMax code seems to be caught in an endless loop.

Did you try supplying the whole path tot he file?


Good idea. I tried this:
Process:TProcess = CreateProcess( "c:\windows\system32\wbem\wmic /?" )

Same endless loop. I notice (fwiw) that the path to wmic is the same on XP and Win7.

I also thought it might be the version of XP I'm testing with. It is wifey's laptop running Chinese SP2. So, I fired up VirtualPC on my box and run English XP SP2 and it does the same hang on that. The rest of my program (which uses a combination of TV3D, wxMax, OpenAL, and Lua) works fine in the VirtualPC and on the laptop. That surprised me, basic 3D in vpc:)

The only things I can say for certain is that the wmic console output differs in some way between XP and Win7. Also the method used to execute a file differs between BMax and PureBasic.

It seems crazy that you can't get the stdout output from this one command line program when you can from all the others I've tried. I really don't understand why, as it's the first time I've ever seen that TProcess could not get the console output. Why it only hangs under XP is anyone's guess??? I have always been aware, even before I started this thread, that I can use the PureBasic DLL hack that I mentioned about in post 3. I'm just curious as hell as to the reasons why BMax can't do this by itself, and thought it was worth pointing out.

Thx for all the help, although I've tried so many things now I feel as though it's not possible.

Plash: In your sig, you say you're running xp sp3. Does the code in my first post work properly on your setup, or does it (gulp) hang?


Space_guy(Posted 2009) [#9]
Im having the exact same problem. Shame to see you didnt find a blitzmax solutions :(
I really really would love to have this working.


skidracer(Posted 2009) [#10]
Have you tried using ReadPipe instead of ReadLine?

wmic may use chr(10) as it's end of line where as freeprocess only recognizes chr(13)


Arabia(Posted 2009) [#11]
I'm still new to Blitzmax and have no idea what wmic actually does.

But anyway:

Your code works fine on my Vista - however last line output is:

NETPROTOCOL - Protocols (and their network characteristics) management.

Process Complete

If I run from the command prompt, I get a lot more info.

Seems like the process is only running for a certain time (does that make sense?)

If you change your code to:

errordata:String = Process.err.ReadLine().Trim()

Delay 500

If pipedata <> "" Then


you get absolutely nothing (on my PC anyway)

I take it that the XP machine is a whole lot slower than your Win 7 machine? Seems a lot like it is a time out issue of sorts :S

Can you maybe pipe the data to a file, and then read it for what you want?


kenshin(Posted 2009) [#12]
skidracer: I tried ReadPipe instead and read the data into a byte array. It works on Win7 but again it hangs on XP. Thx for the idea though.

arabia: I haven't been able to successfully pipe all the info to a file either using BMax under XP.

space_guy: If you like, I can post the dll which I have created as a workaround and also some code to demonstrate how to use it with BMax. It's the best solution I've been able to come up with. Works perfectly in WinXP and Win7 and gets all the system information. Just a pain in the butt having yet another dll in your folder.


Kurator(Posted 2009) [#13]
as an alternative you can redirect the output of wmic with the /OUTPUT: parameter either to the clipboard, stdout or to a file - even in a xml format.

as I investigated, I think this is an unicode issue of wmic


kenshin(Posted 2009) [#14]
I've tried that. It doesn't work on BMax in WinXP. Works fine in Win7 though.

If you have some BMax code which you know does work with redirecting wmic output on XP then please post it.


Kurator(Posted 2009) [#15]
i.e. wmic /OUTPUT:test.txt CPU LIST FULL - for writing infos about the cpu in a test.txt file

wmic /OUTPUT:CLIPBOARD CPU LIST FULL for writing it into the clipboard


kenshin(Posted 2009) [#16]
This code works fine on Win7 and the test.txt file is written, program ends etc.. On XP, the program hangs and no test.txt file is written:
Process:TProcess = CreateProcess( "wmic /OUTPUT:C:\test.txt CPU LIST FULL" )

While Process.Status()
	
Wend

I already tried the redirection thing before beginning this thread, but it still doesn't work on XP. That's what's so strange about it. All methods work fine in Win7, but none work at all in XP.


skidracer(Posted 2009) [#17]
Try adding a PollSystem in that loop.


kenshin(Posted 2009) [#18]
Just tried that, but it still hangs on XP. Same deal, no test.txt file. The bit in quotes works fine on the command line under XP.

It's really got me beat as to why it's not working.


Azathoth(Posted 2009) [#19]
Is wmic the only program that causes it to hang?


Kurator(Posted 2009) [#20]
You guessed right, it works on Vista 64bit....


kenshin(Posted 2009) [#21]
Azathoth: Yeah, other programs which return console output seem to work fine under xp with TProcess on BMax.

It's just this combination of xp, wmic, and TProcess. Bring all three of those together and that's where things will go pear-shaped, no matter what you try.

I don't know why. I asked mum and she didn't know why either:)