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

C / C++ / MFC

 
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 
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 
Here is some more complete code:

BOOL IsRunAsAdministrator()
{
    BOOL isRunAsAdmin = FALSE;
    DWORD dwError = ERROR_SUCCESS;
    PSID pAdministratorsGroup = NULL;

    // Allocate and initialize a SID of the administrators group.
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    if (!AllocateAndInitializeSid(
        &NtAuthority, 
        2, 
        SECURITY_BUILTIN_DOMAIN_RID, 
        DOMAIN_ALIAS_RID_ADMINS, 
        0, 0, 0, 0, 0, 0, 
        &pAdministratorsGroup))
    {
        goto Cleanup;
    }

    // Determine whether the SID of administrators group is enabled in 
    // the primary access token of the process.
    if (!CheckTokenMembership(NULL, pAdministratorsGroup, &isRunAsAdmin))
    {
        goto Cleanup;
    }

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (pAdministratorsGroup)
    {
        FreeSid(pAdministratorsGroup);
        pAdministratorsGroup = NULL;
    }

    return isRunAsAdmin;
}

void RunServiceAsAdmin(char ch, const char *program, const char* name)
{
	// Launch itself as admin
	char param[255];
	SHELLEXECUTEINFO sei = { sizeof(sei) };

	memset(param, 0 , sizeof(param));
	sei.lpVerb = "runas";
	sei.lpFile = "sc.exe";
	sei.hwnd = NULL;
	sei.nShow = SW_NORMAL;

	if(ch == 'I')
	{
		sprintf(param, "create \"%s\" binPath= \"%s\" DisplayName=\"%s\"", name, program, name);
	}
	else
	{
		sprintf(param, "delete \"%s\"", name);
	}

	sei.lpParameters = param;

	if (!ShellExecuteEx(&sei))
	{
		show_error();
	}
}

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;
}

static LONG queryServiceStatus(const char* serviceName){
	SC_HANDLE hSCM = NULL, hService = NULL;
	SERVICE_STATUS_PROCESS ssStatus; 
	DWORD dwBytesNeeded;
	LONG status = 0;
	if ((hSCM = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL) {
		show_error();
		return 0;
	} 
			
	// Get a handle to the service.
    hService = OpenService( 
        hSCM,         // SCM database 
        serviceName,            // name of service 
        SERVICE_QUERY_STATUS);  // full access 
 
    if (hService == NULL)
    {
        goto END_QUERY;
    }    

    // Check the status in case the service is not stopped. 
    if (!QueryServiceStatusEx( 
            hService,                     // handle to service 
            SC_STATUS_PROCESS_INFO,         // information level
            (LPBYTE) &ssStatus,             // address of structure
            sizeof(SERVICE_STATUS_PROCESS), // size of structure
            &dwBytesNeeded ) )              // size needed if buffer is too small
    {
        goto END_QUERY;
    }

    // Check if the service is already running. It would be possible 
    // to stop the service here, but for simplicity this example just returns. 

    if(ssStatus.dwCurrentState == SERVICE_RUNNING)
    {
       status = SERVICE_RUNNING;
	   goto END_QUERY;
    }


modified 23-Sep-23 18:31pm.

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 
GeneralRe: How to get disk model and serial number for the disk Windows is installed on Pin
Valentinor19-Sep-23 22:15
Valentinor19-Sep-23 22:15 
GeneralRe: How to get disk model and serial number for the disk Windows is installed on Pin
Richard MacCutchan19-Sep-23 22:26
mveRichard MacCutchan19-Sep-23 22:26 
AnswerRe: How to get disk model and serial number for the disk Windows is installed on Pin
Richard MacCutchan19-Sep-23 22:28
mveRichard MacCutchan19-Sep-23 22:28 
AnswerRe: How to get disk model and serial number for the disk Windows is installed on Pin
JohnCodding19-Sep-23 22:57
JohnCodding19-Sep-23 22:57 

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.