If you get error 193 “Not a valid Win32 application” when starting a Windows service, it’s often because the Windows Service Manager cannot find the service. This is caused by the way the Windows Service Manager handles command line arguments. Error 193, among other things, occurs when a directory name contains a space (e.g., C:\Users\Fridolin Froehlich) and there is a file in the parent directory (C:\Users) whose filename consists of the first word of the directory (e.g., “C:\Users\Fridolin”). Sounds complicated – so let’s look at an example:
Example:
Our user is called “Fridolin Froehlich” and his home directory is C:\Users\Fridolin Froehlich
There is also a file named Fridolin in the Users directory
The created Windows service is located in the directory C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin
We register the Windows service in PowerShell with administrator privileges using the command:sc.exe create "Mein Windows-Service" binpath="C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe"
After that, the creation of the Windows service is confirmed:
[SC] CreateService SUCCESS
IMPORTANT: This success message does not guarantee that the service will actually be found and started by the Windows Service Manager.
Now let’s try to start the service:
sc.exe start "Mein Windows-Service"
We instantly get an error message: [SC] StartService FAILED, error 193.
Error 193 – The Cause
While creating the Windows service, we did put the binpath in quotation marks, but Windows only takes the text within the quotes to define the service’s binpath. You can see this if you open the Service Manager (by entering services.msc in the search or from the command line) and view the service’s details. There the path looks like this:
C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe
But the quotation marks are missing. That’s why the Service Manager tries to find and run the file C:\Users\Fridolin. As is well known, command line arguments are separated by spaces, so our binpath looks like two (!) arguments to the Windows Service Manager.
How do you fix error 193 when starting a Windows service?
- Option A: Add quotes to the binpath
This is the cleanest solution. To avoid error 193 when starting a Windows service, you need to use escaped quotation marks in the binpath:\”sc.exe create "Mein Windows-Service" binpath="\”C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe"
Note the backslash (\) before the additional quotes. - Option B: Find and rename the problematic file
In the example above, the fileC:\Users\Fridolinis the problem. If you still need the file, simply rename it. It’s enough to give it a file extension (e.g..txt). For example, use the commandren C:\Users\Fridolin C:\Users\Fridolin.txt.
If you don’t need the file anymore, you can just delete it, e.g. with the commanddel C:\Users\Fridolin - Option C: Choose a different directory to save the Windows service
Usually the user directory (in our caseC:\Users\Fridolin Froehlich) is the problem since it often contains multiple words (first name, last name). So we need to select or create a directory that is not inside the user directory. For example,C:\Projekte\Mein Windows-Service. You can create this directory withmd "C:\Projekte\Mein Windows-Service".
Then, copy your service exe uh service EXE to this directory:copy "C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe" "C:\Projekte\Mein Windows-Service\"
What to do if you get the error message “Set-Content : A positional parameter cannot be found that accepts argument ‘binpath='”?
This error occurs if you use only the command sc in PowerShell instead of sc.exe.
The command sc in Windows PowerShell launches the “Set-Content” cmdlet. But that’s not what we need. We need sc.exe – a command line program for communicating with the Service Control Manager and Windows services.
Sources
How to: register service with SC command without losing quotes in binpath value
When creating a service with sc.exe how to pass in context parameters?