11

I wrote a batch script to execute after my installation that is made with Inno Setup. The problem is that I have the following command line for my service creation:

sc create MySQL start= auto DisplayName= MySQL binPath= "C:\MyApp\MySQL 5.5\bin\mysqld" --defaults-file="C:\MyApp\MySQL 5.5\my.ini"

The letters with accents are the problem of this code, I can't execute it if I open the bat file in cmd, but when I type the service is created normally. How can I fix that?

llanfair
  • 1,845
  • 4
  • 27
  • 43
  • 1
    You could use `AfterInstall` and then `Exec` function or `[Run]` `Filename: "{cmd}"; Parameters: "sc create MySQL start= auto DisplayName= MySQL binPath= ""C:\A Nova Solução Informática\MySQL 5.5\bin\mysqld"" --defaults-file=""C:\A Nova Solução Informática\MySQL 5.5\my.ini"""; Flags: runhidden` – RobeN Mar 04 '13 at 15:12
  • 1
    Try to add `CHCP 65001` at the beginning of your batch file and save it as UTF-8 without BOM. @Roben, or just `[Run]` section. – TLama Mar 04 '13 at 15:13
  • @RobeN, your solution partially helped me, the problem is that after the instalation of the service, the cmd process stay opened and the instalation don't finalize itself. – llanfair Mar 06 '13 at 11:08

2 Answers2

16

Rather than calling SC directly, it's cleaner (and easier to deal with any errors or service dependencies) to use the API. Note that this example assumes that you're using ANSI Inno, but it's fairly straightforward to modify this for Unicode.

Personally, though, I prefer to build install/start/stop/uninstall commands into the service executable itself, making it self-registering. Obviously this isn't possible to do yourself for a third-party service, but you could check to see if it already supports something like this.

One final point is that you must not hard-code the path to a C: folder. You should be using {app} instead.

Nanhydrin
  • 4,332
  • 2
  • 38
  • 51
Miral
  • 12,637
  • 4
  • 53
  • 93
  • Do you have a Unicode version of the API already? I would need it very much. Thank you! – Maiken Roskilde May 23 '16 at 16:41
  • 3
    @MaikenRoskilde If you're using Unicode Inno Setup, then simply change the `A` suffix just prior to the `@` sign in the `external` entries to a `W` instead. No other changes should be required. – Miral May 24 '16 at 00:08
  • The linked script by Silvio Iaccarino did not work for me. I was using Unicode Inno Setup and tried changing the suffixes as suggested but still didn't get it to work. I kept getting an error: "servicemanager is not available" or similar. See [this answer](https://stackoverflow.com/a/5416744/207981) for an alternative script/library that **did** work for me. – maltem-za May 26 '17 at 13:14
  • It's cleaner, but it's like 1TB code just for that !?! – Sandburg Mar 19 '19 at 16:57
  • It's hardly that big, especially when compiled. If it bothers you, you can extract most of it to a file that you can #include as needed. (Or use the library that bszom linked to, which did it for you already.) – Miral Mar 21 '19 at 01:50
4

You could try to add this command to [RUN] section (as TLama suggested) or create AfterInstall function in [CODE] section.

[Run] 
Filename: "{cmd}"; Parameters: "sc create MySQL start= auto DisplayName= MySQL 
binPath= ""C:\MyApp\MySQL 5.5\bin\mysqld"" 
--defaults-file=""C:\MyApp\MySQL 5.5\my.ini"""; 
Flags: runhidden
llanfair
  • 1,845
  • 4
  • 27
  • 43
RobeN
  • 5,346
  • 1
  • 33
  • 50
  • 1
    Has anyone tested this code? I am trying to use this code but it does not work. The cmd window will just pop up and do nothing. – Newbee Sep 17 '13 at 15:07
  • @Govs did you try with `/c` at the beginning of `Parameters` (`cmd /c sc ...`), or just `Filename: "sc"; Parameters: "create ..."` – Matthieu Oct 02 '14 at 20:36
  • 1
    `Filename: "sc"; Parameters: "create service_name start= auto DisplayName= display_name binPath= ""{sys}\service_name.exe"""; Flags: runascurrentuser runhidden` works for me – bartolo-otrit Jan 08 '16 at 13:05
  • @bartolo-otrit two syntaxes : (yours) `Filename: "{sys}\sc.exe"; Parameters:"create ..."`or `Filename: "{cmd}"; Parameters:"sc create ..."` I really would like to know the subtleties? – Sandburg Mar 20 '19 at 08:37
  • @Sandburg Concerning direct call vs indirect call by a command line I don't have any subtleties to share this time, but it looks like an indirect call is unneeded in this case. – bartolo-otrit Mar 20 '19 at 08:57