-2

Once again I am fighting with Pascal syntax.

I just don't get it where begin and end should be used.

My current approach is this, but Inno Setup tells me that a semicolon is expected in the line "if IsServiceRunning('oscmaintenanceservice') = true then"

Can anybody shed some light on this?

Thank you!

begin
   if (CurStep=ssInstall) then
           MsgBox('ssInstall.', mbInformation, MB_OK);
           if IsServiceInstalled('oscmaintenanceservice') = true then
               MsgBox('ssInstall: Service is installed.', mbInformation, MB_OK);
               if IsServiceRunning('oscmaintenanceservice') = true then
                           MsgBox('ssInstall: Service is running.', mbInformation, MB_OK);
                       if not StopService('oscmaintenanceservice') = true then
                               MsgBox('ssInstall: Couldnt stop service.', mbInformation, MB_OK);
                       else
                               MsgBox('ssInstall: Service was stopped.', mbInformation, MB_OK);
                           if not RemoveService('oscmaintenanceservice') = true then
                                   MsgBox('ssInstall: Couldnt remove service.', mbInformation, MB_OK);
                           else
                                   MsgBox('ssInstall: Service was removed', mbInformation, MB_OK);
               else
                       MsgBox('ssInstall: Service not running.', mbInformation, MB_OK);
               end;
           else
                   MsgBox('ssInstall: Service not installed.', mbInformation, MB_OK);
   if (CurStep = ssPostInstall) then
       DeleteFile(ExpandConstant('{localappdata}\OnScreenCommunicator\mutex.dat'));
       PinAppTo(ExpandConstant('{app}\OSC.exe'),  pdStartMenu);
       if (IsHigherThanWindowsXP()) then
       begin
           PinAppTo(ExpandConstant('{app}\OSC.exe'), pdTaskbar);
       end;
       if not InstallService(ExpandConstant('{app}\maintenanceservice.exe'),'oscmaintenanceservice','oscmaintenanceservice','desc', SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START) = true then
       begin
           MsgBox('ssPostInstall: Couldnt install service.', mbInformation, MB_OK);
       end;
       if not StartService(ExpandConstant('{app}\maintenanceservice.exe')) then
       begin
           MsgBox('ssPostInstall: Couldnt start service.', mbInformation, MB_OK);
       end
Maiken Roskilde
  • 447
  • 3
  • 14
  • I wrote a pretty comprehensive guide to using begin..end in Pascal and Inno Setup in the post @MartinPrikryl linked. – Ken White May 23 '16 at 18:15
  • I wish there was an online converter available. :-) – Maiken Roskilde May 23 '16 at 18:20
  • @MartinPrikryl I do see your tutorial, but anyways, something is still wrong. Would you perhaps be willing to take a look at my current code? – Maiken Roskilde May 23 '16 at 18:37
  • The code in your question is missing `begin` after the `if (CurStep=ssInstall) then`. And it is missing both `begin` and `end` around the code block after the `if (CurStep = ssPostInstall) then`. – Martin Prikryl May 23 '16 at 18:43
  • @MartinPrikryl I changed that (I have changed my code according to that), but now it tells me that a semicolon is missing. I don't see why. – Maiken Roskilde May 23 '16 at 18:54
  • Just add `;` after every `end`. Some are not necessary, but they do not hurt. But mainly, revert to your previous code. You broke it. The `IsServiceRunning` branch is not anymore within the `IsServiceInstalled` branch. I do not think you wanted that. – Martin Prikryl May 23 '16 at 19:00
  • @MartinPrikryl I did, and I updated the code in the post, but now I'm getting a "Identifier expected" error. Can you have a look again? – Maiken Roskilde May 23 '16 at 19:15
  • If you know C, C#, Java, C++ etc. you can think of "begin" as the Pascal equivalent of "{" and "end" as the equivalent of "}". Now you just habe to find out when to use ";" and when not. Generally, it is similar as in C, just that before "else" you should not use it (well, there is a little more, but that is seldom important). – Rudy Velthuis May 23 '16 at 19:30
  • @RudyVelthuis Yes, I know C++, but I still don't get a hang of the 3rd line, whether MsgBox needs a begin / end. Do you think you could correct my mistake? – Maiken Roskilde May 23 '16 at 19:47
  • It is not necessary here, since it is not part of a control structure (if, else, while, with, etc.), but it can't hurt either. Would you put braces around this in C, etc.? I doubt it. – Rudy Velthuis May 23 '16 at 20:24
  • We're not going to babysit you through fixing your code. Read the link you were provided, and apply it to your code. It addresses both the use of `begin..end` and `;`. – Ken White May 23 '16 at 20:26
  • Not babysitting, but it might also help other users in the same situation. Pascal is just a complicated programming language. – Maiken Roskilde May 23 '16 at 20:27
  • Actually, Pascal is quite simple, compared to other programming languages. I tried to fix your code, but somehow, the structure seems to be wrong. There are too many elses, compared to ifs. You might want to format your code so the desired structure is clearer. I don't quite know what you want to achieve. Again, replace begin and end with { and } and see if you can see the structure clearer. And again, properly format your code, so everyone, including you, can see the entire structure. – Rudy Velthuis May 23 '16 at 20:38
  • @RudyVelthuis Oh, okay, so you can't fix it as well. This motivates me. :-) – Maiken Roskilde May 23 '16 at 20:39
  • Why don't you simply write the code without begin and end, but properly indented, so you can see the structure. Then start inserting begin and end where needed. Most of them are not needed at all. Only if you have more than one statement at the same indentation level, you put begin/end around them. – Rudy Velthuis May 23 '16 at 20:41
  • Instead of downvoting my question you should have answererd it, if you knew what the fault is. – Maiken Roskilde May 23 '16 at 22:19
  • 1
    Pascal is not a complicated language (which is why they use it for teaching programming). I wrote a tutorial on using `begin..end` and `;` already, and we've shown you where it is located. If you can't be bothered to read through it to work out your issue, and you expect more from us than a clear, concise tutorial, you're expecting us to hold your hand and babysit, and that's not the purpose of this site. We share *knowledge*, and it's your responsibility to take that knowledge we're sharing and apply it to your particular situation. – Ken White May 23 '16 at 22:44

