View Single Post
Old 08-28-2008, 04:15 PM   #171
igorsk
Wizard
igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.igorsk ought to be getting tired of karma fortunes by now.
 
Posts: 3,442
Karma: 300001
Join Date: Sep 2006
Location: Belgium
Device: PRS-500/505/700, Kindle, Cybook Gen3, Words Gear
Here's a little wrapper I had used to handle unicode commandline arguments.
Code:
import sys, os, ctypes, pprint
class Argvu:
    def __init__(self,CommandLine=None):
        if CommandLine==None:
            CommandLine=ctypes.windll.kernel32.GetCommandLineW()
        argv_count=ctypes.c_int()
        cmd_string=ctypes.c_wchar_p(CommandLine)
        array_memory_address=ctypes.windll.shell32.CommandLineToArgvW(cmd_string,ctypes.byref(argv_count))
        match_array_type=ctypes.c_wchar_p*argv_count.value
        self.argv=[arg for arg in match_array_type.from_address(array_memory_address)]

        #running from a script:
        #sys.argv[0] == script.py
        #our argv[0] == python.exe
        #running from an exe:
        #sys.argv[0] == prog.exe
        #our argv[0] == prog.exe
        #if we have the first case, we need to remove the interpreter from argv

        if not sys.argv[0].endswith(".exe") and self.argv[0].endswith(".exe"):
            self.argv=self.argv[1:]

        ctypes.windll.kernel32.LocalFree(array_memory_address)
    def __str__(self):
        return pprint.pformat(self.result)

argv = Argvu().argv

if __name__=='__main__':
    c2a=Argvu()
    print c2a
    print c2a.argv
igorsk is offline   Reply With Quote