How do I create a windows service as an exe file using C ? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I create a windows service as an exe file using C ?

jcprogrammerjcprogrammer dubaiPosts: 18Member

I want to run a windows service which will notify me of any changes to a folder/file in the shared drive at my office. But mind you we don't have administrator privileges on our computers. Is there a way to write a program in C and create an exe file which will run on my office computer? Any help/suggestions would be welcome... Thanks

Tagged:
«134

Comments

  • IcepickleIcepickle GermanyPosts: 49Member

    as far as i see, you only need a program that you can start / stop with a filesystem watcher, or by "continuously" polling a file and check its date time stamp (with at least an interval of a second between checks, depending on how was you want to be notified).

    The question is, do you want to write it with C/C++, or are you fine with an easier language like C#, Vb.net?

    What do you mean, run like a service? You couldn't install it as a service, as you mentioned, you do not have administrator rights? Is it enough for you that the file notifications are only given to you when you are logged in on your computer? if it helps, most files contain a history which user updated it last

  • jcprogrammerjcprogrammer dubaiPosts: 18Member
    edited August 2014

    @Icepickle said: as far as i see, you only need a program that you can start / stop with a filesystem watcher, or by "continuously" polling a file and check its date time stamp (with at least an interval of a second between checks, depending on how was you want to be notified).

    The question is, do you want to write it with C/C++, or are you fine with an easier language like C#, Vb.net?

    What do you mean, run like a service? You couldn't install it as a service, as you mentioned, you do not have administrator rights? Is it enough for you that the file notifications are only given to you when you are logged in on your computer? if it helps, most files contain a history which user updated it last

    Thanks. Actually I have just started learning C. I have no idea of C# or VB.net, though I am very good with vba for excel. I also don't have much knowledge on windows services either but I had come across this term online and thought that this was the only way to create a program which will reside in memory once my computer starts and update me of any changes in the shared folder. I am open to any new and simpler ideas which you can share as well.

    My basic requirement is a program which will run when I login to my office computer and notify me of any changes that happen to that particular folder while I am logged in. But my IT dept should not be aware that I am running such a program, that's why I wanted a simple c program written as an exe as exe's are portable.

  • IcepickleIcepickle GermanyPosts: 49Member

    You could use vbscript files, these are not executables, are used in most networked environments, and can safely do what you ask them to do :)

    if you save this script as Monitor.vbs and you double click it, it will check a folder each 5 seconds to see if a file was added/removed, or updated since last time it checked the folder

    It will run continuously until you decide to kill the process (it runs under WScript.exe inside of the taskmanager, with your user priviliges). Upon directory changed it will show a messagebox in front of your other programs. You can change the folder it watches, by changing the watchFolder argument

    You should recognise some vba in here ;)

    To start it manually, you could add it to your startup programs, by coping it into:

    C:\Users[UserName]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

    replacing [UserName] by your username

    Dim watchFolder
    watchFolder = "C:\Dev"
    
    Function DirectoryChanged(directory, byref lastDateStamp, byref lastCount)
        Dim result
        Dim fso, folder, files, len, dStamp, objFile
        result = false
    
        dStamp = lastDateStamp
    
        set fso = CreateObject("Scripting.FileSystemObject")
        set folder = fso.GetFolder(directory)
        set files = folder.Files
        len = 0
        For Each objFile in folder.Files
            if objFile.DateLastModified > dStamp then
                dStamp = objFile.DateLastModified
                if lastCount <> 0 then
                    result = true
                end if
            end if
            len = len + 1
        next
    
        if (lastCount <> len) then
            if (lastCount > 0) then
                result = true
            end if
            lastCount = len
        end if
    
        lastDateStamp = dStamp
    
        DirectoryChanged = result
        Exit Function
        error_occured:
        DirectoryChanged = false
    End Function
    
    Dim curDate, curCount
    curDate = Now()
    curCount = 0
    
    
    while true
        if DirectoryChanged(watchFolder, curDDate, curCount) = true then
            MsgBox "'" & watchFolder & "' changed!", vbSystemModal, "Change detected in folder"
        end if
        WScript.Sleep(5000)
    wend
    

    @tienkhoanguyen: how about staying on topic? You sound slightly cuckoo

  • jcprogrammerjcprogrammer dubaiPosts: 18Member

    @icepickle wow! thank you so much! I know vba as I use it to code excel files so I could understand the logic behind this code. Thank you so much again. So now I am thinking of giving up learning C and learn more about vb scripting. Could you recommend a good book to learn it? Also what I want to know is, is VB portable to Linux? I am also trying to learn system programming for Linux hence took up C... Thanks

  • jcprogrammerjcprogrammer dubaiPosts: 18Member

    @icepickle this code is great but when I tested it doesn't give alerts instantly. I created a folder on my desktop and when I added another folder inside it, it didn't give me any alert but I got 2 alerts when I deleted a folder. So it looks like it alerts you only when a file or folder is deleted. Also how do I modify the code to display the modified, created or deleted file/folder? Thanks

  • IcepickleIcepickle GermanyPosts: 49Member

    @jcprogrammer‌ i only went for changes in one folder, and it only checks if something happened to the files. It doesn't actually check the folders. To check the subfolders, you would have to iterate the subfolders, by reusing the same command to scan any folders that are deeper. Mind you this could be some work, depending on how complex the directory structure is

    without much changes, you should be able to find out which was the added/changed file. If you also iterate the Folders in the current folder, you should be able to follow the same algorithm i gave you to find out if there are differences.

    To know which file was deleted will be a bit more difficult, since its gone, you would have to keep a cache, and then again, depending on how complex the folder structure is, this could take some doing, it is possible though, and yes, possible in VBS

    VB is not portable to linux, don't even try it :) Sure, there used to be a project like Gambas, but bash shell scripting is more powerfull to me, than a vbscript

  • jcprogrammerjcprogrammer dubaiPosts: 18Member

    @Icepickle‌ just one quick question, why are we iterating through all the files in the folder? if there are changes to any of the files or sub folders wouldn't this change the DateLastModified of the main folder? So why can't I just use the code to check the DateLastModified of the folder rather than looping through all the files in it?

  • IcepickleIcepickle GermanyPosts: 49Member
    edited August 2014

    Sadly enough, no, DateLastModified points to the actual node in the filesystem, it doesn't keep track of files that were changed inside the folder, so you have to:

    • count all the file/folder nodes inside a folder
    • compare every datemodified with the last time you checked it
  • jcprogrammerjcprogrammer dubaiPosts: 18Member

    @Icepickle said: Sadly enough, no, DateLastModified points to the actual node in the filesystem, it doesn't keep track of files that were changed inside the folder, so you have to:

    • count all the file/folder nodes inside a folder
    • compare every datemodified with the last time you checked it

    actually I tried it for only the folder and it works. Below is the modified code:

    Dim watchFolder
    watchFolder = "Z:\Sales"
    
    Function DirectoryChanged(directory, byref lastDateStamp, byref lastCount)
        Dim result
        Dim fso, folder, files, len, dStamp, objFile
        result = false
    
        dStamp = lastDateStamp
    
        set fso = CreateObject("Scripting.FileSystemObject")
        set folder = fso.GetFolder(directory)
        set files = folder.Files
        len = 0   
        if folder.DateLastModified > dStamp then
            dStamp = folder.DateLastModified
            if lastCount <> 0 then
               result = true
            end if
        end if
        len = len + 1    
    
        if (lastCount <> len) then
            if (lastCount > 0) then
                result = true
            end if
            lastCount = len
        end if
    
        lastDateStamp = dStamp
    
        DirectoryChanged = result
        Exit Function
        error_occured:
        DirectoryChanged = false
    End Function
    
    Dim curDate, curCount
    curDate = Now()
    curCount = 0
    
    
    while true
        if DirectoryChanged(watchFolder, curDDate, curCount) = true then
            WScript.Echo "'" & watchFolder & "' changed!", vbSystemModal, "Change detected in folder"
        end if
        WScript.Sleep(5000)
    wend
    
  • IcepickleIcepickle GermanyPosts: 49Member

    good, it could be that the network implementation of the last changed time is different (here on the network that doesn't work for me :D)

    in case you are leaving it out, you could also skip the lines 13 and 14, 21 to 29, and you don't need the lastCount parameter

    i guess it was also a typo but on row 44 CurDDate is being given as a parameter, though curDate is the defined one ;)

    happy to hear it works :)

«134
Sign In or Register to comment.