기본 콘텐츠로 건너뛰기

[윈도우즈 서비스] 설치 파일 만들기(WIX) - #4

이제 윈도우즈 서비스를 다 만들었기 때문에 설치 파일을 만들어 보겠습니다.
저희 같은 경우에는 불특정 다수의 PC에 설치하는 것이 아니고 서버에만 설치하면 되기 때문에
파일을 서버에 복사한 뒤 [윈도우즈 서비스] Console 실행 - #1 에서 설명한 방식대로 서비스를 등록해도 됩니다.
하지만 설치 파일을 만들면 이러한 작업을 자동으로 해주기 때문에 번거로운 작업을 하지 않아도 됩니다.
WIX를 이용하여 윈도우즈 서비스를 등록하는 방법을 알아보도록 하겠습니다.

How To: Install a Windows service

To install a Windows service, use the ServiceInstall element. Other configuration can be made using the ServiceControl element and the ServiceConfig element from WixUtilExtension.

Step 1: Install the service

The ServiceInstall element contains the basic information about the service to install. This element should be the child of a Component element whose key path is a sibling File element that specifies the service executable file.

Tip: to specify a system account, such as LocalService or NetworkService, use the prefix NT AUTHORITY.
For example, use NT AUTHORITY\LocalService as the Account attribute value to run the service under this account.

ServivceInstall 이라는 요소에 윈도우즈 서비스 정보를 입력합니다.
  • Start : 시작 유형
  • Account : 로그인 계정

Step 2: Configure the service (optional)

Using the util:ServiceConfig element from WixUtilExtension, you can configure how the service behaves if it fails. To use it, add WixUtilExtension to your project, add the the util namespace to your WiX authoring, and prefix the element name with the util prefix:
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">  
...  
<ServiceInstall>  
<util:ServiceConfig FirstFailureActionType="restart"  
SecondFailureActionType="restart"  
ThirdFailureActionType="restart" />  
</ServiceInstall>
아래는 WIX 예제 XML 파일입니다. 이 예제 파일을 본인의 상황에 맞게 수정하면 됩니다.
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="{2D00166E-A14A-4F24-B94F-3D5E9ED21D65}" Name="MyApp" Language="1033" Version="1.0.0.0" Manufacturer="MyCompany" UpgradeCode="{8F800905-91E8-4234-AD80-A485F156FE1B}">
	<Package InstallerVersion="400" Compressed="yes" InstallScope="perMachine" />
	
	<Media Id='1' Cabinet='MyAppCAB.cab' EmbedCab='yes' />
 
	<Directory Id='TARGETDIR' Name='SourceDir'>
  	<Directory Id='ProgramFilesFolder'>
    	<Directory Id='MyCompany' Name='MyCompany'>
      	<Directory Id='INSTALLDIR' Name='MyApp'>
        	<Component Id='ReadmeComponent' Guid='{2D00166E-AAAA-4F24-B94F-3D5E9ED21D65}'>
          	<File Id="Readme" Name="Readme.txt" DiskId="1" Source="Readme.txt"/>
        	</Component>
        	<Component Id='WindowsServicesComponent' Guid='{3D00166E-AAAA-4F24-B94F-3D5E9ED21D66}'>
          	<File Id="WindowsServicesSample.exe" 
          	Name="WindowsServicesSample.exe" 
          	DiskId="1" 
          	Source="WindowsServicesSample.exe"/>
          	
            <ServiceInstall 
          	Id="WindowsServicesSample" 
          	Name="WindowsServicesSample" 
          	DisplayName="WindowsServicesSample" 
          	Description="Example Windows Services" 
          	Start="auto" 
          	Type="ownProcess" 
          	ErrorControl="normal" 
          	Account="LocalSystem" 
          	Arguments="/ARGUMENTS LIST" 
          	Vital="yes" 
          	Interactive="no"/>
          	
            <ServiceControl Id="WindowsServicesSample" 
          	Name="WindowsServicesSample" 
          	Start="install" 
          	Stop="both" 
          	Remove="uninstall" 
          	Wait="yes"/>
          	
            <ServiceConfig 
          	ServiceName="WindowsServicesSample" 
          	OnInstall="yes" 
          	DelayedAutoStart="yes"/>
         	</Component>
      	</Directory>
    	</Directory>
  	</Directory>
	</Directory>
 
	<Feature Id='MyFeature' Title='My 1st Feature' Level='1'>
     	<ComponentRef Id='ReadmeComponent' />
     	<ComponentRef Id='WindowsServicesComponent' />
	</Feature>
 
  </Product>
</Wix>
ServiceInstall, ServiceControl의 Name 속성ServiceConfig의 ServiceName을 같게 설정해줘야 서비스 등록이 됩니다.

윈도우즈 서비스 등록 시 권한 문제로 실패한다면 사용자 권한 등록으로 해결할 수 있습니다.
권한 문제로 등록 실패

How to Verify That You Have Sufficient Privileges to Install System Services

