现有一exe后台程序server.exe(后台程序有配置文件,配置文件与可执行文件在同一目录下)
如:
../server.exe
../logs/
../Sessions/
../debug.log
../hnet_server.conf
../panic.log
怎样在mfc中调用CreateProcess|WinExec|ShellExec来正确执行server.exe调用时,没有读取配置文件,而是使用了在server.exe中写好的默认设置。。
求教
这个与主程序的运行目录有关吗?
如:
../server.exe
../logs/
../Sessions/
../debug.log
../hnet_server.conf
../panic.log
怎样在mfc中调用CreateProcess|WinExec|ShellExec来正确执行server.exe调用时,没有读取配置文件,而是使用了在server.exe中写好的默认设置。。
求教
这个与主程序的运行目录有关吗?
PROCESS_INFORMATION pi;
BOOL bCreate = ::CreateProcess(NULL, _T("D:\\server.exe"), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!bCreate)
{
// 失败处理
}
::CloseHandle(pi.hThread);
::CloseHandle(pi.hProcess);
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW;
BOOL ret=CreateProcess(
_T("D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever\\server.exe"),
_T("server.exe"),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if(!ret)...我是这样写的server.exe没有读取配置文件,,,
#include <windows.h>
#include <stdio.h>
#include <tchar.h>void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
} // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d)\n", GetLastError() );
return;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}如果是通过获取自己路径 来进行创建的话
应该是木问题
但是你传入的server.exe的路径最好是绝对路径,并且调用CreateProcess函数时,LPCTSTR lpCurrentDirectory参数必须设置,否则在server.exe中获得当前路径可能不正确
当前运行程序的目录与/Server是在同一目录
server.exe与其配置文件放在/Server/下
一旦exe不在那个指定的目录
进程不会创建成功的
求写一个
server.exe的路径
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d\\Sever\\server.exe
配置文件的路径
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d\\Sever
主程序的路径
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.cb=sizeof(STARTUPINFO);
CreateProcess(
_T("D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever\\server.exe"),
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
_T("D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever\\"),,
&si,
&pi);
配置文件是由主程序读还是由server.exe读?
格式大概是这样的:
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever\\server.exe
通过字符串函数获得目录路径:
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever然后再通过字符串函数获得配置文件路径:
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\nt_i386_vc90d\\Sever\\你的配置文件名
然后就可以读取配置文件并分析了
_chdir在内部调用SetCurrentDirectory(),但还会调用SetEnvironmentVariable(),从而使不同驱动器的当前目录得以保留。
server.exe是别人给的程序。没法修改。。
只能通过写配置文件来控件它。
我单独运行server.exe时,它能读取我写的配置文件。
int pos;
//应用程序全路径+文件名
TCHAR cur_exe[MVO_BUFFER_SIZE];
GetModuleFileName(NULL,cur_exe,MVO_BUFFER_SIZE);
//应用程序路径
CString cur_path=_T("");
cur_path=(CString)cur_exe;
pos=cur_path.ReverseFind('\\');
cur_path=cur_path.Left(pos+1); CString server=cur_path+CString("Server\\server.exe");这样?这样与写绝对路径有差别么???
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d\\Sever\\server.exe
配置文件的路径
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d\\Sever
主程序的路径
D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d那么在主程序中
CString strPath;
::GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();
strPath = strPath.Left(strPath.ReverseFind('\\'));
strPath += _T("\\Server");
::SetCurrentDirectory(strPath);
strPath += _T("server.exe"); STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bCreate = ::CreateProcess(NULL, strPath, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!bCreate)
{
// 失败处理
}
::CloseHandle(pi.hThread);
::CloseHandle(pi.hProcess);
一般在CreateProcess启动进程时设置了当前目录这个参数,就应该没有问题了
错了一点,strPath += _T("server.exe");改为strPath += _T("\\server.exe");
嗯
非常感谢你这么热心
但是我发现server.exe竟然不运行
CreateProcess失败。
我写绝对路径还能运行
只是读不到配置文件
strPath="D:\\Program Files\\HOOPS-1700_VS_NET_2008\\bin\\nt_i386_vc90d\\Sever\\server.exe";
而下面根本不会执行
CString strPath;
::GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();
strPath = strPath.Left(strPath.ReverseFind('\\'));
strPath += _T("\\Server\\");
::SetCurrentDirectory(strPath);
strPath += _T("server.exe");
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bCreate = ::CreateProcess(NULL, strPath.GetBuffer(MAX_PATH), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
用cmd命令可行。
但是不知道怎么加到程序中去。
http://baike.baidu.com/view/697167.htm
第八个参数lpCurrentDirectory
这个参数用来指定子进程的工作路径
如果程序内部寻找配置文件使用相对路径则在这个路径的基础上进行查找
所以你需要设置这个参数来指定你的配置文件的位置
::GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();
strPath = strPath.Left(strPath.ReverseFind('\\'));
strPath += _T("\\Server\\");
ShellExecute(NULL,_T("open"),_T("server.exe"),NULL,strPath,SW_HIDE);
上面这样是可以的
可以通过strPath设置路径
而且可以读到配置文件
运行正确而用createprocess不行。
不知道问题出在哪但是shellexecute不能获得进程句柄,我无法随时关闭它。