How to reinitialize native code in managed dll
For testing an existing application I wrote a dll that can be loaded into our simulation application. All is working just fine until I want to reset the existing application from within the dll. Although the main() is restarted, it seems that the memory is not reset/initialized.
The goal is to change as little as possible in the existing application, so actually I don't want to rewrite the application to initialize its variables at startup. Besides that, all local static variables also keep their old values.

Below a sample of how I call the existing application from within the dll.

    void TimerThread::Run(void)
    {
      while(true)
      {
        if ((nullptr != mpMainThread) && (mpMainThread->ThreadState == System::Threading::ThreadState::Stopped))
        {
          // Cleanup MainThread when thread has stopped
          delete mpMainThread;
          mpMainThread = nullptr;
        }

        if (nullptr == mpMainThread)
        {
          // (Re)create MainThread in which the existing application is executed
          mpMainThread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(&Main));
          mpMainThread->Priority = System::Threading::ThreadPriority::Highest;
          mpMainThread->Start();
        }
        dtStartTime = System::DateTime::Now;           // Keep track when started.
        if (nullptr != mpMainThread)
        {
          //Simulate timertick in existing application
          main_callback_timer_elapsed();
        }

        dtEndTime = System::DateTime::Now;
        iDuration = dtEndTime->Millisecond - dtStartTime->Millisecond;  // Determine execution time.
        System::Threading::Thread::Sleep(((TIMER_INTERVAL - iDuration) > 0) ? (miInterval - iDuration) : 0);    // Set sleep time depending on time spent
      }
    }

    void TimerThread::Main(void)
    {
      main();     // Run main off existing application
    }

    void TimerThread::Reset(void)
    {
      mpMainThread->Abort();      // Reset existing application by aborting MainThread
    }

The main of the existing application is quite common. Below an indication of the main(). 

    int main(void)
    {
      static char test = 0;

      init_stuff();

      while(true)
      {
        test = 1;

        do_stuff();
        while(!timer_tick)
        {
          check_timer();
        }
        timer_tick = FALSE;
      }
    }

The static test variable is initialized at 0 and set to 1 in the infinite loop. When the application is reset from within the dll, the main restarts, but the test variable keeps the value 1. Obviously I want this variable to be reset to 0 when I reset the application.

Any ideas?
How to reinitialize native code in managed dll
===

For testing an existing application I wrote a dll that can be loaded into our simulation application. All is working just fine until I want to reset the existing application from within the dll. Although the main() is restarted, it seems that the memory is not reset/initialized.
The goal is to change as little as possible in the existing application, so actually I don't want to rewrite the application to initialize its variables at startup. Besides that, all local static variables also keep their old values.

Below a sample of how I call the existing application from within the dll.

    void TimerThread::Run(void)
    {
      while(true)
      {
        if ((nullptr != mpMainThread) && (mpMainThread->ThreadState == System::Threading::ThreadState::Stopped))
        {
          // Cleanup MainThread when thread has stopped
          delete mpMainThread;
          mpMainThread = nullptr;
        }

        if (nullptr == mpMainThread)
        {
          // (Re)create MainThread in which the existing application is executed
          mpMainThread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(&Main));
          mpMainThread->Priority = System::Threading::ThreadPriority::Highest;
          mpMainThread->Start();
        }
        dtStartTime = System::DateTime::Now;           // Keep track when started.
        if (nullptr != mpMainThread)
        {
          //Simulate timertick in existing application
          main_callback_timer_elapsed();
        }

        dtEndTime = System::DateTime::Now;
        iDuration = dtEndTime->Millisecond - dtStartTime->Millisecond;  // Determine execution time.
        System::Threading::Thread::Sleep(((TIMER_INTERVAL - iDuration) > 0) ? (miInterval - iDuration) : 0);    // Set sleep time depending on time spent
      }
    }

    void TimerThread::Main(void)
    {
      main();     // Run main off existing application
    }

    void TimerThread::Reset(void)
    {
      mpMainThread->Abort();      // Reset existing application by aborting MainThread
    }

The main of the existing application is quite common. Below an indication of the main(). 

    int main(void)
    {
      static char test = 0;

      init_stuff();

      while(true)
      {
        test = 1;

        do_stuff();
        while(!timer_tick)
        {
          check_timer();
        }
        timer_tick = FALSE;
      }
    }

The static test variable is initialized at 0 and set to 1 in the infinite loop. When the application is reset from within the dll, the main restarts, but the test variable keeps the value 1. Obviously I want this variable to be reset to 0 when I reset the application.

Any ideas?