Click here to Skip to main content
15,877,618 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Rick R. 202323-Sep-23 13:06
Rick R. 202323-Sep-23 13:06 
QuestionRe: Service not created correctly under windows 11 but any older version. Pin
Randor 23-Sep-23 15:57
professional Randor 23-Sep-23 15:57 
AnswerRe: Service not created correctly under windows 11 but any older version. Pin
Rick R. 202324-Sep-23 6:37
Rick R. 202324-Sep-23 6:37 
GeneralRe: Service not created correctly under windows 11 but any older version. Pin
Randor 24-Sep-23 7:08
professional Randor 24-Sep-23 7:08 
Questionfingerprint sensor code with c++ Pin
ibiere21-Sep-23 23:56
ibiere21-Sep-23 23:56 
AnswerRe: fingerprint sensor code with c++ Pin
CPallini22-Sep-23 0:52
mveCPallini22-Sep-23 0:52 
QuestionType of array and printf specifiers Pin
Member 114540620-Sep-23 4:40
Member 114540620-Sep-23 4:40 
AnswerRe: Type of array and printf specifiers Pin
Mircea Neacsu20-Sep-23 5:11
Mircea Neacsu20-Sep-23 5:11 
AnswerRe: Type of array and printf specifiers Pin
k505420-Sep-23 5:36
mvek505420-Sep-23 5:36 
AnswerRe: Type of array and printf specifiers Pin
CPallini20-Sep-23 5:51
mveCPallini20-Sep-23 5:51 
QuestionHow to get disk model and serial number for the disk Windows is installed on Pin
JohnCodding19-Sep-23 20:41
JohnCodding19-Sep-23 20:41 
AnswerRe: How to get disk model and serial number for the disk Windows is installed on Pin
Richard MacCutchan19-Sep-23 21:59
mveRichard MacCutchan19-Sep-23 21:59 

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.