Monday, October 26, 2009

Interoperating with Unmanaged Code Example-Create a Wrapper Class

To create a wrapper class, declare DLL functions within a class. Then define a static
method for each DLL function you want to call.

For example:


using System;
using System.Runtime.InteropServices;
class Win32MessageBox
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text,
String caption, uint type);
public static void Show(string message, string caption)
{
MessageBox(new IntPtr(0), message, caption, 0);
}
}

class Program
{
static void Main(string[] args)
{
Win32MessageBox.Show("Hello, world!", "My box");
}
}

Interoperating with Unmanaged Code Example-Using Delegate to Call Back Functions in Managed Code

We know that to call an unmanaged function in a COM object, We need to do:
1. Add DllImportAttribute
2. Create a prototype method for the COM function

like:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, String text,
String caption, uint type);


But when the unmanaged function has long pointer parameter?

Here is an example easy to be found in msdn:

using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp {

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}


The COM Fuction BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam), notice that if the argument is begin with lp-(long pointer)prefix, this is a function require a call back in COM.

To call a function that requires a callback, follow these steps:
1. Create a method to handle the callback.
2. Create a delegate for the method.
3. Create a prototype for the function, specifying the delegate for the callback
argument.
4. Call the function.

See the references:
http://msdn.microsoft.com/en-us/library/d186xcf0(VS.71).aspx
http://www.pinvoke.net/default.aspx/user32.EnumWindows

Thursday, October 22, 2009

schtasks commadline

Parameter List:
/Create : Creates a new scheduled task.

/Delete : Deletes the scheduled task(s).

/Query : Displays all scheduled tasks.

/Change : Changes the properties of scheduled task.

/Run : Runs the scheduled task immediately.

/End : Stops the currently running scheduled task.

/? Displays this help/usage.

Examples:
SCHTASKS
SCHTASKS /?
SCHTASKS /Run /?
SCHTASKS /End /?
SCHTASKS /Create /?
SCHTASKS /Delete /?
SCHTASKS /Query /?
SCHTASKS /Change /?




SCHTASKS /Create /?

SCHTASKS /Create [/S system [/U username [/P password]]]
[/RU username [/RP password]] /SC schedule [/MO modifier] [/D day]
[/I idletime] /TN taskname /TR taskrun [/ST starttime] [/M months]
[/SD startdate] [/ED enddate]

Description:
Enables an administrator to create scheduled tasks on a local or
remote systems.

Parameter List:
/S system Specifies the remote system to
connect to. If omitted the system
parameter defaults to the local
system.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/RU username Specifies the user account (user
context) under which the task runs.
For the system account, valid values
are "", "NT AUTHORITY\SYSTEM" or
"SYSTEM".

/RP password Specifies the password for the user.
To prompt for the password, the value
must be either "*" or none.
Password will not effect for the
system account.

/SC schedule Specifies the schedule frequency.
Valid schedule types: MINUTE, HOURLY,
DAILY, WEEKLY, MONTHLY, ONCE,
ONSTART, ONLOGON, ONIDLE.

/MO modifier Refines the schedule type to allow
finer control over schedule
recurrence. Valid values are listed
in the "Modifiers" section below.

/D days Specifies the day of the week to run
the task. Valid values: MON, TUE,
WED, THU, FRI, SAT, SUN and for
MONTHLY schedules 1 - 31 (days of the
month).

/M months Specifies month(s) of the year.
Defaults to the first day of the
month. Valid values: JAN, FEB, MAR,
APR, MAY, JUN, JUL, AUG, SEP, OCT,
NOV, DEC.

/I idletime Specifies the amount of idle time to
wait before running a scheduled
ONIDLE task.
Valid range: 1 - 999 minutes.

/TN taskname Specifies a name which uniquely
identifies this scheduled task.

/TR taskrun Specifies the path and file name of
the program to be run by this
scheduled task.
Example: C:\windows\system32\calc.exe

/ST starttime Specifies the time to run the task.
The time format is HH:MM:SS (24 hour
time) for example, 14:30:00 for
2:30 PM.

/SD startdate Specifies the first date on which the
task runs. The format is
"mm/dd/yyyy".

/ED enddate Specifies the last date when the task
should run. The format is
"mm/dd/yyyy".

/? Displays this help/usage.

Modifiers: Valid values for the /MO switch per schedule type:
MINUTE: 1 - 1439 minutes.
HOURLY: 1 - 23 hours.
DAILY: 1 - 365 days.
WEEKLY: weeks 1 - 52.
ONCE: No modifiers.
ONSTART: No modifiers.
ONLOGON: No modifiers.
ONIDLE: No modifiers.
MONTHLY: 1 - 12, or
FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY.

