Re: uptime for Win XP?

2004-12-12 Thread Andrey Ivanov
>> I believe that "uptime" works from the console, but don't have a machine
>> to check it with...

> Doesn't work for me, but if you have win32all installed, you can get it
> from Python:
> >>> import win32api
> >>> print "Uptime:", win32api.GetTickCount(), "Milliseconds"
> Uptime: 148699875 Milliseconds

MSDN  recommends  another approach. They say, that you should retrieve
the  value  of "System Up Time" counter from HKEY_PERFORMANCE_DATA. In
theory,  you  can do it without win32all, by using _winreg module. All
you  need  is  to  know  a  counter  index,  which can be fetched from
registry.  On  my  system "System Up Time" counter has index "674", so
Python code should look like this:

>>> import _winreg
>>> value, type_code = _winreg.QueryValueEx(_winreg.HKEY_PERFORMANCE_DATA, 
>>> "674")
>>> print "Uptime: %s miliseconds" % (value,)

But in current implementation of _winreg it doesn't work. I've checked
the  sources  and  found  that  current  implementation doesn't handle
ERROR_MORE_DATA,  which  prevents  it  from retrieving any performance
counters. I'm thinking of bug/patch submission.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: uptime for Win XP?

2004-12-13 Thread Andrey Ivanov
[Peter Hanson]
> The real solution, in spite of the dozen alternatives we've
> now produced, seems to be to use the win32pdh library
> to access the "System"-> "System Up Time" value.  It
> claims to return an 8-byte value, which likely doesn't
> wrap quite so soon.  (And yes, remarkably, with the advent
> of Windows XP Pro it is now possible to keep a Windows
> machine running for longer than 49 days, even if it's
> used as a development machine.  Well, for Python development,
> anyway. ;-)
> 
> For the life of me, however, I can't figure out how to do it.

Here's how. :-)

=
import win32pdh

query = win32pdh.OpenQuery()
counter = win32pdh.AddCounter(query, r"\System\System Up Time")

win32pdh.CollectQueryData(query)

(bizzare_int, val) = win32pdh.GetFormattedCounterValue(counter, \
win32pdh.PDH_FMT_LONG)

print "Uptime: %s secs" % (val,)
==

Writting this script was harder than I initially thought due to
a lack of documentation for win32all. And I still don't know what
that bizzare_int value stands for (an error/status code?).

Well, the registry interface to counters is definitely easier to use,
but not available to Python at the moment :-(

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: uptime for Win XP?

2004-12-14 Thread Andrey Ivanov
>> Writting this script was harder than I initially thought due to
>> a lack of documentation for win32all. And I still don't know what
>> that bizzare_int value stands for (an error/status code?).

[Fredrik Lundh]
> if I'm not mistaken, the corresponding Win32 function is called
> PdhGetFormattedCounterValue, which has two [in] parameters
> (counter handle, format code) and two [out] parameters (counter
> type, counter value)
> 
> so "counter type" is a good guess.
> 
>  

[David Bolen]
> The pywin32 documentation tends not to duplicate information already
> available via MSDN (whether in a local installation or at
> msdn.microsoft.com) on the underlying Win32 API, so when in doubt,
> that's where to look.  Then, the pywin32 documentation will sometimes
> qualify how the Python interface maps that function.
> 
> But in particular, a general rule (as has already been posted) is that
> any out parameters are aggregated along with the overall result code
> into a result tuple.
> 
> -- David

Thanks for reply! MSDN did contain the answer, but I didn't noticed
it.

--
Andrey

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: win32 process name

2004-12-22 Thread Andrey Ivanov
[phil]

> I need to know if a process is running.
> not just python.exe
> but python.exe myapp

> from win32all
> EnumProcesses gives me the pids, then
> OpenProcess(pid) gives me a handle.
> Then what?
> GetModuleFileNameEX?

It   won't   do   the   right  thing  for  you.  As  far  as  I  know,
GetModuleFileNameEx()  returns  the name of a particular DLL, but what
you need to know is a *commandline*. I think that this is not possible
at  all.  Microsoft's  examples  use named mutexes to test whether the
process  is  already running or not. It is quite easy. Here's a quick
example:

import sys
import win32event

STANDARD_ACCESS_READ = 131072

mutex_handle = None

try:
mutex_handle = win32event.OpenMutex(STANDARD_ACCESS_READ, False, "Test")
except:
pass

if mutex_handle:
sys.exit("Instance is already running")
else:
mutex_handle = win32event.CreateMutex(None, False, "Test")

try:
while 1:
pass
except:
pass


-- 
Andrey

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with os.listdir and delay with unreachable network drives on Windows

2004-12-23 Thread Andrey Ivanov
[Read Roberts]
> I wrote my own directory browser in order to get around a bug where 
> tkFileDialog.askdirectory()  can't handle non-ascii paths. However, I
> have a problem where I call os.listdir() on a  mapped network drive,
> e.g. os.listdir("Z:\\"), and if the network drive is unavailable, the
> UI hangs  until the OS returns with an exception because the network
> shared drive is unavailable.

>   I would like to work around this by simply not looking into  mapped
> drives that are not currently mounted.  Is there some way to check 
> the status of mapped drive to see if it is currently mounted, or a 
> better solution? ( I use the call win32api.GetLogicalDriveStrings() 
> to get the list of available drives).

> Also, is there any way to enumerate remote volumes that are mounted 
> by not mapped? I can't easily find this in the win32 stuff or via 
> googling. The win32net calls to enumerate servers and shares sound 
> likely, but don't show such volumes on my system, although I may not
> be using the right arguments.

> I have the current Windows binary install of Python 2.3.4 on my 
> Windows XP system.

Maybe  a  win32net.WNetGetUniversalName()  [expands drive to UNC name]
and   win32net.NetGetUseInfo()  [returns  various  info  on  UNC  name
including  status] will help you. I don't have a time to setup shares,
so I can't guarantee that this approach will work as you expect. First
function  might  raise an exception on disconnected devices, which you
will  need  to handle. You might also need win32file.GetDriveType() to
distinguish between remote and local drives.

-- 
Andrey

-- 
http://mail.python.org/mailman/listinfo/python-list