Resolution

  1. Run gpedit.msc.
  2. Navigate to Computer Configuration > Windows Settings > Security Settings > Local Policies >User Rights Assignment.
  3. In the details pane, double-click Log on as a service.
  4. Click Add User or Group… and add the account to the list of accounts that have the Log on as a service right. Once you have selected the user, click OK.
  5. Click OK and close the policy editor.

Supported on

RAS RD Session Host Agent is supported on the following operating systems: 
  • Windows Server 2019
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2012
  • Windows Server 2008 R2
  • Windows Server 2008

댓글

이 블로그의 인기 게시물

80040154 오류로 인해 CLSID가 {xxxx-...}인 구성 요소의 COM 클래스 팩터리를 검색하지 못했습니다.

원문보기 .NET 으로 만든 응용프로그램에서 com 객체를 호출한 경우 Windows7 64bit 에서 제목과 같은 에러가 발생했다. Win32 COM 과 .NET 프로그램간의 호환성 때문에 생긴 문제였다. 원인은 .NET 실행시 JIT 컴파일러에 의해 최적화된 기계어로 변환되기 때문.. Win32 COM은 컴파일시.. Win32 COM에 맞춰 빌드 속성에서 하위버전으로 맞춰 컴파일을 다시하는 방법도 있지만 메인 프로젝트가 .NET이라면 참조되는 모든 프로젝트를 다 바꿔야할 노릇.. 또 다른 방법은 COM+를 이용하여 독립적으로 만드는 것이다. 분리시키는 방법은 아래 주소해서 확인할 수 있다. http://support.microsoft.com/kb/281335 나의 경우는 Win32 COM DLL을 64비트 .NET 프로그램에서 참조하니 COM 객체를 제대로 호출하지 못하였습니다. 그래서 .NET 프로그램의 Target Machine을 x86으로 설정하니 제대로 COM 객체를 호출하였습니다.

[Pyinstaller] 실행 파일 관리자 권한 획득하기

고객사에서 일부 사용자에게서 프로그램 오류가 발생한다며 아래와 같이 에러 캡처를 보내왔습니다. 프로그램에서 로그를 남기기 위해 로그 파일을 생성하는데 권한의 문제로 로그 파일을 생성하지 못해 프로그램 오류가 발생한 것 같습니다. 처음에는 Python 코드에서 관리자 권한을 요청하는 코드를 넣으려고 했는데, 실제로 Stackoverflow를 찾아보면 이런 내용이 나옵니다. 프로그램이 관리자 권한으로 실행되지 않았다면 관리자 권한으로 다시 프로그램을 실행시키는 코드입니다. import os import sys import win32com.shell.shell as shell ASADMIN = 'asadmin' if sys.argv[-1] != ASADMIN: script = os.path.abspath(sys.argv[0]) params = ' '.join([script] + sys.argv[1:] + [ASADMIN]) shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) sys.exit(0) 하지만 개인적으로 이런 방식은 마음에 들지 않았고 조금 더 찾아보니 Pyinstaller로 exe 파일을 만들 때 옵션을 설정하여 관리자 권한을 요청하도록 할 수 있다고 합니다. --uac-admin을 옵션에 추가하면 프로그램 실행 시 관리자 권한을 요청할 수 있습니다. pyinstaller.exe --uac-admin sample.py 하지만 안타깝게도 이 방식은 원하는 대로 동작하지 않았습니다. 마지막으로 manifest 파일을 이용하여 시도해보았습니다. spec 파일을 이용하여 pyinstaller로 빌드하면 <실행 파일 이름>.manifest 라는 파일이 생성됩니다. 파일에서 아랫부분을 찾아볼 수 있습니다. <security> <re

초간단 프로그램 락 걸기

프로그램에 락을 걸 일이 생겨났다. 하드웨어 락을 걸면 쉬울텐데 그 정도는 아니고 프로그램의 실행 날짜를 제한 해 달라고 한다. 그래서 파일(license.lic)을 가지고 락을 걸리고 결정을 했다. 요구 사항은 아래와 같다. 1. license.lic 파일이 없으면 프로그램을 실행 할수 없게 한다. 2. 지정한 날짜를 넘어서는 프로그램을 실행 할수 없게 한다. 3. 사용자가 시스템 날짜를 되돌렸을때 인식하여 프로그램을 실행 할수 없게 한다. 음.... 1.번 문제는 사용자가 프로그램을 실행하기 위해서 license.lic 파일을 받아야만 한다. license.lic 파일에는 최근 실행 날짜/종료날짜 이런식으로 적도록 한다.(물론 내용은 암호화 한다.) 최근 실행날짜는 프로그램이 실행때마다 업데이트 하도록 하고 시스템 날짜와 비교하여 시스템 날짜가 최근 실행 날짜보다 이전의 날짜면 시스템 날짜를 되돌렸다고 인식하도록 한다.(3.번 문제 해결) 시스템 날짜와 종료 날짜를 비교하여 시스템 날짜가 종료 날짜를 넘으면 프로그램을 실행 할수 없도록 한다.(2.번 문제 해결)