Creating a window from PyHandle? Possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fadly Tabrani

    #1

    Creating a window from PyHandle? Possible?

    Hi guys,

    This is the first time I'm posting to the newsgroup, hope you can help out.

    I'm writing a module to interface with the task scheduler in windows
    using PyWin32. Everything has been great so far but I'm stuck on
    displaying the propertys page of a task. Here's the code:

    import pythoncom
    import win32api
    from win32com.tasksc heduler import taskscheduler
    import win32ui

    task_scheduler = pythoncom.CoCre ateInstance\
    (taskscheduler. CLSID_CTaskSche duler,
    None, pythoncom.CLSCT X_INPROC_SERVER ,
    taskscheduler.I ID_ITaskSchedul er)

    def show_schedule_p age(task_name):

    TASKPAGE_TASK = 0,
    TASKPAGE_SCHEDU LE = 1,
    TASKPAGE_SETTIN GS = 2

    try:
    task = task_scheduler. Activate(task_n ame)
    except pythoncom.com_e rror, e:
    print 'Task name not found...'
    return

    # ** This returns a PyHandle
    handle = task.QueryInter face(taskschedu ler.IID_IProvid eTaskPage).\
    GetPage(1, True)

    # ** Can't create a window with it using win32ui, returns error
    try:
    win32ui.CreateW indowFromHandle (handle)
    except win32ui.error, e:
    print e

    # ** Can't add it to a property sheet, return an error
    property_sheet = win32ui.CreateP ropertySheet('T ask Schedule')
    try:
    property_sheet. AddPage(handle)
    except (win32ui.error, TypeError), e:
    print e

    show_schedule_p age('task1')

    Any ideas on how to do it? The task schduler api reference is at
    http://msdn.microsoft.com/library/de...start_page.asp

    I'm pretty new at this, is there anything that I've missed out?

    Cheers,
    Fadly Tabrani
    python loves me loves python
  • Roger Upole

    #2
    Re: Creating a window from PyHandle? Possible?

    The basic problem is that the handle returned from GetPage is not an MFC
    object, and win32ui is expecting an MFC property sheet page, not a raw
    handle.
    I played around with this a while back, and managed to display the property
    page
    by sending the window message PSM_ADDPAGE to a PyCPropertyShee t.
    However, you can't use SendMessage until you've called CreateWindow for the
    property sheet, and you can't call CreateWindow until you've added a
    property
    page. (catch-22!) I got around this by adding a dummy page to the property
    sheet,
    calling CreateWindow & SendMessage to add the task page, and then removing
    the stand-in. But since the task page isn't MFC, after I did that any
    attempt to call methods on the property sheet caused an access violation.

    Roger

    import win32api,win32c on, win32ui, pythoncom
    import traceback, os
    from win32com.tasksc heduler import taskscheduler
    PSM_ADDPAGE = win32con.WM_USE R + 103
    PSM_REMOVEPAGE = win32con.WM_USE R + 102
    task_name='test _addtask_4.job'

    ts=pythoncom.Co CreateInstance( taskscheduler.C LSID_CTaskSched uler,None,

    pythoncom.CLSCT X_INPROC_SERVER ,taskscheduler. IID_ITaskSchedu ler)

    t=ts.Activate(t ask_name)
    iptp=t.QueryInt erface(tasksche duler.IID_IProv ideTaskPage)
    tp=iptp.GetPage (taskscheduler. TASKPAGE_TASK,F alse)

    fname=os.path.j oin(os.environ['SYSTEMROOT'],'system32','ms task.dll')
    dll=win32ui.Loa dLibrary(fname)
    try:
    dll.AttachToMFC ()
    except:
    traceback.print _exc()

    ps=win32ui.Crea tePropertySheet ('eh ?????')

    ## property sheet won't let you CreateWindow unless there's already a
    property page attached,
    ## and you can't do a SendMessage until you've created a window !!!!
    pptask=win32ui. CreatePropertyP age(401) ## 401 is the task page template from
    mstask.dll, identified using EnumResources
    ps.AddPage(ppta sk)
    ps.CreateWindow ()
    ps.SendMessage( PSM_ADDPAGE,0,t p)
    ## get rid of the dummy
    ps.RemovePage(0 )



    "Fadly Tabrani" <no@spam.net> wrote in message
    news:ckaq81$50h $1@mawar.singne t.com.sg...[color=blue]
    > Hi guys,
    >
    > This is the first time I'm posting to the newsgroup, hope you can help[/color]
    out.[color=blue]
    >
    > I'm writing a module to interface with the task scheduler in windows
    > using PyWin32. Everything has been great so far but I'm stuck on
    > displaying the propertys page of a task. Here's the code:
    >
    > import pythoncom
    > import win32api
    > from win32com.tasksc heduler import taskscheduler
    > import win32ui
    >
    > task_scheduler = pythoncom.CoCre ateInstance\
    > (taskscheduler. CLSID_CTaskSche duler,
    > None, pythoncom.CLSCT X_INPROC_SERVER ,
    > taskscheduler.I ID_ITaskSchedul er)
    >
    > def show_schedule_p age(task_name):
    >
    > TASKPAGE_TASK = 0,
    > TASKPAGE_SCHEDU LE = 1,
    > TASKPAGE_SETTIN GS = 2
    >
    > try:
    > task = task_scheduler. Activate(task_n ame)
    > except pythoncom.com_e rror, e:
    > print 'Task name not found...'
    > return
    >
    > # ** This returns a PyHandle
    > handle =[/color]
    task.QueryInter face(taskschedu ler.IID_IProvid eTaskPage).\[color=blue]
    > GetPage(1, True)
    >
    > # ** Can't create a window with it using win32ui, returns error
    > try:
    > win32ui.CreateW indowFromHandle (handle)
    > except win32ui.error, e:
    > print e
    >
    > # ** Can't add it to a property sheet, return an error
    > property_sheet = win32ui.CreateP ropertySheet('T ask Schedule')
    > try:
    > property_sheet. AddPage(handle)
    > except (win32ui.error, TypeError), e:
    > print e
    >
    > show_schedule_p age('task1')
    >
    > Any ideas on how to do it? The task schduler api reference is at
    >[/color]
    http://msdn.microsoft.com/library/de...start_page.asp[color=blue]
    >
    > I'm pretty new at this, is there anything that I've missed out?
    >
    > Cheers,
    > Fadly Tabrani
    > python loves me loves python[/color]


    Comment

    Working...