Пример #1
0
//
// Purpose: 
//   Entry point for the process
//
// Parameters:
//   None
// 
// Return value:
//   None
//
void __cdecl _tmain(int argc, TCHAR *argv[]) 
{ 
	// If command-line parameter is "install", install the service. 
	// Otherwise, the service is probably being started by the SCM.

	if( lstrcmpi( argv[1], TEXT("install")) == 0 )
	{
		SvcInstall();
		return;
	}

	if( lstrcmpi( argv[1], TEXT("uninstall")) == 0 )
	{
		SvcUninstall();
		return;
	}

	// TO_DO: Add any additional services for the process to this table.
	SERVICE_TABLE_ENTRY DispatchTable[] = 
	{ 
		{ SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain }, 
		{ NULL, NULL } 
	}; 

	// This call returns when the service has stopped. 
	// The process should simply terminate when the call returns.

	if (!StartServiceCtrlDispatcher( DispatchTable )) 
	{ 
		LbtReportFunctionError(TEXT("StartServiceCtrlDispatcher")); 
	}

	printf("Use \"install\" to install the service\n");
	printf("Use \"uninstall\" to uninstall the service\n");
} 
Пример #2
0
VOID wmain(int argc, wchar_t *argv[], wchar_t *envp[]) {
	if (argc > 1) {
		AccountName = argv[1];
		DebugOut("argv[1] = \"%S\"\n", argv[1]);
	}
	SERVICE_TABLE_ENTRY DispatchTable[] = {
		{ SERVICE_NAME, ServiceStart },
		{ NULL, NULL }
	};
	if (!StartServiceCtrlDispatcherW(DispatchTable)) {
		DWORD LastError = GetLastError();
		if (LastError == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
			for (;;) {
				printf("Service %S options:\n", SERVICE_NAME);
				printf("\t[1] - Install\n");
				printf("\t[2] - Uninstall\n");
				printf("\t[0] - Exit\n");
				printf("Your choose: ");
				char action = getchar();
				while (getchar() != '\n') {}
				switch (action) {
				case '1':
					SvcInstall();
					break;
				case '2':
					SvcUninstall();
					break;
				case '0':
					return;
				default:
					printf(">> Unrecongnized action\n");
					break;
				}
			}
		} else DebugOut(">> StartServiceCtrlDispatcherW failed! (LastError=0x%x)\n", LastError);
	}
}
Пример #3
0
int 
wmain(int argc, WCHAR **argv)
{
  // If command-line parameter is "install", install the service
  // or upgrade if already installed
  // If command line parameter is "forceinstall", install the service
  // even if it is older than what is already installed.
  // If command-line parameter is "upgrade", upgrade the service
  // but do not install it if it is not already installed.
  // If command line parameter is "uninstall", uninstall the service.
  // Otherwise, the service is probably being started by the SCM.
  bool forceInstall = !lstrcmpi(argv[1], L"forceinstall");
  if (!lstrcmpi(argv[1], L"install") || forceInstall) {
    WCHAR updatePath[MAX_PATH + 1];
    if (GetLogDirectoryPath(updatePath)) {
      LogInit(updatePath, L"maintenanceservice-install.log");
    }

    LOG(("Installing service"));
    SvcInstallAction action = InstallSvc;
    if (forceInstall) {
      action = ForceInstallSvc;
      LOG((" with force specified"));
    }
    LOG(("...\n"));

    if (!SvcInstall(action)) {
      LOG(("Could not install service (%d)\n", GetLastError()));
      LogFinish();
      return 1;
    }

    LOG(("The service was installed successfully\n"));
    LogFinish();
    return 0;
  } 

  if (!lstrcmpi(argv[1], L"upgrade")) {
    WCHAR updatePath[MAX_PATH + 1];
    if (GetLogDirectoryPath(updatePath)) {
      LogInit(updatePath, L"maintenanceservice-install.log");
    }
    LOG(("Upgrading service if installed...\n"));
    if (!SvcInstall(UpgradeSvc)) {
      LOG(("Could not upgrade service (%d)\n", GetLastError()));
      LogFinish();
      return 1;
    }

    LOG(("The service was upgraded successfully\n"));
    LogFinish();
    return 0;
  }

  if (!lstrcmpi(argv[1], L"uninstall")) {
    WCHAR updatePath[MAX_PATH + 1];
    if (GetLogDirectoryPath(updatePath)) {
      LogInit(updatePath, L"maintenanceservice-uninstall.log");
    }
    LOG(("Uninstalling service...\n"));
    if (!SvcUninstall()) {
      LOG(("Could not uninstall service (%d)\n", GetLastError()));
      LogFinish();
      return 1;
    }
    LOG(("The service was uninstalled successfully\n"));
    LogFinish();
    return 0;
  }

  SERVICE_TABLE_ENTRYW DispatchTable[] = { 
    { SVC_NAME, (LPSERVICE_MAIN_FUNCTIONW) SvcMain }, 
    { NULL, NULL } 
  }; 

  // This call returns when the service has stopped. 
  // The process should simply terminate when the call returns.
  if (!StartServiceCtrlDispatcherW(DispatchTable)) {
    LOG(("StartServiceCtrlDispatcher failed (%d)\n", GetLastError()));
  }

  return 0;
}
Пример #4
0
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND	- 处理应用程序菜单
//  WM_PAINT	- 绘制主窗口
//  WM_DESTROY	- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case MYWM_NOTIFYICON: 
		if(lParam==WM_RBUTTONDOWN){    
			HMENU hMenu = LoadMenuW(hInst,MAKEINTRESOURCEW(IDR_MENU1));
			HMENU hSubMenu = GetSubMenu(hMenu,0);
			POINT pos; 
			GetCursorPos(&pos);
			SetForegroundWindow(hWnd);
			TrackPopupMenu(hSubMenu,TPM_LEFTALIGN,pos.x,pos.y,0,hWnd,NULL);
			DestroyMenu(hMenu);
		}
	break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 分析菜单选择:
		switch (wmId)
		{
		case ID_EXIT:
			NOTIFYICONDATAW m_tnid;
			memset(&m_tnid,0,sizeof(m_tnid));

			//OnCreate 函数中return 之前添加托盘生成代码
			m_tnid.cbSize=sizeof(NOTIFYICONDATA); 
			m_tnid.hWnd=hWnd; 
			m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP; 
			m_tnid.uCallbackMessage=MYWM_NOTIFYICON;

			//用户定义的回调消息 
			wcscpy_s<128>(m_tnid.szTip,L"v-Judge-Kernel");
			m_tnid.uID=NULL;
			HICON hIcon; 
			hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON2)); 
			m_tnid.hIcon=hIcon; 
			Shell_NotifyIconW(NIM_DELETE,&m_tnid); 
			if(hIcon)
				DestroyIcon(hIcon);
			DestroyWindow(hWnd);
			break;
		case ID_UNINSTALL:
			if(IDYES == MessageBoxW(0,L"Are you sure to uninstall the service v-Judge_Kernel?",L"Warning!",MB_YESNO)){
				if(SvcUninstall())
					MessageBoxW(0,L"Successfully uninstall.",L"Message",MB_OK);
				else
					MessageBoxW(0,L"Unsuccessfully uninstall.",L"Message",MB_OK);
			}
			break;
		case ID_INSTALL:
			if(IDYES == MessageBoxW(0,L"Are you sure to install the service v-Judge_Kernel?",L"Warning!",MB_YESNO)){
				if(SvcInstall())
					MessageBoxW(0,L"Successfully install.",L"Message",MB_OK);
				else
					MessageBoxW(0,L"Unsuccessfully install.",L"Message",MB_OK);
			}
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: 在此添加任意绘图代码...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}