Click here to Skip to main content
15,894,955 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: For loop Pin
Randor 2-Oct-23 5:47
professional Randor 2-Oct-23 5:47 
GeneralRe: For loop Pin
trønderen2-Oct-23 8:01
trønderen2-Oct-23 8:01 
GeneralRe: For loop Pin
Randor 2-Oct-23 8:27
professional Randor 2-Oct-23 8:27 
GeneralRe: For loop Pin
trønderen2-Oct-23 12:17
trønderen2-Oct-23 12:17 
GeneralRe: For loop Pin
Randor 2-Oct-23 12:54
professional Randor 2-Oct-23 12:54 
GeneralRe: For loop Pin
trønderen2-Oct-23 21:11
trønderen2-Oct-23 21:11 
GeneralRe: For loop Pin
Randor 3-Oct-23 0:08
professional Randor 3-Oct-23 0:08 
GeneralRe: For loop Pin
trønderen3-Oct-23 9:54
trønderen3-Oct-23 9:54 
GeneralRe: For loop Pin
jschell3-Oct-23 4:35
jschell3-Oct-23 4:35 
GeneralRe: For loop Pin
k505429-Sep-23 11:55
mvek505429-Sep-23 11:55 
GeneralRe: For loop Pin
Randor 29-Sep-23 12:14
professional Randor 29-Sep-23 12:14 
GeneralRe: For loop Pin
honey the codewitch3-Oct-23 12:32
mvahoney the codewitch3-Oct-23 12:32 
AnswerRe: For loop Pin
Dave Kreskowiak25-Sep-23 8:53
mveDave Kreskowiak25-Sep-23 8:53 
GeneralRe: For loop Pin
Calin Negru25-Sep-23 22:40
Calin Negru25-Sep-23 22:40 
GeneralRe: For loop Pin
Richard Andrew x6428-Sep-23 12:12
professionalRichard Andrew x6428-Sep-23 12:12 
GeneralRe: For loop Pin
Dave Kreskowiak28-Sep-23 12:20
mveDave Kreskowiak28-Sep-23 12:20 
GeneralRe: For loop Pin
harold aptroot26-Sep-23 15:15
harold aptroot26-Sep-23 15:15 
GeneralRe: For loop Pin
Calin Negru26-Sep-23 23:09
Calin Negru26-Sep-23 23:09 
GeneralRe: For loop Pin
harold aptroot5-Oct-23 21:32
harold aptroot5-Oct-23 21:32 
QuestionService not created correctly under windows 11 but any older version. Pin
Rick R. 202323-Sep-23 14:54
Rick R. 202323-Sep-23 14:54 
I'm using the following function for years and used it under any Version of Windows since XP. It creates a systemtask with the start type set to "auto", just like it is expected to do. But under windows 11, the start type of the installed service always defaults to "manual" when being created. Any help solving this is very much appreciated. Am using VS2022 using toolset 1.41_XP (for reasons)
(already added some extra check for win 11... but still the start type defaults to manual in the newly created task)

static int manage_service(int action) {
	SC_HANDLE hSCM = NULL, hService = NULL;
	SERVICE_DESCRIPTION descr = { server_name };
	char path[PATH_MAX + 20]; // Path to executable plus magic argument
	int success = 1;

	GetModuleFileName(NULL, path, sizeof(path));
	strncat(path, " ", sizeof(path));
	strncat(path, service_magic_argument, sizeof(path));

	if (IsRunAsAdministrator()) {
		if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
			GENERIC_WRITE : GENERIC_READ)) == NULL) {
			success = 0;
			show_error();
		}
		else if (action == ID_INSTALL_SERVICE) {
			hService = CreateService(hSCM, service_name, service_name,
				SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
				SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
				path, NULL, NULL, NULL, NULL, NULL);
			if (hService) {
				ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
					SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
				ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);

				// Check Windows version
				OSVERSIONINFOEX osvi;
				ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
				osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
				osvi.dwMajorVersion = 11; // Windows 11

				if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
					// Set start type to AUTO_START for Windows 11
					ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
						SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
				}
			}
			else {
				show_error();
			}
		}
		else if (action == ID_REMOVE_SERVICE) {
			if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
				!DeleteService(hService)) {
				show_error();
			}
		}
		else if ((hService = OpenService(hSCM, service_name,
			SERVICE_QUERY_STATUS)) == NULL) {
			success = 0;
		}

		CloseServiceHandle(hService);
		CloseServiceHandle(hSCM);
	}
	else {
		if (action == ID_INSTALL_SERVICE) {
			RunServiceAsAdmin('I', path, service_name);
		}
		else if (action == ID_REMOVE_SERVICE) {
			RunServiceAsAdmin('R', path, service_name);
		}
		else {
			if ((hSCM = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL) {
				success = 0;
				show_error();
			}
			if ((hService = OpenService(hSCM, service_name,
				SERVICE_QUERY_STATUS)) == NULL) {
				success = 0;
			}

			CloseServiceHandle(hService);
			CloseServiceHandle(hSCM);
		}
	}

	return success;
}

AnswerRe: Service not created correctly under windows 11 but any older version. Pin
Randor 23-Sep-23 10:43
professional Randor 23-Sep-23 10:43 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Rick R. 202323-Sep-23 12:13
Rick R. 202323-Sep-23 12:13 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Randor 23-Sep-23 12:22
professional Randor 23-Sep-23 12:22 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Rick R. 202323-Sep-23 12:24
Rick R. 202323-Sep-23 12:24 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Randor 23-Sep-23 12:53
professional Randor 23-Sep-23 12:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.