home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2983
Discussions: 188,378, Posts: 2,243,448, Members: 232,606
Old October 30th, 2008, 03:10 PM   Digg it!   #1 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
win api using vb.net help

Hey All,

I have used other win API's before. I am having a bit of trouble with this one. I am trying to create a scheduled task through vb.net. Yes I know I can do it manually or through a bat file. However I would like to create a backup option in an application I have. I would like to try to NOT shell out to a bat file.

Here is what I have: (its in a class so i am just copying the parts that run)

Code:
   ' Schedule constants
    Const JOB_RUN_PERIODICALLY As Byte = &H1
    Const JOB_NONINTERACTIVE As Byte = &H10

 Private Structure ScheduledParameters
        Dim JobTime As Long
        Dim DaysOfMonth As Long
        Dim DaysOfWeek As Byte
        Dim Flags As Byte
        Dim dummy As Integer
        Dim Command As String
    End Structure

 Private Function SetParameters() As ScheduledParameters
        Dim stcReturnValue As ScheduledParameters

        With stcReturnValue
            ' Change the time to one used by the api
            .JobTime = 36000000
            .DaysOfMonth = 0

            'Sunday is 0
            'Monday is 1
            'Tuesday is 2, etc.
            .DaysOfWeek = 0

            .Flags = JOB_NONINTERACTIVE
            .Flags = JOB_RUN_PERIODICALLY

            ' Set the command to run
            .Command = "notepad.exe" 'StrConv(Text4.Text, vbUnicode)
        End With

        Return stcReturnValue
    End Function


    Public Sub AddScheduledTask()
        Dim iResultCode As Int64
        Dim strComputerName As String
        Dim stcScheduledTask As ScheduledParameters
        Dim iJobID As Int64

        'Convert the computer name to unicode
        strComputerName = My.Computer.Name

        'Setup the tasks parameters
        stcScheduledTask = SetParameters()

        'Schedule the task
        iResultCode = NetScheduleJobAdd(strComputerName, stcScheduledTask, iJobID)

        'Check if the task was scheduled
        If iResultCode = 0 Then 'Successful
            MsgBox("Task" & iJobID & " has been scheduled.")
        End If
    End Sub
iResultCode = 619526423740154969

which is not a valid system error message. I believe the issue is caused because I am not setting the user nname and pw who the schedule task is suppose to run under.

I need help with someone that has a little more api experience.

Anyone come across this???

Or know what API i need to call before I try to create the scheduled job?

Thanks in advance for your help.

amtrac24 is offline   Reply With Quote
Old October 30th, 2008, 03:59 PM     #2 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
i think the problem is that i need to pass my computer name in unicode...same code worked in vb 6 except:

strComputerName = StrConv("Programmingjd", vbUnicode)

anyone know how to do this in vb.net???

amtrac24 is offline   Reply With Quote
Old October 30th, 2008, 10:38 PM     #3 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
i got it working

i got it working...I had to make some other changes.

I have the code at work and will post it tomorrow for anyone that wants to know.

amtrac24 is offline   Reply With Quote
Old October 31st, 2008, 10:37 AM     #4 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
Code:
  Private Declare Function NetScheduleJobAdd Lib "netapi32.dll" _
          (<MarshalAs(UnmanagedType.LPWStr)> ByVal Servername As String, ByVal lpBuffer As IntPtr, ByRef JobId As Int32) As Int32

 Private Structure ScheduledParameters
        Dim JobTime As Int32
        Dim DaysOfMonth As Int32
        Dim DaysOfWeek As Byte
        Dim Flags As Byte
        <MarshalAs(UnmanagedType.LPWStr)> Dim Command As String
    End Structure



    Private Function SetParameters() As ScheduledParameters
        Dim stcReturnValue As ScheduledParameters

        With stcReturnValue
            'Change the time to one used by the api
            .JobTime = 0 'milliseconds from midnight

            '0 is the first day of the month, 1 is the second day of the month, etc.
            For i As Int32 = 0 To 30
                .DaysOfMonth = CType(.DaysOfMonth + 2 ^ i, Int32)
            Next

            'Monday is 0
            'Tuesday is 1, etc.
            For i As Int32 = 0 To 6
                .DaysOfWeek = CType(.DaysOfWeek + 2 ^ i, Byte)
            Next

            '.Flags = JOB_NONINTERACTIVE
            .Flags = JOB_RUN_PERIODICALLY

            'Set the command to run
            .Command = "notepad.exe"
        End With

        Return stcReturnValue
    End Function



    Public Function AddScheduledTask() As Boolean
        Dim iResultCode As Int32
        Dim strComputerName As String
        Dim stcScheduledTask As ScheduledParameters
        Dim iJobID As Int32 = 0
        Dim ptrParams As IntPtr

        Try
            strComputerName = My.Computer.Name

            'Setup the tasks parameters
            stcScheduledTask = SetParameters()

            'Allocate The Pointer
            ptrParams = Marshal.AllocHGlobal(Marshal.SizeOf(stcScheduledTask))
            Marshal.StructureToPtr(stcScheduledTask, ptrParams, False)

            'Schedule the task
            iResultCode = NetScheduleJobAdd(strComputerName, ptrParams, iJobID)

            'Check if the task was scheduled
            If iResultCode = 0 Then 'Successful
                Return True
            Else
                Return False
            End If

        Finally
            Marshal.FreeHGlobal(ptrParams)
        End Try

    End Function
amtrac24 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need program for disconnecting from 'net on win 2000 server davidamarkley Applications and Operating Systems 2 July 15th, 2003 04:45 PM
any good API references???? Jüš† ä gü¥ Webmastering and Programming 4 April 13th, 2003 08:05 AM
how about this... API is to Java as ________ is to C... zskillz Webmastering and Programming 14 March 26th, 2003 02:43 AM
where can i download a copy of the java API? zskillz Webmastering and Programming 9 February 21st, 2002 04:01 PM
Win XP Pro with Windows Messenger a Repalcement for Net Meeting? surreal Applications and Operating Systems 1 January 28th, 2002 01:53 PM

Most Active Discussions
Is It Just Me? (2882)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
dual monitors wont boot (0)
Folderchat Weekday thread (439)
New Build ( Finally ) (0)
MSN Hotmail Down??? (7)
Help with an Ati Radeon HD 4850.. (23)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Building first computer, will t.. (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:09 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28