为了测试现有应用程序,我编写了一个可以加载到我们的模拟应用程序中的 dll。一切正常,直到我想从 dll 中重置现有应用程序。虽然 main() 重新启动,但似乎内存没有重置/初始化。目标是在现有应用程序中尽可能少地改变,所以实际上我不想重写应用程序以在启动时初始化其变量。除此之外,所有局部静态变量也保留其旧值。
下面是我如何从 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
}
现有应用程序的主要是相当常见的。下面是 main() 的指示。
int main(void)
{
static char test = 0;
init_stuff();
while(true)
{
test = 1;
do_stuff();
while(!timer_tick)
{
check_timer();
}
timer_tick = FALSE;
}
}
静态测试变量初始化为 0 并在无限循环中设置为 1。当从 dll 中重置应用程序时,主程序重新启动,但测试变量保持值 1。显然,我希望在重置应用程序时将此变量重置为 0。
有任何想法吗?