Results 1 to 8 of 8

Thread: VB6 - Use Vista+ Task Scheduler 2.0 API

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    VB6 - Use Vista+ Task Scheduler 2.0 API

    One of the things that's always been a little clunky is using the Task Scheduler from code. Windows Vista made many changes to Task Scheduler, and also introduced a new 2.0 API with a COM interface quite usable from Visual Basic.

    You don't always need something like this but it's good to know it has gotten easier. One nice thing about it is that you can now defer long-winded processes to run in batch mode with lowered priority. You can even have Task Scheduler wake the machine up from sleep or hibernation to run your scheduled task.

    Because Task Scheduler opens a few vulnerabilities due to its nature (as do 3rd party "chron" type tools, perhaps even worse) it was changed during the life of Windows 2003 Server so that only an Administrator (or Backup Operator) can schedule tasks anymore. Interestingly, elevation isn't required unless you want the scheduled task to run elevated. The scheduling User simply needs to be a member of one of those two Groups. This is yet another reason why you should normally work as a Standard User, even with UAC enabled.


    Some Issues

    Now an Admininstrator can schedule tasks for other Users of course, by providing the User/PW. In Vista or later Windows versions the password will be stored in encrypted form, making things a bit more secure.

    But if a User is to have a scheduled task run while not logged on, that user needs the Group Policy setting "Log on as batch job."

    This applies whether the target User is an admin or not.

    In the "Home" editions of Windows there isn't an easy way to set this GPO. One tool I find handy is the free polsedit, which comes in both 32-bit and 64-bit builds. This can be handy even for higher-level editions of Vista, because you may be missing the Group Policy Management Console:
    If you are running Windows Vista with no service packs installed, the GPMC is included with the operating system. Upgrading to Windows Vista with Service Pack 1 removes the GPMC. To reinstall the GPMC, install the Remote Server Administration Tools (RSAT) for Windows Vista with SP1.

    The Demo

    I am including a small demonstration of using the COM interface to Task Scheduler that is new with the 2.0 API in Vista or later.

    The demo consists of two programs:
    • CreateDB - A VB6 program written to run in fully unattended mode, our batch worker program.
    • SchedCDB - A VB6 program used to set run parameters for CreateDB and schedule the task to run at a future time. This is a more typical VB6 GUI program.

    CreateDB accepts a command line with two parameters: the number of records to populate a sample database table with, and a log file prefix to use.

    CreateDB creates an empty MDB and MDW, adds a table to the MDB, then adds the supplied number of random records to it. All logging (including failures) should be recorded in the folder logs as a file named <logprefix>.log, in standard VB6 logfile format.

    SchedCDB presents a window with fields for the number of records, the logfile prefix, whether to run as the current user, user and password if not, whether to wake the machine, and when to start. It will create the logs folder if not present.

    Once you click the Schedule Task button an "Are you sure?" informational prompt occurs and if approved the task is scheduled. Then you can close SchedCDB.


    Closing Comments

    It's all pretty easy really.

    The new API opens up a lot of capabilities and you can get pretty exotic without the old rat's nest of 1.0 API calls required. Task Scheduler itself offers a few new features we didn't have before Vista.

    Note that if scheduling CreateDB to run under another user, that user will need read/write access to the program (and logs) folder!

    The attachment contains a ReadMe that covers a lot of ground, and suggests other things you might explore beyond what this simple demo does.

    It's great for overnight crunching operations like media transcoding, large data file transformation, or database maintenance.
    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by dilettante; Dec 31st, 2010 at 03:11 PM. Reason: reposted attachment, added error checks to catch bad passwords, etc.

  2. #2
    New Member
    Join Date
    Feb 2011
    Posts
    2

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    I was looking at your example and it appears to be the ONLY example I can find on using the Task Scheduler 2.0 api from VB6...so first of all "Thank you very much for taking the time to do this."

    I'm working on a project that needs to be able to run in a mixed XP/Vista+ environment. I am having trouble modifying your example to use late binding on the tasksched.dll. As I am using my project on XP and below late binding is required as I only conditionally want to try creating the scheduled event Vista+ systems.

    You obviously have a handle on DLL's in VB6, would you mind explaining the way to late bind the DLL?

    Thank you.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    I'm at a loss here because the example given actually does use late binding. It is forced to because VB6 does not support some of the "Automation Types" used in several method calls of TaskScheduler library objects.

    It's true, the SchedCDB example program does reference the library but it uses no early binding. About the only value the program gets from the reference is access to the Enums in the library, which for some very odd reason are marked "hidden." You could remove the reference from SchedCDB, then add these definitions in the Schedule_CreateDB subroutine:
    Code:
        Const TASK_LOGON_PASSWORD = 1
        Const TASK_RUNLEVEL_HIGHEST = 1
        Const TASK_TRIGGER_TIME = 1
        Const TASK_ACTION_EXEC = 0
        Const TASK_CREATE_OR_UPDATE = 6
        Const TASK_LOGON_S4U = 2
    This would even let you compile the program under Windows XP, however it does nothing to let the program run under WinXP.

    Windows XP and 2000 do not have this ability.


    The Task Scheduling subsystem was radically changed in Windows 6.0 (Vista), and one of these changes was the introduction of the ActiveX ("scripting") interfaces that my example uses. There is also a new API for use from C++ programs.

    Before Win 6.x there were actually several scheduling systems for different purposes, and the rewrite (Task Scheduler 2.0) unified these into one scheduling engine. Task Scheduler 2.0 will still accept 1.0 API calls made by legacy programs, but there is no forward compatibility path in a Scheduler 1.0 system to let it use Scheduler 2.0 objects at all.

    Prior to Vista you have Mstask.dll, which does not contain the Automation interfaces that I am using here. Vista (and later) have tasksched.dll which is a superset of this. On Vista I believe the Mstask.dll only contains stubs that map 1.0 calls to the new 2.0 tasksched.dll for action.


    Adding those Const values will let you compile on Windows XP. But the program cannot schedule anything except on Vista (or Server 2008, Windows 7, etc.). There is no early binding being used at all.

    To let a program run in XP that can schedule on Vista, you simply need to check the OS Major Version value and if less than 6 then don't try to create and use the Task Scheduler 2.0 objects.

    Having a reference to a library is not what make early binding, it just provides information for use at compile time. Note that the program looks like:
    Code:
    Public Sub Schedule_CreateDB()
        Dim strId As String
        Dim TaskDef As Object 'TaskScheduler.ITaskDefinition
        Const THREAD_PRIORITY_BELOW_NORMAL As Long = 7
        
        strId = Replace$(txtLogFile.Text, ".", "-") 'Create an Id value, no periods here!
        With CreateObject("Schedule.Service") 'New TaskScheduler.TaskScheduler
            .Connect
        :
        :
        :
    No early binding here at all, only late binding.

    People really do need to get off Windows XP, it isn't even a good platform for compiling VB6 programs any more and it's lousy for testing any new Windows features. Anybody running VB6 under XP Mode in Windows 7 needs to ask themselves what they're doing and why. That's just a mess.

    It may be why you see so few examples of using newer Windows features (like Scheduler 2.0) in VB6 programs though. Lots of cave men out there I guess!


    So I hope this helps. Or perhaps I completely misinterpreted your question?
    Last edited by dilettante; Feb 28th, 2011 at 02:39 AM.

  4. #4
    New Member
    Join Date
    Feb 2011
    Posts
    2

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    Yes, I apologize. I saw the late binding reference in the comments, but thought that was being used for a different object. It was the constant values that were causing me the real issue and I was not aware. I'm going to try to distill your example down to a class or module that exports only the Task Scheduler 2.0 API stuff. I'll post it here if I get it done this week, unless you are planning to do it (don't want to step on any toes). Thanks again for doing this, and for your reply.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    Well considering the large set of options available I'm not convinced that wrapping the Task Scheduler 2.0 classes with another class buys you much. 2/3rds or so of the code in my example is either defining and scheduling a very application-specific task or else some minor help functions for dealing with XML time formats. Another application would probably have very different "inputs" both in types and values.

    Can't hurt to post what you come up with though. Might be worth starting a second thread here in case others want to correspond about it with questions, suggestions, etc. You could always reply here with a link to your new thread to help people find it if they came here.

  6. #6
    Lively Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    72

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    Hello. I need to schedule a task in a vb6 application. I found this post and Dilettante's solution. I ask if it is possible to modify it to use it on windows 10. Or what if there is an alternative? Thanks

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    Task Scheduler 2.0 is still the current version, so it should work on Windows 10 without issue.

    My type library has all of the Task Scheduler interfaces defined, so you could more easily modify things with VB able to pop up all the functions and their arguments, like dilettante's project has
    Dim TaskDef As Object 'TaskScheduler.ITaskDefinition
    CreateObject("Schedule.Service") 'New TaskScheduler.TaskScheduler


    You could switch that to Dim TaskDef As ITaskDefinition and Dim TaskSched As New TaskScheduler

    Easier to modify with early binding.

  8. #8
    Lively Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    72

    Re: VB6 - Use Vista+ Task Scheduler 2.0 API

    Hello Fafalone. Thanks for your answer. I had found your example code. It is very interesting. But I don't have much experience using interfaces. I'll try. Maybe if I'm in trouble, I'll ask you for help.
    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width