October 30th, 2008, 03:10 PM
|
#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. |
| |
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??? |
| |
October 30th, 2008, 10:38 PM
|
#3 (permalink)
| | Member
Join Date: Oct 2003
Posts: 255
|
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. |
| |
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 |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |