[Python-Dev] UUID module

Paul Moore p.f.moore at gmail.com
Sat Jun 10 17:19:04 CEST 2006


On 6/10/06, Paul Moore <p.f.moore at gmail.com> wrote:
> On 6/10/06, Ka-Ping Yee <python-dev at zesty.ca> wrote:
> > ...so i looked at PEAK's getnodeid48() routine and borrowed the
> > Win32 calls from there, with a comment giving attribution to PEAK.
>
> Instead of using pywin32, could you use ctypes, as that's part of core
> Python? It looks like the only Win32 API you use is CoCreateGUID, so
> wrapping it should be doable...

Here's some sample code (taken from Thomas Heller's comtypes module)

>>> from ctypes import oledll, Structure, byref
>>> from ctypes.wintypes import BYTE, WORD, DWORD
>>> class GUID(Structure):
...     _fields_ = [("Data1", DWORD),
...                 ("Data2", WORD),
...                 ("Data3", WORD),
...                 ("Data4", BYTE * 8)]
...
>>> guid = GUID()
>>> oledll.ole32.CoCreateGuid(byref(guid))
0
>>> guid
<__main__.GUID object at 0x00978EE0>
>>> guid.Data1
3391869098L
>>> guid.Data2
51115
>>> guid.Data3
20060
>>> guid.Data4
<__main__.c_byte_Array_8 object at 0x00978E40>
>>>

I'm not sure what the int(...[-13,-1], 16) does (as it stands, it
fails for me - I think you need to put str() round the CreateGuid
call) but I *think* it's the equivalent of guid.Data1 above.

So, you'd have:

def win32_getnode():
    """Get the hardware address on Windows using Win32 extensions."""
    from ctypes import oledll, Structure, byref
    from ctypes.wintypes import BYTE, WORD, DWORD
    class GUID(Structure):
        _fields_ = [("Data1", DWORD),
                    ("Data2", WORD),
                    ("Data3", WORD),
                    ("Data4", BYTE * 8)]

    guid = GUID()
    oledll.ole32.CoCreateGuid(byref(guid))
    return guid.Data1

Hope this is of use.
Paul.


More information about the Python-Dev mailing list