1 Answers1

1

This is a version of your code with the minimal number begin, end and ;.

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    MsgBox('ssInstall.', mbInformation, MB_OK);

    if IsServiceInstalled('oscmaintenanceservice') then
    begin
      MsgBox('ssInstall: Service is installed.', mbInformation, MB_OK);

      if IsServiceRunning('oscmaintenanceservice') then
      begin
        MsgBox('ssInstall: Service is running.', mbInformation, MB_OK);

        if not StopService('oscmaintenanceservice') then
          MsgBox('ssInstall: Couldnt stop service.', mbInformation, MB_OK)
        else
          MsgBox('ssInstall: Service was stopped.', mbInformation, MB_OK);

        if not RemoveService('oscmaintenanceservice') then
          MsgBox('ssInstall: Couldnt remove service.', mbInformation, MB_OK)
        else
          MsgBox('ssInstall: Service was removed', mbInformation, MB_OK);
      end
        else MsgBox('ssInstall: Service not running.', mbInformation, MB_OK)
    end
      else MsgBox('ssInstall: Service not installed.', mbInformation, MB_OK)
  end;

  if CurStep = ssPostInstall then
  begin
    DeleteFile(ExpandConstant('{localappdata}\OnScreenCommunicator\mutex.dat'));
    PinAppTo(ExpandConstant('{app}\OSC.exe'),  pdStartMenu);

    if IsHigherThanWindowsXP() then
      PinAppTo(ExpandConstant('{app}\OSC.exe'), pdTaskbar);

    if not InstallService(ExpandConstant('{app}\maintenanceservice.exe'), 'oscmaintenanceservice', 'oscmaintenanceservice', 'desc', SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START) then
    begin
      MsgBox('ssPostInstall: Couldnt install service.', mbInformation, MB_OK);

      if not StartService(ExpandConstant('{app}\maintenanceservice.exe')) then
        MsgBox('ssPostInstall: Couldnt start service.', mbInformation, MB_OK)
    end;
  end;
end;

For a comparison, below is an equivalent version of the code with all the optional/redundant begin, end and ;. Maybe it will help you to understand.

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    MsgBox('ssInstall.', mbInformation, MB_OK);

    if IsServiceInstalled('oscmaintenanceservice') then
    begin
      MsgBox('ssInstall: Service is installed.', mbInformation, MB_OK);

      if IsServiceRunning('oscmaintenanceservice') then
      begin
        MsgBox('ssInstall: Service is running.', mbInformation, MB_OK);

        if not StopService('oscmaintenanceservice') then
        begin
          MsgBox('ssInstall: Couldnt stop service.', mbInformation, MB_OK);
        end
          else
        begin
          MsgBox('ssInstall: Service was stopped.', mbInformation, MB_OK);
        end;

        if not RemoveService('oscmaintenanceservice') then
        begin
          MsgBox('ssInstall: Couldnt remove service.', mbInformation, MB_OK);
        end
          else
        begin
          MsgBox('ssInstall: Service was removed', mbInformation, MB_OK);
        end;
      end
        else
      begin
        MsgBox('ssInstall: Service not running.', mbInformation, MB_OK);
      end;
    end
      else
    begin
      MsgBox('ssInstall: Service not installed.', mbInformation, MB_OK);
    end;
  end;

  if CurStep = ssPostInstall then
  begin
    DeleteFile(ExpandConstant('{localappdata}\OnScreenCommunicator\mutex.dat'));
    PinAppTo(ExpandConstant('{app}\OSC.exe'),  pdStartMenu);

    if IsHigherThanWindowsXP() then
    begin
      PinAppTo(ExpandConstant('{app}\OSC.exe'), pdTaskbar);
    end;

    if not InstallService(ExpandConstant('{app}\maintenanceservice.exe'), 'oscmaintenanceservice', 'oscmaintenanceservice', 'desc', SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START) then
    begin
      MsgBox('ssPostInstall: Couldnt install service.', mbInformation, MB_OK);

      if not StartService(ExpandConstant('{app}\maintenanceservice.exe')) then
      begin
        MsgBox('ssPostInstall: Couldnt start service.', mbInformation, MB_OK);
      end;
    end;
  end;
end;

Once you get your head around this, please accept the duplicate, or delete your question altogether.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992