I don't know french but hopefully you know C++
This code allows process to login under defined administrator account:
// autoupdatetool.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "autoupdatetool.h"
#include "process.h"
#include "utilities.h"
#include "errno.h"
#include "windows.h"
#include "ProcessWithLogon.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#if WINVER < 0x0500
#pragma comment(lib, "AdvAPI32.lib")
#error Incorrect version of windows.h being included, check the warning message
#pragma message ("**************************************************************")
#pragma message ("************************WARNING!!*****************************")
#pragma message ("**************************************************************")
#pragma message ("** **")
#pragma message ("** This application uses functions defined in the Windows **")
#pragma message ("** Platform SDK that only work under Windows 2000 or XP. **")
#pragma message ("** **")
#pragma message ("** Building this EXE on a non 2000/XP platform will limit **")
#pragma message ("** the intended functionality of the application. **")
#pragma message ("** **")
#pragma message ("** The Windows Platform SDK must be installed locally in **")
#pragma message ("** order to build the EXE. **")
#pragma message ("** **")
#pragma message ("**************************************************************")
#pragma message ("************************WARNING!!*****************************")
#pragma message ("**************************************************************")
#endif
/////////////////////////////////////////////////////////////////////////////
// CAutoupdatetoolApp
BEGIN_MESSAGE_MAP(CAutoupdatetoolApp, CWinApp)
//{{AFX_MSG_MAP(CAutoupdatetoolApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoupdatetoolApp construction
CAutoupdatetoolApp::CAutoupdatetoolApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CAutoupdatetoolApp object
CAutoupdatetoolApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CAutoupdatetoolApp initialization
BOOL CAutoupdatetoolApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
char *args[4];
char TempPath[100];
int errorvalue;
LPVOID lpMsgBuf;
CString msg;
CString newUserName = Pathinfo::DOCACL_USERNAME;
CString password = Pathinfo::DOCACL_PASSWD;
CString applicationName = Pathinfo::DOCACL_PATH;
CString LocalDomainName;
CString WindowsSystemDirectory;
CString DevFile = "\\regsvr32.exe\" -s \"developer_file_loadsupport.dll\"";
CString BannerSupport = "\\regsvr32.exe\" -s \"bannersupport.dll\"";
DWORD errorCode, MaxBufSize = 1024;
char buf[1025];
char Windows_System_Directory[MAX_PATH+1];
strcpy(TempPath,Pathinfo::TMS_EXE_PATH);
args[0] = TempPath;
args[1] = "exec??";
args[2] = "two";
args[3] = NULL;
CAutoupdatetoolDlg dlg;
dlg.DoDownloadNow();
GetSystemDirectory(Windows_System_Directory, MAX_PATH);
WindowsSystemDirectory = Windows_System_Directory;
WindowsSystemDirectory.Insert(0, "\"");
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#if(WINVER >= 0x0500)
#define BUFSIZE 1024
try
{
ProcessWithLogon CreateProcessWithLogin;
try
{
if(GetComputerName(buf, &MaxBufSize) == 0)
throw int (errorCode = GetLastError());
LocalDomainName = buf;
CreateProcessWithLogin.Login(newUserName, password, LocalDomainName);
CreateProcessWithLogin.StartProcessAndWaitForCompletion(applicationName);
applicationName = WindowsSystemDirectory + DevFile;
CreateProcessWithLogin.StartProcessAndWaitForCompletion(applicationName);
applicationName = WindowsSystemDirectory + BannerSupport;
CreateProcessWithLogin.StartProcessAndWaitForCompletion(applicationName);
}
catch (int)
{
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf, 0, NULL );
MessageBox(NULL, (LPCTSTR)lpMsgBuf, "'AutoUpdate' Execution Error", MB_ICONSTOP );
LocalFree(lpMsgBuf);
}
}
catch (CMemoryException)
{
AfxMessageBox("AutoUpdate reported a memory exception.", MB_ICONSTOP, 0);
}
#endif // WINVER >= 0x0500
errorvalue = _execv(Pathinfo::TMS_EXE_PATH,args);
switch(errno)
{
case E2BIG:
msg=CLib::getMessage(kE2BIG);
break;
case EACCES:
msg=CLib::getMessage(kEACCES);
break;
case ENOENT:
msg=CLib::getMessage(kENOENT);
break;
case EMFILE:
msg=CLib::getMessage(kEMFILE);
break;
case ENOEXEC:
msg=CLib::getMessage(kENOEXEC);
break;
case ENOMEM:
msg=CLib::getMessage(kENOMEM);
break;
}
AfxMessageBox(msg,MB_ICONSTOP|MB_OK,0);
return FALSE;
}
// ProcessWithLogon.cpp : implementation file
//
#include "stdafx.h"
#include "autoupdatetool.h"
#include "ProcessWithLogon.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ProcessWithLogon
ProcessWithLogon::ProcessWithLogon()
{
m_LoginFlags = 0;
m_CreationFlags = CREATE_UNICODE_ENVIRONMENT;
m_CurrentDirectory = NULL;
m_Environment = NULL;
m_NewUser = false;
m_UserName = NULL;
m_PassWord = NULL;
m_Domain = NULL;
m_ApplicationName = NULL;
m_CommandLine = NULL;
}
ProcessWithLogon::~ProcessWithLogon()
{
if(m_UserName)
SysFreeString(m_UserName);
if(m_PassWord)
SysFreeString(m_PassWord);
if(m_Domain)
SysFreeString(m_Domain);
if(m_ApplicationName)
SysFreeString(m_ApplicationName);
if(m_CommandLine)
SysFreeString(m_CommandLine);
}
HRESULT ProcessWithLogon::Login(CString & UserName, CString & Password, CString & Domain)
{
HRESULT rtn = S_OK;
if(m_NewUser)
{
SysFreeString(m_UserName);
SysFreeString(m_PassWord);
SysFreeString(m_Domain);
}
try
{
m_UserName = UserName.AllocSysString();
m_PassWord = Password.AllocSysString();
m_Domain = Domain.AllocSysString();
m_NewUser = true;
}
catch(CMemoryException)
{
rtn = E_OUTOFMEMORY;
}
return(rtn);
}
HRESULT ProcessWithLogon::StartProcessAndWaitForCompletion(CString & Process)
{
DWORD lpExitCode;
HRESULT rtn = S_OK;
LPVOID lpMsgBuf;
STARTUPINFOW StartupInfo;
PROCESS_INFORMATION ProcessInfo;
m_ApplicationName = NULL;
try
{
m_ApplicationName = Process.AllocSysString();
::ZeroMemory(&StartupInfo, sizeof (StartupInfo));
::ZeroMemory(&ProcessInfo, sizeof (ProcessInfo));
try
{
if(CreateProcessWithLogonW( (LPCWSTR) m_UserName, (LPCWSTR) m_Domain,
(LPCWSTR) m_PassWord, m_LoginFlags,
NULL, m_ApplicationName,
m_CreationFlags, m_Environment,
m_CurrentDirectory, &StartupInfo,
&ProcessInfo) == 0)
throw int( );
GetExitCodeProcess(ProcessInfo.hProcess, &lpExitCode);
while(lpExitCode == STILL_ACTIVE)