Click here to Skip to main content
15,887,676 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 5:03
UrbanBlues13-Aug-11 5:03 
AnswerRe: How to get Actual CFormView Size - MFC Vs2010 Pin
Richard MacCutchan13-Aug-11 6:37
mveRichard MacCutchan13-Aug-11 6:37 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 22:19
UrbanBlues13-Aug-11 22:19 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
Richard MacCutchan13-Aug-11 22:32
mveRichard MacCutchan13-Aug-11 22:32 
AnswerRe: How to get Actual CFormView Size - MFC Vs2010 Pin
«_Superman_»13-Aug-11 8:48
professional«_Superman_»13-Aug-11 8:48 
GeneralRe: How to get Actual CFormView Size - MFC Vs2010 Pin
UrbanBlues13-Aug-11 22:20
UrbanBlues13-Aug-11 22:20 
QuestionHeap corruption - maybe you can see more than me [modified] Pin
blackshadow313-Aug-11 3:19
blackshadow313-Aug-11 3:19 
QuestionCannot Interact with service using SERVICE_USER_DEFINED_CONTROL [SOLVED] Pin
vishalgpt13-Aug-11 2:16
vishalgpt13-Aug-11 2:16 
Firstly sorry for such a lengthy code.
I found this sample in codeproject. But when i try to run this. cmd.exe is not invoking.

CustomMessageSender is working ok. But LaunchAppIntoDifferentSession()fails in launching the cmd.exe. Have i missed something. I tried a lot but failed.



FileName : ServiceTwo

C++
#pragma comment (lib,"WtsApi32.lib")
#pragma comment (lib,"UserEnv.lib")

#include <iostream>
#include <Windows.h>
#include <WinSvc.h>
#include <WtsApi32.h>
#include <UserEnv.h>
#include <TlHelp32.h>

//CUSTOM MESSAGE FOR SERVICE TO LAUNCH AN APP INTO SESSION 1
#define SERVICE_CONTROL_CUSTOM_MESSAGE 0x095

//ADD CUSTOM METHOD TO LAUNCH
BOOL LaunchAppIntoDifferentSession(LPWSTR appName, LPPROCESS_INFORMATION pi);

SERVICE_STATUS				m_ServiceStatus;
SERVICE_STATUS_HANDLE		m_ServiceStatusHandle;

BOOL bRunning				=TRUE;

VOID WINAPI ServiceControlHandler(DWORD Opcode);


using namespace std;


