新建进程

时间:2008-07-24 05:30:23   来源:论坛整理  作者:  编辑:chinaitzhe
我想在一个win32控制台进程里面新建另外的win32控制台应用程序,然后主进程向新建的进程发个消息问候一下就OK了。
网友回复:新建进程可以用ShellExecute, CreateProcess或者WinExec函数。
1。ShellExecute(NULL, "open", "c:\\windows\\notepad.exe", NULL, NULL, SW_SHOWNORMAL)

2。STARTUPINFO si = {0};
si.cb = sizeof(si);

PROCESS_INFORMATION pi = {0};
CreateProcess(NULL, "c:\\windows\\notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

3。WinExec("c:\\windows\\notepad.exe", SW_SHOWNORMAL);

主进程向新建的进程发个消息依靠于你这里的消息发送机制了。比如通过socket,pipe,WM_COPYDATA,Memory Map File等等。
网友回复:
引用 1 楼 coding_hello 的回复:
新建进程可以用ShellExecute, CreateProcess或者WinExec函数。
1。ShellExecute(NULL, "open", "c:\\windows\\notepad.exe", NULL, NULL, SW_SHOWNORMAL)

2。STARTUPINFO si = {0};
si.cb = sizeof(si);

PROCESS_INFORMATION pi = {0};
CreateProcess(NULL, "c:\\windows\\notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

3。WinExec("c:\\windows\\notepad.exe", SW_SHOWNORMAL);

主…


up!!
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





    WinExec("MyWin.exe", SW_HIDE);

        HWND hAppWnd = ::FindWindow(NULL, "MyWin");

        if (hAppWnd)

        {

            ::SendMessage(hAppWnd, WM_HELLO, (LPARAM)0, (WPARAM)0);        

        }




网友回复:我用这个WinExec("c:\\windows\\notepad.exe", SW_SHOWNORMAL); 建得一个记事本,但是怎么向它发送信息呢,我用HWND hAppWnd = ::FindWindow(NULL, "无标题 - 记事本");获得它的句柄了,但是用SendMessage为什么发送不了消息呢。记事本还是空白的啊
网友回复:
引用 3 楼 bubu8633 的回复:
C/C code
WinExec("MyWin.exe", SW_HIDE);
HWND hAppWnd = ::FindWindow(NULL, "MyWin");
if (hAppWnd)
{
::SendMessage(hAppWnd, WM_HELLO, (LPARAM)0, (WPARAM)0);
}


需要自定义WM_HELLO 消息!
网友回复:在C 里面哦,没用VC,也没用MFC,怎么定义消息呢
网友回复:
引用 6 楼 wu209000 的回复:
在C 里面哦,没用VC,也没用MFC,怎么定义消息呢


#define MY_MSG WM_USER 100

要求发送方和接收方进程都由你自己编写
网友回复:还有。。
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





在Windows程序中,各个进程之间经常需要交换数据,进行数据通讯。常用的方法有



  使用内存映射文件

  通过共享内存DLL共享内存

  使用SendMessage向另一进程发送WM_COPYDATA消息



比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.



WM_COPYDATA消息的主要目的是答应在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继续同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:



这个函数的原型及其要用到的结构如下:



SendMessage(hwnd,WM_COPYDATA,wParam,lParam); 

其中,WM_COPYDATA对应的十六进制数为0x004A



wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:

typedef struct tagCOPYDATASTRUCT{

    DWORD dwData;//用户定义数据

    DWORD cbData;//数据大小

    PVOID lpData;//指向数据的指针

}COPYDATASTRUCT;

该结构用来定义用户数据。



具体过程如下:





首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.



接受方在DefWndProc事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.



代码中有适量的解释,大家请自己看吧.



具体代码如下:

//---------------------------------------------------

//发送方:

//---------------------------------------------------



using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;



namespace WindowsFormGetMsg

{

 public class Form1 : System.Windows.Forms.Form

 {

  private System.Windows.Forms.TextBox textBox1;

  private System.ComponentModel.Container components = null;

  const int WM_COPYDATA = 0x004A;



  public Form1()

  {

   InitializeComponent();

  }



  protected override void Dispose( bool disposing )

  {

   if( disposing )

   {

    if (components != null) 

    {

     components.Dispose();

    }

   }

   base.Dispose( disposing );

  }



  #region Windows Form Designer generated code

  private void InitializeComponent()

  {

   this.textBox1 = new System.Windows.Forms.TextBox();

   this.SuspendLayout();

   // 

   // textBox1

   // 

   this.textBox1.Location = new System.Drawing.Point



(176, 32);

   this.textBox1.Name = "textBox1";

   this.textBox1.Size = new System.Drawing.Size(160, 



21);

   this.textBox1.TabIndex = 0;

   this.textBox1.Text = "textBox1";

   // 

   // Form1

   // 

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 



14);

   this.ClientSize = new System.Drawing.Size(432, 266);

   this.Controls.AddRange(new 



System.Windows.Forms.Control[] {

          



          



this.textBox1});

   this.Name = "Form1";

   this.Text = "接收方";

   this.ResumeLayout(false);



  }

  #endregion



  [STAThread]

  static void Main() 

  {

   Application.Run(new Form1());

  }



  protected override void DefWndProc(ref 



System.Windows.Forms.Message m)

  {

   switch(m.Msg)

   {

     //接收自定义消息 USER,并显示其参数

    case WM_COPYDATA:

     COPYDATASTRUCT mystr = new 



COPYDATASTRUCT();

       Type mytype = mystr.GetType();



       mystr =(COPYDATASTRUCT)m.GetLParam(mytype);

     this.textBox1.Text  =mystr.lpData;



     break;

    default:

     base.DefWndProc(ref m);

     break;



   }



  }



 }

 [StructLayout(LayoutKind.Sequential)] 

 public struct COPYDATASTRUCT

 {

  public IntPtr dwData;

  public int cbData;

  [MarshalAs(UnmanagedType.LPStr)] public string lpData;

 }

}





//---------------------------------------------------

//接受方

//---------------------------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;



namespace WindowsFormGetMsg

{

 public class Form1 : System.Windows.Forms.Form

 {

  private System.Windows.Forms.TextBox textBox1;

  private System.ComponentModel.Container components = null;

  const int WM_COPYDATA = 0x004A;



  public Form1()

  {

   InitializeComponent();

  }



  protected override void Dispose( bool disposing )

  {

   if( disposing )

   {

    if (components != null) 

    {

     components.Dispose();

    }

   }

   base.Dispose( disposing );

  }



  #region Windows Form Designer generated code

  private void InitializeComponent()

  {

   this.textBox1 = new System.Windows.Forms.TextBox();

   this.SuspendLayout();

   // 

   // textBox1

   // 

   this.textBox1.Location = new System.Drawing.Point



(176, 32);

   this.textBox1.Name = "textBox1";

   this.textBox1.Size = new System.Drawing.Size(160, 



21);

   this.textBox1.TabIndex = 0;

   this.textBox1.Text = "textBox1";

   // 

   // Form1

   // 

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 



14);

   this.ClientSize = new System.Drawing.Size(432, 266);

   this.Controls.AddRange(new 



System.Windows.Forms.Control[] {

          



          



this.textBox1});

   this.Name = "Form1";

   this.Text = "接收方";

   this.ResumeLayout(false);



  }

  #endregion



  [STAThread]

  static void Main() 

  {

   Application.Run(new Form1());

  }



  protected override void DefWndProc(ref 



System.Windows.Forms.Message m)

  {

   switch(m.Msg)

   {

     //接收自定义消息 USER,并显示其参数

    case WM_COPYDATA:

     COPYDATASTRUCT mystr = new 



COPYDATASTRUCT();

       Type mytype = mystr.GetType();



       mystr =(COPYDATASTRUCT)m.GetLParam(mytype);

     this.textBox1.Text  =mystr.lpData;



     break;

    default:

     base.DefWndProc(ref m);

     break;



   }



  }



 }

 [StructLayout(LayoutKind.Sequential)] 

 public struct COPYDATASTRUCT

 {

  public IntPtr dwData;

  public int cbData;

  [MarshalAs(UnmanagedType.LPStr)] public string lpData;

 }

}









作者Blog:http://blog.csdn.net/TheAres/






网友回复:很好,很强大 ^_^
网友回复:可以用 CreateConsole 这个函数
http://www.iptry.cn
关键字:新建,进程,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面