1

I've found a lot of questions on this topic which I will post links to. Unfortunately they all either didn't answer my question or they seem to but then led to code examples on msdn that didn't compile in Visual Studio.

I want to turn an exe compiled from Visual Studio in C++ into a service using the method mentioned in the top answer here but when I try to add the necessary serviceMain function which leads to all the other service functions that need to be added I end up trying to compile this file which doesn't compile. One of the many errors being that it complains about #include "sample.h" because it's was never created despite the fact that the page notes that the compiler will generate that header file which VS doesn't do.

I just want to be able to take a self-made via VS exe made from a .cpp file and turn it ito a windows service without using an external program unless built into the OS such as sv.exe as mentioned in the link above with as little WINAPI as possible.

the code from that svc.cpp file produces the following errors with #include "smaple.h" commented out.

argument of type "const char *" is incompatible with parameter of type "HMODULE"    

    identifier "SVC_ERROR" is undefined 

        'DWORD GetModuleFileNameW(HMODULE,LPWSTR,DWORD)': cannot convert argument 1 from 'const char [1]' to 'HMODULE'  

    'SVC_ERROR': undeclared identifier  

Also when googling about the the const char errors I got led to this which didn't work even when changing the error code to active which is what it is for me. I got led to that because when I googled about the error I found another question in which the answer said that it depended on changing the build settings and when I googled about that I got led to the question I linked to directly above whose answer didn't work.

On a side note I can't believe how much harder it is (as well as how much more code is involved) to turn an executable program into a service is turning out to be when the only general difference is automatic vs manual execution.

Also if there is a way to do what I'm trying to do without using WINAPI nor an external program please let me know.

Update: I trying to imitate what programs such as AV programs do when they add themselves to the registry so that they autostart on boot.

Community
  • 1
  • 1
