2012-11-12 21 views
2

我几个月来一直在学习C++/CLI,但无论我尝试什么,我似乎都无法解决我遇到的问题。将命令行参数传递给C++/CLI中的Windows窗体文件

我需要将String ^,Array ^或ArrayList ^数据类型从main.cpp传递到Form1.h。我试图在main.cpp中创建全局变量,然后使用extern调用变量。但是,这不适用于String,Array和ArrayList数据类型。

我该怎么做呢?提前致谢。这里是我的代码释义:

//main.cpp 

bool LoadFileFromArgument = false; //A boolean can be declared global 
String^ argument; //this will not pass from main.cpp to Form1.h 

int main(array<System::String ^> ^args) 
{ 
String^argument = args[1] 

// Enabling Windows XP visual effects before any controls are created 
Application::EnableVisualStyles(); 
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it 
Application::Run(gcnew Form1()); 

return 0; 
} 



//Form1.h 

public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 

     extern bool LoadFileFromArgument; //is grabbed without error 
     extern String^ argument; //will not work 

以下是错误:

error C3145: 'argument' : global or static variable may not 
have managed type 'System::String ^' 

may not declare a global or static variable, 
or a member of a native type that refers to objects in the gc heap 
+0

感谢Grhm,我曾尝试之前,但从来没有得到它的工作。非常简单,谢谢! – Ethan

回答

2

你能不能为表单创建一个重载的构造函数。即

public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(String^ argument) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
     // Use "argument" parameter as req'd. 
    } 

    Form1(void) 
    { 
     //....usual constructor here... 
     //..etc... 

然后从主

// Create the main window and run it 
Application::Run(gcnew Form1(argument)); 
相关问题