VOID WINAPI ServiceControlHandler(DWORD Opcode)
{
	PROCESS_INFORMATION pi;
	LPWSTR appName = L"cmd.exe";
	switch(Opcode)
	{
	case SERVICE_CONTROL_CUSTOM_MESSAGE:
		LaunchAppIntoDifferentSession(appName, &pi);
		break;
	case SERVICE_CONTROL_PAUSE:
		m_ServiceStatus.dwCurrentState	= SERVICE_PAUSED;
		break;
	case SERVICE_CONTROL_CONTINUE:
		m_ServiceStatus.dwCurrentState	= SERVICE_RUNNING;
		break;
	case SERVICE_CONTROL_STOP:
		m_ServiceStatus.dwWin32ExitCode	= 0;
		m_ServiceStatus.dwCurrentState	= SERVICE_STOPPED;
		m_ServiceStatus.dwCheckPoint	= 0;
		m_ServiceStatus.dwWaitHint		= 0;

		SetServiceStatus(m_ServiceStatusHandle, &m_ServiceStatus);
		bRunning = FALSE;
		break;
	case SERVICE_CONTROL_INTERROGATE:
		break;
	return;
}

BOOL LaunchAppIntoDifferentSession(LPWSTR appName,LPPROCESS_INFORMATION pi)
{
	//PROCESS_INFORMATION pi;
	STARTUPINFO si;
	BOOL bResult = FALSE;
	DWORD dwSessionID = 0, winLogonPid = 0, winLogonSessionID = 0, dwCreationFlags;
	HANDLE hUserTokenDup, hPToken, hProcess;

	dwSessionID = WTSGetActiveConsoleSessionId();

	PROCESSENTRY32 procEntry;

	HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
	if (hSnap == INVALID_HANDLE_VALUE)return 1;

	procEntry.dwSize = sizeof(PROCESSENTRY32);

	if (!Process32First(hSnap, &procEntry)) return 1;

	do 
	{
		if (wcscmp(procEntry.szExeFile,L"winlogon.exe") == 0)
		{
			ProcessIdToSessionId(procEntry.th32ProcessID, &winLogonSessionID);
			if (winLogonSessionID == dwSessionID)
			{
				winLogonPid = procEntry.th32ProcessID;
			}
		}
	} while (Process32Next(hSnap, &procEntry));

	hProcess = OpenProcess(MAXIMUM_ALLOWED,false,winLogonPid);

	if (!OpenProcessToken(hProcess,TOKEN_DUPLICATE,&hPToken))
	{
		CloseHandle(hProcess);
		return false;
	}

	if (!DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,&hUserTokenDup))
	{
		CloseHandle(hProcess);
		CloseHandle(hPToken);
	}

	ZeroMemory(&si,sizeof(STARTUPINFO));
	si.cb = sizeof(STARTUPINFO);
	si.lpDesktop = L"winsta0\\default";

	dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;

	//bResult = CreateProcessAsUser(hUserTokenDup,NULL,L"cmd.exe",NULL,NULL,FALSE,dwCreationFlags,NULL,NULL,&si,&pi);
	bResult = CreateProcessAsUser(hUserTokenDup,NULL,appName,NULL,NULL,FALSE,dwCreationFlags,NULL,NULL,&si,pi);

	CloseHandle(hProcess);
	CloseHandle(hPToken);
	CloseHandle(hUserTokenDup);

	return bResult;
}


Debuged and found Error is in LaunchAppIntoDifferentSession() only. Now winlogon.exe no more runs in session 0.
Regards,
Vishal
modified on Saturday, August 13, 2011 10:05 PM

AnswerRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 2:21
mveRichard MacCutchan13-Aug-11 2:21 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 2:40
vishalgpt13-Aug-11 2:40 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 4:36
mveRichard MacCutchan13-Aug-11 4:36 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 4:59
vishalgpt13-Aug-11 4:59 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
Richard MacCutchan13-Aug-11 5:56
mveRichard MacCutchan13-Aug-11 5:56 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 14:30
vishalgpt13-Aug-11 14:30 
AnswerRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL Pin
vishalgpt13-Aug-11 16:09
vishalgpt13-Aug-11 16:09 
GeneralRe: Cannot Interact with service using SERVICE_USER_DEFINED_CONTROL[SOLVED] Pin
vishalgpt13-Aug-11 16:04
vishalgpt13-Aug-11 16:04 
QuestionBitmap on Picture Control Pin
manju 312-Aug-11 21:18
manju 312-Aug-11 21:18 
AnswerRe: Bitmap on Picture Control Pin
Richard MacCutchan13-Aug-11 0:43
mveRichard MacCutchan13-Aug-11 0:43 
QuestionRe-intilize stack pointer Pin
ForNow12-Aug-11 9:41
ForNow12-Aug-11 9:41 
AnswerRe: Re-intilize stack pointer Pin
MicroVirus12-Aug-11 10:42
MicroVirus12-Aug-11 10:42 
AnswerRe: Re-intilize stack pointer Pin
Richard MacCutchan12-Aug-11 23:00
mveRichard MacCutchan12-Aug-11 23:00 
QuestionAbout wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
frankgt4012-Aug-11 7:31
frankgt4012-Aug-11 7:31 
AnswerRe: About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
Maximilien12-Aug-11 8:01
Maximilien12-Aug-11 8:01 
GeneralRe: About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
frankgt4012-Aug-11 16:56
frankgt4012-Aug-11 16:56 
AnswerRe: About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work? Pin
MicroVirus12-Aug-11 8:25
MicroVirus12-Aug-11 8:25 

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.