codehelp4
  • 132
  • 1
  • 2
  • 15
  • `GetModuleFileName( "", ... )` should be `GetModuleFileName( NULL, ... )` instead. The example is passing a string where a module handle is expected. Just because the example does not compile as-is does not alleviate you from needing to **read documentation**, like [this one](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197.aspx), to understand what the example is *trying* to show you. – Remy Lebeau Mar 23 '17 at 01:45
  • And services are not easy to write from scratch, there is a whole API involved to communicate back and forth with the Service Control Manager. It is not just a matter of "automatic vs manual execution" of the EXE. You don't seem to understand what a service actually is. If all you want is auto-execution, look at the Registry's various [`Run` keys](https://msdn.microsoft.com/en-us/library/aa376977.aspx), or the [Task Scheduler](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383614.aspx). – Remy Lebeau Mar 23 '17 at 01:50
  • 1
    Aside from not understanding the example correctly, I don't understand why it can be called **complete** if it's not able to compile from copy/paste. Also as far as looking at the registry I would still need to use a bunch of [WINAPI code] (http://stackoverflow.com/questions/508614/create-a-new-windows-registry-key-using-c) because I'd need to be able to add the values programmatically. Again I don't understand why it would require so much. – codehelp4 Mar 23 '17 at 04:35
  • Also when I said automatic vs manual execution being the general difference I meant core difference in functionality of a program between what the program can do without being a service vs being a service – codehelp4 Mar 23 '17 at 04:36
  • You're supposed to build `sample.h` yourself, [as described in the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/bb540472(v=vs.85).aspx). The compiler won't do it for you. – Harry Johnston Mar 23 '17 at 22:17
  • The call to `GetModuleFileName` is indeed a bug in the sample code. Whoever wrote the code made a mistake, it happens, they didn't do it on purpose just to confuse you. It probably compiled at the time it was posted, the compiler has gotten somewhat pickier over the last couple of decades. – Harry Johnston Mar 23 '17 at 22:21
  • @HarryJohnston I quote "The file Sample.h is generated when building the resource-only DLL, Sample.dll. For more information, see Sample.mc." [taken from here](https://msdn.microsoft.com/en-us/library/windows/desktop/bb540475(v=vs.85).aspx) And as far as what you said about GetModuleFileName thanks for confirming my suspicion. It's surprising to me that we wouldn't update it and ironic that visual studio would have issues with it. I've written feedback to them regarding that page. – codehelp4 Mar 23 '17 at 23:05
  • Did you follow the instructions for building the resource-only DLL? It won't happen automatically. If you successfully built `sample.dll` but it didn't generate `sample.h` then something else has gone wrong. Post the output from the build process. – Harry Johnston Mar 23 '17 at 23:11
  • (I'm sure that if anyone at Microsoft had noticed that there was a bug in the sample they'd have gotten it fixed. They do look at posted MSDN feedback, I think, eventually, so thanks for doing that - but don't hold your breath!) – Harry Johnston Mar 23 '17 at 23:15
  • Note that the original purpose of services is device drivers. They are very different from applications. In this context, applications are called "user-mode" programs. Services (device drivers) are "kernel mode" programs, as in [User mode and kernel mode](https://msdn.microsoft.com/en-us/windows/hardware/drivers/gettingstarted/user-mode-and-kernel-mode). Services have **much** higher privilege. You must be prepared to do much more reading to implement a service. – Sam Hobbs Mar 24 '17 at 02:42
  • Since you say nothing about the external program you are trying to convert to a service, this is not a question. – Sam Hobbs Mar 24 '17 at 02:44
  • @HarryJohnston Don't worry I won't hold my breath. LOL – codehelp4 Mar 25 '17 at 00:35
  • @user34660 I thought that an antivirus program was an example of a service with its real-time protection and the fact that it starts at boot. I'm trying to do something similar to that. – codehelp4 Mar 25 '17 at 00:35
  • The main reason to make an antivirus program a service is so that it has access to everything, even more than a typical user (owner of the computer) would need or should have. If you ignore that and just dwell on things like automatically starting at boot time then of course it is not clear what the difference is. – Sam Hobbs Mar 25 '17 at 00:44
  • FYI you can run a regular exe as a service without modifying the code of the exe by registering a generic service adapter that launches your exe. For example, you can use the program [nssm](https://nssm.cc/) – Wyck Mar 25 '17 at 01:15
  • @user34660 hmm never knew that. Thanks for the info. I started wondering just now why I was trying to make my program a service and after googling the difference I realized why it was suggested to me to do so. By [this](https://www.google.com/#q=what+the+difference+between+a+program+and+a+service+&*) definition the reason I'm trying to make my program a service is because its function is equivalent to a background process. – codehelp4 Mar 25 '17 at 01:17
  • @Wyck Thanks I found that program too but as I mentioned in the main question I was trying to avoid using an external program although with how much of challenge not going that route seems to be I may end up going that route so thanks for reminding me about it. – codehelp4 Mar 25 '17 at 01:42
  • See [Windows Service and UI Problem](https://social.msdn.microsoft.com/Forums/vstudio/en-US/c164bc60-b8c0-4525-8c0b-2db069e694eb/windows-service-and-ui-problem?forum=csharpgeneral) and [Windows Services Frequently Asked Questions (FAQ) | Tips for managing Windows 10/8.1/8/2012 R2/7/2008/Vista/2003/XP Services](https://www.coretechnologies.com/WindowsServices/FAQ.html#GUIServices) and [Impact of Session 0 Isolation on Services and Drivers in Windows - Windows 10 hardware dev](https://msdn.microsoft.com/en-us/library/windows/hardware/dn653293(v=vs.85).aspx). – Sam Hobbs Mar 25 '17 at 02:07
  • @user34660, services and device drivers, while they share some infrastructure, are conceptually quite different things. Services are user-mode, not kernel-mode. – Harry Johnston Mar 25 '17 at 02:47

0 Answers0