// SpawnProcess.cpp : Defines the entry point for the DLL application.
//
//#include "stdafx.h"
#include
#include
#include
#include
#include
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
BOOL APIENTRY SpawnProcessInNTDesktop(LPSTR exeName,LPSTR parameters,LPSTR
currDirectory)
{
DWORD dwThreadId;
HWINSTA hwinstaSave;
HDESK hdeskSave;
HWINSTA hwinstaUser;
HDESK hdeskUser;
RPC_BINDING_HANDLE h = NULL;
char buffer[256];
char desktopName[80];
STARTUPINFO startInfo;
PROCESS_INFORMATION processInfo;
// Ensure connection to service window station and desktop, and
// save their handles.
hwinstaSave = GetProcessWindowStation();
dwThreadId = GetCurrentThreadId();
hdeskSave = GetThreadDesktop(dwThreadId);
// Impersonate the client and connect to the User's
// window station and desktop.
RpcImpersonateClient(h);
hwinstaUser = OpenWindowStation("WinSta0", TRUE, MAXIMUM_ALLOWED);
if (hwinstaUser == NULL)
{
RpcRevertToSelf();
return 0;
}
SetProcessWindowStation(hwinstaUser);
hdeskUser = OpenDesktop("Default", 0, TRUE, MAXIMUM_ALLOWED);
RpcRevertToSelf();
if (hdeskUser == NULL)
{
SetProcessWindowStation(hwinstaSave);
CloseWindowStation(hwinstaUser);
return 0;
}
SetThreadDesktop(hdeskUser);
//Use CreateProcess to spawn process
//
lstrcpy(desktopName,"WinSta0\\Default");
memset(&startInfo,0,sizeof startInfo);
startInfo.cb = sizeof startInfo;
startInfo.lpDesktop = desktopName;
wsprintf(buffer,"%s %s",exeName, parameters);
if(!CreateProcess(NULL,
buffer,
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW|CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS,
NULL,
currDirectory,
&startInfo,
&processInfo))
{
LPVOID lpMsgBuf;
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 );
LocalFree( lpMsgBuf );
return 0;
}
//
SetThreadDesktop(hdeskSave);
SetProcessWindowStation(hwinstaSave);
CloseDesktop(hdeskUser);
CloseWindowStation(hwinstaUser);
return TRUE;
}
|