Examples:
SCHTASKS /Create /S system /U user /P password /RU runasuser
/RP runaspassword /SC HOURLY /TN rtest1 /TR notepad
SCHTASKS /Create /S system /U domain\user /P password /SC MINUTE
/MO 5 /TN rtest2 /TR calc.exe /ST 12:00:00
/SD 10/20/2001 /ED 10/20/2001 /RU runasuser /RP
SCHTASKS /Create /SC MONTHLY /MO first /D SUN /TN game
/TR c:\windows\system32\freecell
SCHTASKS /Create /S system /U user /P password /RU runasuser
/RP runaspassword /SC WEEKLY /TN test1 /TR notepad.exe
SCHTASKS /Create /S system /U domain\user /P password /SC MINUTE
/MO 5 /TN test2 /TR c:\windows\system32\notepad.exe
/ST 18:30:00 /RU runasuser /RP *
SCHTASKS /Create /SC MONTHLY /MO first /D SUN /TN cell
/TR c:\windows\system32\freecell /RU runasuser





SCHTASKS /delete /?

SCHTASKS /Delete [/S system [/U username [/P password]]] /TN taskname
[/F]

Description:
Deletes one or more scheduled tasks.

Parameter List:
/S system Specifies the remote system to connect to.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/TN taskname Specifies the name of the scheduled task to
delete. Wildcard "*" may be used to delete
all tasks.

/F (Force) Deletes the task and suppresses
warnings if the specified task is currently
running.

/? Displays this help/usage.

Examples:
SCHTASKS /Delete /TN * /F
SCHTASKS /Delete /TN "Backup and Restore"
SCHTASKS /Delete /S system /U user /P password /TN "Start Restore"
SCHTASKS /Delete /S system /U user /P password /TN "Start Backup" /F








SCHTASKS /query /?

SCHTASKS /Query [/S system [/U username [/P password]]] [/FO format]
[/NH] [/V] [/?]

Description:
Enables an administrator to display the scheduled tasks on the
local or remote system.

Parameter List:
/S system Specifies the remote system to connect to.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/FO format Specifies the output format to be
displayed. Valid values: TABLE, LIST, CSV.

/NH Specifies that the column header should not
be displayed in the output.
Valid only for TABLE and CSV formats.

/V Specifies additional output to be
displayed.

/? Displays this help/usage.

Examples:
SCHTASKS /Query
SCHTASKS /Query /?
SCHTASKS /Query /S system /U user /P password
SCHTASKS /Query /FO LIST /V /S system /U user /P password
SCHTASKS /Query /FO TABLE /NH /V






SCHTASKS /change /?

SCHTASKS /Change [/S system [/U username [/P password]]] {[/RU runasuser]
[/RP runaspassword] [/TR taskrun]} /TN taskname

Description:
Changes the program to run, or user account and password used
by a scheduled task.

Parameter List:
/S system Specifies the remote system to connect to.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/RU username Changes the user name (user context) under
which the scheduled task has to run.
For the system account, valid values are
"", "NT AUTHORITY\SYSTEM" or "SYSTEM".

/RP password Specifies a new password for the existing
user context or the password for a new
user account. Password will not effect for
the system account.

/TR taskrun Specifies a new program that the scheduled
task runs. Type the path and file name of
the program.

/TN taskname Specifies which scheduled task to change.

/? Displays this help/usage.

Examples:
SCHTASKS /Change /RP password /TN "Backup and Restore"
SCHTASKS /Change /TR restore.exe /TN "Start Restore"
SCHTASKS /Change /S system /U user /P password /RU newuser
/TN "Start Backup"






SCHTASKS /Run /?

SCHTASKS /Run [/S system [/U username [/P password]]] /TN taskname

Description:
Runs a scheduled task immediately.

Parameter List:
/S system Specifies the remote system to connect
to.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/TN taskname Identifies the scheduled task to run.

/? Displays this help/usage.

Examples:
SCHTASKS /Run /?
SCHTASKS /Run /TN "Start Backup"
SCHTASKS /Run /S system /U user /P password /TN "Backup and Restore"








SCHTASKS /End /?

SCHTASKS /End [/S system [/U username [/P password]]] /TN taskname

Description:
Stops a running scheduled task.

Parameter List:
/S system Specifies the remote system to connect
to.

/U username Specifies the user context under
which the command should execute.

/P password Specifies the password for the given
user context.

/TN taskname Specifies the scheduled task to
terminate.

/? Displays this help/usage.

Examples:
SCHTASKS /End /?
SCHTASKS /End /TN "Start Backup"
SCHTASKS /End /S system /U user /P password /TN "Backup and Restore"