how to call my dll from windows service?

12,784

A DLL is "called" when you use it's functions. So if you have a DLL project with a few classes and a few functions inside your classes, simply add the DLL as a reference to your Service project and call the appropriate functions.

You may want to use this library for creating your Windows Service. The reason being, when you build a Windows Service using the Visual Studio template, the resulting build isn't really 'run'. The resulting build is a Windows Service that must be installed and then started using the Services Snap-In. But using Hoytsoft's custom Windows Service library, your service will install itself automatically and then start itself automatically - just like a regular Windows Form application.

To ensure your Service runs at startup, remember to configure your Service class to AutoStart (as explained in his CodeProject article). To ensure your Service runs continuously, even when the process is killed, you can add this handy registry hack which sets the Restart Service flags to Restart Immediately.

This Registry Hack == enter image description here

Registry Hack:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\<YOUR_SERVICE_NAME_HERE>", true);
key.SetValue("FailureActions", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, Microsoft.Win32.RegistryValueKind.Binary);
key.Close();

Understand that any administrative user can still kill your process by simply disabling your service.

Share:
12,784
Usher
Author by

Usher

Updated on June 08, 2022

Comments

  • Usher
    Usher about 2 years

    I created a c# library file,that will go to local path for e.g(c:\test.txt) find the file and upload to ftp server.

    For testing i just called the dll from console app but how i can run as a windows service,that will run continously ?

    I suppose to run this dll as a service to monitor the c:\ folder if any file comes like "test.txt" then upload.

    Thanks in Advance