Wednesday, May 6, 2009

Build Windows Service with Setup Project using VS2008

To create a windows service with a setup project for a service can easy way to install and start up the service after the service is installed.
here both windows service and  setup project are more works have to do.
first of all we have to create a windows service project and add some code for setup program so that
it capable installation the windows service and then to start it up.

Create a Windows Service Project


  1. In the Add new projcet dialog select Windows Service Template to create new service project.
  2. Right click service1.cs and click View Code
  3. In the OnStart event handler, replace the comments with anything code you want the service to run.
  4. In the OnStop event handler, replace the comments with the code if you need to free the resources.
  5. For the service installation, double click service1.cs to open Design View, In the Design View Right click and select Add Installer in this step a new class ProjectInstaller.cs will add to the project, two elements serviceProcessInstaller1 and serviceInstaller1 you can see in the ProjectInstaller.cs Design View.
  6. In the Properties of serviceInstaller1, change the ServiceName, DisplayName and Description properties for you need.
  7. In the Properties of serviceProcessInstaller1, change the Account to LocalSystem.
    Note: If you want override OnShutdown Event in the Service, you have to use LocalService Account, Otherwise like LocalSystem the OnShutdown Event will never be invoke.
    (LocalService and NetworkService values are available only in Microsoft Windows XP)
  8. If you want the service start up automatical at every tiems system start, you have to change the property StartType to Automatic.

    If you want the service startup when the service end of it installation, there more steps start from step 9 below you have to do.
  9. Add new class as a Install class into the project, open it in code view, Override the Comit Method as below:
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);
            ServiceController[] scServices = ServiceController.GetServices();
            foreach(ServiceController sc in scServices)
            {
                if(sc.ServiceName == "yourServiceName")
                {
                    if(sc.Status == ServiceControllerStatus.Stopped)
                    {
                        sc.Start();
                        break;
                    }
                }
            }
        }
Create a Setup Project for Windows Service
1. In the Add new projcet dialog select Setup Project Template to create new setup project.
2. Open the properties panel of the project, in the properties panle change the Manufacturer, ProductName to your fit, through the
    installation all the files will copy to the default folder which is composite from Program file/[Manufacturer]/[ProductName].
    but you can change the default install location setting in Properties of File System->Application Folder.
    the RemovePreciousVersions is false by default, indicate when you reinstall the program, the old version won't be uninstall.
    there are some more properties have to concern like Version, Author, Description, Subject etc.
2. In the Solution Explorer, Right click the projcet name and select View->File System, right click in the Application Folder
    and select Add->File or Add->Folder to add anything files you want to add to the folder where you service installed. you also
    can select Add->Project Output to specific any other projects output file into this setup project.
3. This step can install mySev.exe to windows system as a service. In the Solution Explorer, Right click the projcet name and select
    View->Custom Actions, Now a Select item dialog will show up to you, in the Look In Dropdown list select the file of
    window service mySev.exe in the Application Folder and then select ok. the file must been add by step 2.
4. In the Solution Explorer right click Project Name and select Properties to open the Project Properties Page, Click the buttom
    Prerequister, checked all .NET Framework version you need and uncheck the all you don't.
    If any version you select in the prerequist is missing in the installation computer, the install program will popup dialog to installing
    user to request install the properly version of .NET Framework.

For debug or some reason you can use Installutil.exe to install or uninstall the windows service by manual
Installutil.exe is only for .net, can not be uses in earlier windows service develop by C or C++.

Monday, May 4, 2009

Date Format in SQL

MS SQL
-- in this sample Dateadd() function retrieve that day befor 7 days by getdate() , then format it to varchar
-- the format as a date of japan yy/mm/dd
-- convert() function format date to 111 as japan date format , like this yyyy/mm/dd
select convert(varchar(20), DATEADD(day, -7, getdate()) ,111)

-- This sample casting date to float and floor the times part to 00:00:00.000
-- so that return the result same as 2008-10-22 00:00:00.000
-- If you trying to compare two datetime, this probably is best choice
SELECT CAST(FLOOR( CAST( GETDATE() AS FLOAT ) )AS DATETIME)


MySQL
-- Format a DATETIME or TIMESTAM to VARCHAR
-- for sample 2009-06-19 13:30:21

DATE_FORMAT(update_date,'%Y-%m-%d %T')

The formats that can be used are:

Format Description
%a Abbreviated weekday name
%b Abbreviated month name
%c Month, numeric
%D Day of month with English suffix
%d Day of month, numeric (00-31)
%e Day of month, numeric (0-31)
%f Microseconds
%H Hour (00-23)
%h Hour (01-12)
%I Hour (01-12)
%i Minutes, numeric (00-59)
%j Day of year (001-366)
%k Hour (0-23)
%l Hour (1-12)
%M Month name
%m Month, numeric (00-12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss AM or PM)
%S Seconds (00-59)
%s Seconds (00-59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00-53) where Sunday is the first day of week
%u Week (00-53) where Monday is the first day of week
%V Week (01-53) where Sunday is the first day of week, used with %X
%v Week (01-53) where Monday is the first day of week, used with %x
%W Weekday name
%w Day of the week (0=Sunday, 6=Saturday)
%X Year of the week where Sunday is the first day of week, four digits, used with %V
%x Year of the week where Monday is the first day of week, four digits, used with %v
%Y Year, four digits
%y Year, two digits