Form aus einer Assembly aufrufen



  • Hallo,

    ich möchte in Visual Studio 2008 C++ eine Assembly zur Laufzeit laden und dessen Form aufrufen. In C# klappt das ganze mit folgendem Code einwandfrei,würde jedoch das Projekt lieber mit c++ weitermachen:

    string Exepath;
                object ob = new object();
                Exepath = Application.StartupPath;
                if (System.IO.File.Exists(Exepath+"/ClassLibrary1.dll") == true)
                {
                    try
                    {
                        Assembly a = Assembly.LoadFrom(Exepath+"/ClassLibrary1.dll");
    
                        Type type = a.GetType("Halcon.Form1");
                        if (a.GetType("Halcon.Form1") != null)
                        {
                            ob = Activator.CreateInstance(type);
                            BV_Form = ob as Form;
                        }
    
                    }
                 }
    

    Nun kann an anderer Stelle im Code einfach die global deklarierte BV_Form vom Typ Form mit BV_Form.Show(); aufgerufen werden und die Form aus der Assembly wird sichtbar.

    In C++ habe ich nun das Problem, dass ich nicht einfach das Objekt der BV_Form zuweisen kann, wie kann man die Form denn sonst am besten aufrufen??
    Habe bisher folgenden C++-Code:

    String^ Exepath;
    	   String^ DLLPath;
    	   System::Object^ ob;
    	   Exepath = Application::StartupPath;
    	   if (System::IO::File::Exists(Exepath + "//CL.dll") == true)
               {
                 try
                    {
    		  DLLPath=Exepath + "\\CL.dll";
                      //Bei Verwendung der Load-Fkt. findet er merkwürdigerweise die 
                      //Assembly nicht mehr, deswegen die LoadFrom-Fkt.
    
    		  Assembly^ a = Assembly::LoadFrom(DLLPath);
    
                      Type^ type = a->GetType("Halcon.Form1");
                      if (a->GetType("Halcon.Form1") != nullptr)
                      {
    		     ob = Activator::CreateInstance(type);
    		    //BV_Form = (*Form) ob;  //Klappt so nicht
                      }
                 }
    

    Vielen Dank im Voraus!!!



  • Hallo,

    //BV_Form = (*Form) ob;  //Klappt so nicht 
    //BV_Form^ = safe_cast<BV_Form^>(ob); // Sollte so gehen. _>> Quatsch mit Sosse
    Form^ BV_Form = safe_cast<Form^>(ob); // Sollte so gehen.
    


  • Vielen Dank für die Hilfe!!!!

    Kleine Korrektur damit es wirklich läuft:

    BV_Form = safe_cast<Form^>(ob);
    

Anmelden zum Antworten