本人正在做一个系统安装程序,因各种原因不能使用VC自带的制作软件,现在遇到几个问题,在网上没有找到解决方法(其实是不想找了),特来请教一下:
    1、如何获取系统目录,也就是当前windows操作系统安装的目录
    2、如何在“添加删除程序”中显示我安装的程序,并能顺利卸载。
    3、如何启停“服务”中的某一项服务,如SQL Server    谢谢大家了!!!

解决方案 »

  1.   

    1 SHGetSpecialPath
    3 net stop
      

  2.   

    1)
    UINT GetWindowsDirectory(
      LPTSTR lpBuffer,  // buffer for Windows directory
      UINT uSize        // size of directory buffer
    );
      

  3.   

    void CPhoneSvrManageDlg::OnOpStartsvr() 
    {
    // TODO: Add your command handler code here
    SC_HANDLE  sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if(sh == NULL)
    {
    ErrorMessageBox();
    return;
    } SC_HANDLE  ss = OpenService(sh, "GameNicePhoneSvr", SERVICE_ALL_ACCESS); if(ss == NULL)
    {
    CloseServiceHandle(sh);
    ErrorMessageBox();
    return; } if(!StartService(ss, 0, NULL))
    {
    ErrorMessageBox();
    } CloseServiceHandle(ss);
    CloseServiceHandle(sh);
    }void CPhoneSvrManageDlg::OnOpStop() 
    {
    // TODO: Add your command handler code here
    SC_HANDLE  sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if(sh == NULL)
    {
    ErrorMessageBox();
    return;
    } SC_HANDLE  ss = OpenService(sh, "GameNicePhoneSvr", SERVICE_ALL_ACCESS); if(ss == NULL)
    {
    CloseServiceHandle(sh);
    ErrorMessageBox();
    return; } SERVICE_STATUS ssStatus; if(!ControlService(ss, SERVICE_CONTROL_STOP, &ssStatus))
    {
    ErrorMessageBox();
    } Sleep(100);    while( QueryServiceStatus( ss, &ssStatus ) )
        {
            if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
            {
                Sleep( 100 );
            }
            else
                break;
        }    if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
    {
    }else
    {
    MessageBox("无法停止服务!", "Error!", MB_OK|MB_ICONSTOP);
    } CloseServiceHandle(ss);
    CloseServiceHandle(sh);

    }
      

  4.   

    1、如何获取系统目录,也就是当前windows操作系统安装的目录
    几个相关的API
    GetWindowsDirectory
    GetSystemDirectory
    GetCurrentDirectory
    SHGetSpecialPath    2、如何在“添加删除程序”中显示我安装的程序,并能顺利卸载。添加删除程序主要是注册表中的一些项目,你可以找个已安装的软件,然后在注册表中搜索这些键的位置。一般应在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall    3、如何启停“服务”中的某一项服务,如SQL Server使用服务管理器APIChangeServiceConfig Changes the configuration parameters of a service. 
    ChangeServiceConfig2 Changes the optional configuration parameters of a service. 
    CloseServiceHandle Closes the specified handle to a service control manager object or a service object.  
    ControlService Sends a control code to a service. 
    CreateService Creates a service object and adds it to the specified service control manager database. 
    DeleteService Marks the specified service for deletion from the service control manager database. 
    EnumDependentServices Retrieves the name and status of each service that depends on the specified service. 
    EnumServicesStatus Enumerates services in the specified service control manager database. 
    EnumServicesStatusEx Enumerates services in the specified service control manager database based on the specified information level. 
    GetServiceDisplayName Retrieves the display name of the specified service. 
    GetServiceKeyName Retrieves the service name of the specified service. 
    Handler An application-defined callback function used with the RegisterServiceCtrlHandler function. 
    HandlerEx An application-defined callback function used with the RegisterServiceCtrlHandlerEx function. 
    LockServiceDatabase Requests ownership of the service control manager database lock. 
    NotifyBootConfigStatus Reports the boot status to the service control manager. 
    OpenSCManager Establishes a connection to the service control manager on the specified computer and opens the specified service control manager database. 
    OpenService Opens an existing service. 
    QueryServiceConfig Retrieves the configuration parameters of the specified service. 
    QueryServiceConfig2 Retrieves the optional configuration parameters of the specified service. 
    QueryServiceLockStatus Retrieves the lock status of the specified service control manager database. 
    QueryServiceObjectSecurity Retrieves a copy of the security descriptor associated with a service object. 
    QueryServiceStatus Retrieves the current status of the specified service. 
    QueryServiceStatusEx Retrieves the current status of the specified service based on the specified information level. 
    RegisterServiceCtrlHandler Register a function to handle service control requests for an application. 
    RegisterServiceCtrlHandlerEx Register a function to handle service control requests for an application. 
    ServiceMain An application-defined function that serves as the entry point for a service. 
    SetServiceBits Registers a service type with the service control manager and the Server service. 
    SetServiceObjectSecurity Sets the security descriptor of a service object. 
    SetServiceStatus Updates the service control manager's status information for the calling service. 
    StartService Starts a service. 
    StartServiceCtrlDispatcher Connects the main thread of a service process to the service control manager. 
    UnlockServiceDatabase 
      

  5.   

    1 获得系统目录:
       char sysDir[MAX_PATH];
      ::GetSystemDirectory(sysDir, MAX_PATH);
    2 在
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
    下新增键
      

  6.   

    3 看下面这篇文章
    http://blog.vckbase.com/star/archive/2005/04/30/5085.html
      

  7.   

    对于 1 还有办法是GetWindowsDirectory或者
    从注册表中读取,位置: 
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion 
    的SystemRoot键,取得如:C:\WINDOWS 
      

  8.   


    UINT GetWindowsDirectory(
      LPTSTR lpBuffer,
      UINT uSize
    );2 SHELL有一个接口,可能对你有用
    IShellApp Interface3 
    BOOL ControlService(
      SC_HANDLE hService,
      DWORD dwControl,
      LPSERVICE_STATUS lpServiceStatus
    );
    另外,MSDN里面有个系列文档,专门介绍制作安装程序的,地址为
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/msi/setup/windows_installer_start_page.htm
      

  9.   

    创建一个服务:
    SC_HANDLE CreateService(
      SC_HANDLE hSCManager,
      LPCTSTR lpServiceName,
      LPCTSTR lpDisplayName,
      DWORD dwDesiredAccess,
      DWORD dwServiceType,
      DWORD dwStartType,
      DWORD dwErrorControl,
      LPCTSTR lpBinaryPathName,
      LPCTSTR lpLoadOrderGroup,
      LPDWORD lpdwTagId,
      LPCTSTR lpDependencies,
      LPCTSTR lpServiceStartName,
      LPCTSTR lpPassword
    );删除一个服务:
    BOOL DeleteService(
      SC_HANDLE hService
    );