从c++启动一个c#应用程序,并在该应用程序上执行一个任务
本文关键字:一个 应用程序 应用 任务 程序上 执行 c++ 启动 | 更新日期: 2023-09-27 18:17:09
我看了这篇文章,实现了我的c#应用程序的打开。我的c#应用程序打开一个文件夹并绘制一个图形。我是否有可能告诉我的c#应用程序从c++打开哪个文件夹,然后一旦看到图形并关闭c#程序,它就返回到c++应用程序。
编辑:谢谢马修,我把它修好了
关于我的CreateProcess lpCommandLine变量的另一个查询:(下面是代码)
CString sFolderPath = "C:'Documents and Settings'...";
int nStrBuffer = sFolderPath.GetLength() + 50;
LPTSTR szParam = _tcsdup(sFolderPath.GetBuffer(nStrBuffer));
nRet = ::CreateProcess(szCmdline,// pointer to name of executable module
szParam,// pointer to command line string
NULL,// pointer to process security attributes
NULL,// pointer to thread security attributes
FALSE,// handle inheritance flag
DETACHED_PROCESS,// creation flags
NULL,// pointer to new environment block
NULL,// pointer to current directory name
&sui,// pointer to STARTUPINFO
&pi );// pointer to PROCESS_INFORMATION
我正确地获得了变量szParam,但是当应用程序打开时,没有复制完整的文件名。例如:在上述情况下,只有"和设置...."被复制,因为"C:'Documents"部分被留下。你能指出我的错误吗?
c#实现:[STAThread]
static void Main(string[] args)
{
foreach (string result in args)
{
MessageBox.Show(result);
}
}
这当然是可能的。
c++ CreateProcess()
有一个参数叫做lpCommandLine
。
你需要在c++中做的是传递lpCommandLine
一个字符串,它有你想打开的文件夹的名字。如果文件夹路径包含空格,则需要将字符串用双引号括起来。
在你的c#程序中,你将有一个static void Main(string[] args)
。args
参数将包含您从c++程序传递的文件夹名称,以便您可以适当地对其进行操作。
对于等待c#程序退出的c++程序,它需要使用WaitForSingleObject()
来等待c#程序退出,使用从CreateProcess()
返回的进程句柄。
如下所示:http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result