VC 如何调用应用程序?

dg393624170 2012-02-29 10:40:11
问题:

开机自动运行的服务程序(无界面)中,调用一个应用程序(XXX.exe)?

如果可以的话,给个函数吧?
...全文
187 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxingway 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chenjintaoxp 的回复:]

这是MFC的一段话:

Interactive Services
An interactive service is a service that can interact with the input desktop. Other desktops do not receive user input. For more information, see Window Stations a……
[/Quote]
楼主那个服务是SERVICE_INTERACTIVE_PROCESS类型的吗?可以用ShellExecute或CreateProcess调用应用程序呀
dg393624170 2012-03-01
  • 打赏
  • 举报
回复
提供一段代码,大家帮我解释下,谢谢??
可不可以使用传一个静安装参数?


BOOLLaunchAppIntoDifferentSession(const char *appname)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL bResult = FALSE;
DWORD dwSessionId,winlogonPid;
HANDLE hUserToken,hUserTokenDup,hPToken,hProcess;
DWORD dwCreationFlags;

// Log the client on to the local computer.
dwSessionId = WTSGetActiveConsoleSessionId();

//////////////////////////////////////////
// Find the winlogon process
////////////////////////////////////////

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 (_stricmp(procEntry.szExeFile, "winlogon.exe") == 0)
{
// We found a winlogon process...
// make sure it's running in the console session
DWORD winlogonSessId = 0;
if (ProcessIdToSessionId(procEntry.th32ProcessID, &winlogonSessId)
&& winlogonSessId == dwSessionId)
{
winlogonPid = procEntry.th32ProcessID;
break;
}
}

} while (Process32Next(hSnap, &procEntry));

////////////////////////////////////////////////////////////////////////

WTSQueryUserToken(dwSessionId,&hUserToken);
dwCreationFlags = NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb= sizeof(STARTUPINFO);
si.lpDesktop = "winsta0\\default";
ZeroMemory(&pi, sizeof(pi));
TOKEN_PRIVILEGES tp;
LUID luid;
hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,winlogonPid);

if(!::OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY
|TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID
|TOKEN_READ|TOKEN_WRITE,&hPToken))
{
int abcd = GetLastError();
printf("Process token open Error: %u\n",GetLastError());
}

if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&luid))
{
WriteLogString("Lookup Privilege value Error: %u\n",GetLastError());
}
tp.PrivilegeCount =1;
tp.Privileges[0].Luid =luid;
tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;

DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,NULL,
SecurityIdentification,TokenPrimary,&hUserTokenDup);
int dup = GetLastError();

//Adjust Token privilege
SetTokenInformation(hUserTokenDup,
TokenSessionId,(void*)dwSessionId,sizeof(DWORD));

if (!AdjustTokenPrivileges(hUserTokenDup,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,NULL))
{
int abc =GetLastError();
WriteLogString("Adjust Privilege value Error: %u\n",GetLastError());
}

if (GetLastError()== ERROR_NOT_ALL_ASSIGNED)
{
WriteLogString("Token does not have the provilege\n");
}

LPVOID pEnv =NULL;

if(CreateEnvironmentBlock(&pEnv,hUserTokenDup,TRUE))
{
dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT;
}
else
pEnv=NULL;

// Launch the process in the client's logon session.

bResult = CreateProcessAsUser(
hUserTokenDup, // client's access token
//_T("C:\\SessionLauncher\\a.exe"), // file to execute
appname,
NULL, // command line
NULL, // pointer to process SECURITY_ATTRIBUTES
NULL, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
dwCreationFlags, // creation flags
pEnv, // pointer to new environment block
NULL, // name of current directory
&si, // pointer to STARTUPINFO structure
&pi // receives information about new process
);
// End impersonation of client.

//GetLastError Shud be 0

int iResultOfCreateProcessAsUser = GetLastError();
WriteLogString("CreateProcessAsUser getlasterror:%d",iResultOfCreateProcessAsUser);
//Perform All the Close Handles tasks

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

return 0;

}
请叫我涛哥0-0 2012-02-29
  • 打赏
  • 举报
回复
如果你只想在开机后启动这个exe,完全不用这么坐,可以往系统启动项的注册表写值,还可以将exe的快捷方式直接加到startup文件夹下就OK了。
如果你非要用程序去掉,连个函数,很简单CreateProcess和ShellExecute,建议用CreateProcess。
dg393624170 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 oyljerry 的回复:]
服务调用用户进程,需要模拟当前用户token等,然后CreateProcessAsUser
否则进程会启动在服务session,不可见
[/Quote]

好像就是这么用的

可不可以给个例子啊。。。。。。。
oyljerry 2012-02-29
  • 打赏
  • 举报
回复
服务调用用户进程,需要模拟当前用户token等,然后CreateProcessAsUser
否则进程会启动在服务session,不可见
c_losed 2012-02-29
  • 打赏
  • 举报
回复
system
winexec
CreateProcess
ShellExecute
...
ouyh12345 2012-02-29
  • 打赏
  • 举报
回复
CreateProcess
enjoyreading 2012-02-29
  • 打赏
  • 举报
回复
ShellExecute就可以了吧,方便实用
请叫我涛哥0-0 2012-02-29
  • 打赏
  • 举报
回复
这是MFC的一段话:

Interactive Services
An interactive service is a service that can interact with the input desktop. Other desktops do not receive user input. For more information, see Window Stations and Desktops.

An interactive service must run in the context of the LocalSystem account and be configured to run interactively. Services are configured to run interactively when the dwServiceType parameter in a CreateService call is set to include SERVICE_INTERACTIVE_PROCESS.

意思是说你可以创建一个交互型服务程序,这样就可以与桌面交互了。
方法:通过创建服务函数CreateService() 的一个参数dwServiceType,把它的值设为SERVICE_INTERACTIVE_PROCESS。
然后启动你要进程,用我上面的ShellExecute或CreateProcess。
dg393624170 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 oyljerry 的回复:]
http://www.codeproject.com/Messages/2346466/Calling-CreateProcessAsUser-from-service-on-to-Win.aspx

http://www.codeguru.com/forum/showthread.php?t=256383
[/Quote]

给个例子吧。。。求求各位了

那个英文看的人头大啊。。。。。。。。。
stonespace 2012-02-29
  • 打赏
  • 举报
回复
很多方法可以实现,ShellExecute比较方便,
oyljerry 2012-02-29
  • 打赏
  • 举报
回复
http://www.codeproject.com/Messages/2346466/Calling-CreateProcessAsUser-from-service-on-to-Win.aspx

http://www.codeguru.com/forum/showthread.php?t=256383
dg393624170 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenjintaoxp 的回复:]
如果你只想在开机后启动这个exe,完全不用这么坐,可以往系统启动项的注册表写值,还可以将exe的快捷方式直接加到startup文件夹下就OK了。
如果你非要用程序去掉,连个函数,很简单CreateProcess和ShellExecute,建议用CreateProcess。
[/Quote]

很简单CreateProcess和ShellExecute,建议用CreateProcess在服务程序中应该是掉不起来吧,老兄

汗。。。。。。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