How to solve “Error 193 – not a valid Win32 application”?

Error 193 "Not a valid Win32 application" can occur when starting a Windows service. In this article, I explain what this error means and how you can fix it.

Error 193 “Not a valid Win32 application” can occur when starting a Windows service. In this article, I explain what this error means and how you can fix it.

Error 193 – In Brief

If you encounter the error 193 “Not a valid Win32 application” when starting a Windows service, it is often because the corresponding Windows service has not been correctly registered in the Windows Service Manager. To better understand this, you need to know what distinguishes a Windows service from a regular Windows program.

Windows Service – What Exactly Is It?

A Windows service is not simply a normal Windows program that you start with a mouse click. A Windows service is a program that runs in the background and does not have a user interface. When such a Windows service is running, you see nothing on the monitor.

To start and stop these so-called Windows services, there is the Windows Service Manager. You can access it by typing “services.msc” into the search box on the taskbar. In German, this service manager program is simply called “Dienste”.

For a program to run as a Windows service, it must be registered in the services program. This can be done, for example, with the command-line tool “sc.exe”.

Error 193 – The Cause

How does error 193 “Not a valid Win32 application” actually happen? The Windows Service Manager has a “special” way of handling command line arguments. Error 193 can occur, among other things, when a directory name contains a space (e.g., C:\Users\Fridolin Froehlich) and in the parent directory (C:\Users) there is a file whose name consists of the first word of the directory (e.g., “C:\Users\Fridolin”). Sounds complicated, so let’s look at an example:

“Not a valid Win32 application” – Example:

Suppose your Windows user is named “Fridolin Froehlich” and his home directory is C:\Users\Fridolin Froehlich

In the directory C:\Users there is also a file named Fridolin

Suppose the Windows service you want to register is located in C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin

You register the Windows service in PowerShell with administrator rights with the command:
sc.exe create "Mein Windows-Service" binpath="C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe"

After that, you receive confirmation that the Windows service was created:

[SC] CreateService SUCCESS

That looks good at first glance. We enclosed the binpath in double quotes since there are spaces, and sc.exe apparently was able to create the service successfully.

IMPORTANT: This success message is no guarantee that the service can actually be found and started by the Windows Service Manager.

But if you now try to start the service, it fails:

sc.exe start "Mein Windows-Service"

[SC] StartService FAILED, error 193.

To understand this, you need to know how the service manager handles the binpath argument.

The Windows Service Manager and Its Command Line Arguments … 🤯😫

When you created the Windows service, you set the binpath in quotes, but Windows only takes the text inside the quotes to set the service’s binpath. You can see this if you call up the service manager (search for it in the taskbar search or run services.msc from the command line) and look at the service details. Just search for the service “Mein Windows-Service” in the list and double-click it. In the small window, there is the entry “Path to executable”. There, the path appears like this:

C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe

The quotes are missing, although we used quotes on the command line 🤦‍♂️. That’s what I mean by “the Windows Service Manager only uses the text inside the quotes“.

Alright. So our binpath now looks as above. Great. When the Windows Service Manager wants to start the service, it sends the command C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe to the command processor. This processes the command as if it were entered at the command line. Spaces on the command line act as delimiters. For example, a space must divide the program you want to run from the command line arguments. And if you specify multiple command line arguments, they too must be separated by spaces.

So how does our command processor interpret the call to the Windows service? The command processor “thinks” as follows:

    1. Call the program C:\Users\Fridolin

    1. First argument: Froehlich\Repositories\Mein

    1. Second argument: Windows-Service\bin\Mein

    1. Third argument: Windows-Service.exe

So the command processor tries to find and execute the file C:\Users\Fridolin. If this file exists but is not a Windows program, you get error 193 “Not a valid Win32 application”. Understandable …

How Do You Fix Error 193?

    • Option A: Add quotes to binpath

      This is the cleanest solution. To avoid error 193 when starting a Windows service, you must insert escaped quotes into the binpath argument:

      sc.exe create "Mein Windows-Service" binpath="\"C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe\""

      The important thing is the backslash (\) before the extra quotes.

    • Option B: Locate and rename the problem file

      In the above example, the file C:\Users\Fridolin is causing the problem. If you still need the file, simply rename it. It is sufficient to give this file a new file extension (e.g., .txt). This can be done, for example, with the command ren C:\Users\Fridolin C:\Users\Fridolin.txt.

      If you no longer need the file, just delete it, for example with the command del C:\Users\Fridolin

      But: Doesn’t the problem with the space as delimiter remain? Interestingly, no. Since the file C:\Users\Fridolin
      no longer exists, the command processor no longer tries to call it. It then ignores the spaces and can start the Windows service 🤦‍♂️ 🤷‍♂️

    • Option C: Choose a different directory to store the Windows service

      Usually, the user directory (in our case C:\Users\Fridolin Froehlich) is the problem, because it usually contains multiple words (first name, last name). So we need to find or create a directory that is not inside the user directory. For example: C:\Projekte\Mein Windows-Service This directory can be created with the command md "C:\Projekte\Mein Windows-Service"

      Then, copy your service exe to this directory: copy "C:\Users\Fridolin Froehlich\Repositories\Mein Windows-Service\bin\Mein Windows-Service.exe" "C:\Projekte\Mein Windows-Service\"

Error: “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 Cmdlet “Set-Content”. However, that is not what we want. 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?

Share the Post:

Leave a Reply

Your email address will not be published. Required fields are marked *

More From my Blog

WordPress Cookie Notice by Real Cookie Banner