请教各位一个问题: 我在C++的一个程序里面调用一个Winform(C#环境)窗体,在弹出这个窗体同时,从C++程序里面传入一个参数到这个窗体请问这个参数怎么传入,假如这个参数名称是SerialNum , string类型, 是要在public Form1(){}这个里面做什么动作吗?搞定加分再加10分在线等候...

解决方案 »

  1.   

    public Form1(string s){ this.s=s;
    }
      

  2.   

    啥意思 怎么调用WinForm窗口?是运行c#写的winform程序?还是调用c#的dll?
    如果是运行c#的程序的 在c#的Program.cs里面 把Main函数改一下
    改成
    static void Main(params string[] args)
    {
    //...
    }假设生成 csapp.exe 运行的时候 csapp.exe serialNum1 serialNum2 serialNum3 ...
    则在c#里面的args就能看到serialNum1 serialNum2 ...
      

  3.   


    #include "assert.h"
    #include <mscoree.h>
    #import <mscorlib.tlb> raw_interfaces_only rename("ReportEvent","ReportEventManaged") //high_property_prefixes("_get","_put","_putref")
    using namespace mscorlib;int main()
    {
      HRESULT hr;
      wchar_t buffer[60];
      ICLRRuntimeHost *pHost = NULL;
      ::MessageBoxW(0, L"begin", L"begin", 0);
      hr = CorBindToRuntimeEx(L"v2.0.50727",L"wks",0 //STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN | STARTUP_CONCURRENT_GC
            ,CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (void **)&pHost);
      if (SUCCEEDED(hr))
        ::MessageBoxW(0, L"binded", L"yes", 0);
      else
        ::MessageBoxW(0, L"no", L"no", 0);
      
      DWORD domainId;
      hr = pHost->GetCurrentAppDomainId(&domainId);
      _itow(domainId, buffer, 10);
      ::MessageBoxW(0, buffer, L"yes", 0);
      
      // (This is where you could get CLR managers)
      ICLRControl *pCLRControl;
      hr = pHost->GetCLRControl(&pCLRControl);
      assert(SUCCEEDED(hr));
      
      // must get this stuff before the CLR is started
      ICLRGCManager *pCLRGCManager;
      hr = pCLRControl->GetCLRManager(IID_ICLRGCManager,  (VOID **)&pCLRGCManager);
      //wprintf(L"\n%d\n",hr);
      assert(SUCCEEDED(hr));//  ICLRAssemblyIdentityManager* pCLRAssemblyIdentityManager;
    //  hr = pCLRControl->GetCLRManager(IID_ICLRAssemblyIdentityManager, (VOID **)&pCLRAssemblyIdentityManager);
    //  assert(SUCCEEDED(hr));
      //ClrCreateManagedInstance
      
      hr = pHost->Start();
      if (SUCCEEDED(hr))
        ::MessageBoxW(0, L"started", L"yes", 0);
      else
        ::MessageBoxW(0, L"no", L"no", 0);
      DWORD retVal;
      hr = pHost->ExecuteInDefaultAppDomain(L"D:\\testDll.dll", 
                                            L"testDll.Class1",
                                            L"showBox",
                                            L"hello world!",
                                            &retVal);  if (SUCCEEDED(hr))
        ::MessageBoxW(0, L"yew", L"yes", 0);
      else
        ::MessageBoxW(0, L"no", L"no", 0);  pHost->Stop();  exit(0);
    }
      

  4.   

    各位不好意思,可能是我没有表述清楚我们公司的这个程序是用C++写的,我们没有办法修改,我们也不用去关心,反正是可以直接加参数执行Exe文件,例如参数为SerialNum  运行CMD 然后执行 D:\ABC\ABC\bin\Debug\ABC.EXE SerialNum现在我要做的事情就是运行这个C++程序的时候弹出我做的Winform窗口,但是必须得从C++页面传回一个参数SerialNum到Winform窗口现在的问题就是不知道在Winform里面传入参数感谢各位帮忙,如能贴出代码(C#环境),感谢不尽,追加10分
      

  5.   


    //修改c#的Program.cs里面的Main函数
    static void Main(params string[] args) 

    //... 
    //...其余不变
    Application.Run(new Form1(args));

    //...
    //在Form1里面 添加
    public Form1(params string[] args)
    {
       //...
       //args就是命令行参数了
    }
      

  6.   

    Hi lunat:不好意思,我最个新手,还是看图说话,把第一张图片CMD中的100传到第二张图片窗口中的TextBox,咋弄
      

  7.   

    看来她要码子, 运行 test  100using System;
    using System.Drawing;
    using System.Windows.Forms;// csc /t:winexe
    namespace demo
    {
      class program
      {
        static void Main(string[] args)
        {
          Form frm = new Form();
          TextBox textBox = new TextBox();
          textBox.Location = new Point(2,2);
          textBox.Width = 200;
          if (args.Length > 0)
            textBox.Text = args[0];
          
          frm.Controls.Add(textBox);
          
          Application.Run(frm);
        
        }
        
      };
    }
      

  8.   

    谢谢 Dobzhansky & Luant搞定明天加分结贴
      

  9.   


    //修改c#的Program.cs里面的Main函数
    static void Main(params string[] args) 

    //... 
    //...其余不变
    Application.Run(new Form1(args));

    //...
    //在Form1里面 添加
    public Form1(params string[] args)
    {
       //...
       //args就是命令行参数了
       textBox1.Text = String.Join(" ",args);
    }