From: David Ching on
"JRGlide" <JRGlide(a)discussions.microsoft.com> wrote in message
news:6475E827-3EE4-4CD8-8B17-5466E0A7F30E(a)microsoft.com...
> /////////////////////////////////////////////////////////////////////////////
> // The one and only CViewPointCloudApp object
>
> CViewPointCloudApp theApp;
>
>
> extern "C" {
>
> __declspec(dllexport) void ViewPointCloud (void);
>
> void ViewPointCloud (void)
> {
> AFX_MANAGE_STATE (AfxGetStaticModuleState());
> theApp.Run();
> }
> } // extern "C"
>
>
>
> BOOL CViewPointCloudApp::InitInstance()
> {
> CWinApp::InitInstance();
>
> AfxEnableControlContainer();
>
> // Change the registry key under which our settings are stored.
> // TODO: You should modify this string to be something appropriate
> // such as the name of your company or organization.
> SetRegistryKey(_T("Local AppWizard-Generated Applications"));
>
> LoadStdProfileSettings(); // Load standard INI file options (including
> MRU)
>
> // Register the application's document templates. Document templates
> // serve as the connection between documents, frame windows and views.
>
> CSingleDocTemplate* pDocTemplate;
> pDocTemplate = new CSingleDocTemplate(
> IDR_MAINFRAME,
> RUNTIME_CLASS(CViewPointCloudDoc),
> RUNTIME_CLASS(CMainFrame), // main SDI frame window
> RUNTIME_CLASS(CViewPointCloudView));
> AddDocTemplate(pDocTemplate);
>
> // Parse command line for standard shell commands, DDE, file open
> CCommandLineInfo cmdInfo;
> ParseCommandLine(cmdInfo);
>
> // Dispatch commands specified on the command line
> if (!ProcessShellCommand(cmdInfo))
> return FALSE;
>
> // The one and only window has been initialized, so show and update it.
> m_pMainWnd->ShowWindow(SW_SHOW);
> m_pMainWnd->UpdateWindow();
>
> return TRUE;
> }
>

The callstack showed a crash in CFrameWnd::OnClose(). Can yo break in the
debugger to show the exact source line of the crash?

Hmm, I've never called theApp.Run() from an exported function like you are
doing. I think theApp is already being run! It is run automatically when
the DLL is loaded. theApp.Run() calls InitInstance(). If you remove the
call, does InitInstance() still get called?

Thanks,
David


From: JRGlide on

Thank you for your response.

> The callstack showed a crash in CFrameWnd::OnClose(). Can you break in the
> debugger to show the exact source line of the crash?

Yes I can, except it's at work and I'm at home right now… Although it may
not matter because I'm starting to suspect the problem has something to do
with your next question. So let me move on to that.


> Hmm, I've never called theApp.Run() from an exported function like you are
> doing. I think theApp is already being run! It is run automatically when
> the DLL is loaded. theApp.Run() calls InitInstance(). If you remove the
> call, does InitInstance() still get called?


Yes, you are right, theApp is already being run and InitInstance is also
being called automatically. The idea of calling theApp.Run() was from a
suggestion on another forum to solve another problem that I was having. But
I think I may have been lead on a wild goose chase. So let me get back to
the beginning.

The original problem is that I couldn't get the MFC dll to remain active.
It would become active for just a split second and then disappear. What was
happening is that the calling program would call the hook function which
would get the passed data but then immediately return because there no way to
tell it to wait for the GUI. Something like this:



void ViewPointCloud (void)
{
AFX_MANAGE_STATE (AfxGetStaticModuleState());

// Get data passed data

return;
}



What I really need it to do is something like this:

void ViewPointCloud (void)
{
AFX_MANAGE_STATE (AfxGetStaticModuleState());

// Pass the data to the MFC application and wait for it to complete

return;
}


So getting back to the original question, how do I call the hook function
and get it to wait for the MFC application to complete before returning?
Calling Run() actually solved that problem because the GUI would wait, but I
suspect that caused other problems.

Thank you for your help,

Jim

From: Ajay Kalra on


> The original problem is that I couldn't get the MFC dll to remain active.
> It would become active for just a split second and then disappear.

What does this mean? DLL is loaded in memory unless you unload it, its
still there.

--
Ajay


From: JRGlide on

> > The original problem is that I couldn't get the MFC dll to remain active.
> > It would become active for just a split second and then disappear.
>
> What does this mean? DLL is loaded in memory unless you unload it, its
> still there.


Here is the basic calling routine. (I'm actually trying to call it from a
MATLAB mex function which is a non_MFC dll. But this simplifies things and
is what I use for testing.)


__declspec(dllimport) int ViewPointCloud (void);

int main(int argc, char* argv[])
{
ViewPointCloud();
return 0;
}

And this is the hook function in theApp:


__declspec(dllexport) void ViewPointCloud (void);

void ViewPointCloud (void)
{
AFX_MANAGE_STATE (AfxGetStaticModuleState());
}


So what happens is that the main function calls the hook function,
ViewPointCloud, which in turn invokes the dll as you said. But since the
hook function has nothing to wait on, it immediately returns to the main
calling routine which then exits, which I think causes the dll to unload from
memory. I can't figure out how to get hook function to wait until the user
is finished with the MFC program before returning, and unloading the dll.

Thank you,

Jim

From: Scott McPhillips [MVP] on
"Ajay Kalra" <ajaykalra(a)yahoo.com> wrote in message
news:83264DC9-21F3-46F5-AE76-2D9C14D2F03B(a)microsoft.com...
>
>
>> The original problem is that I couldn't get the MFC dll to remain active.
>> It would become active for just a split second and then disappear.
>
> What does this mean? DLL is loaded in memory unless you unload it, its
> still there.


He moved an entire MFC application into the DLL and wants it to run as it
did before. I.e. he needs to run the MFC message pump in the DLL. Calling
Run is probably the way to do that, but I don't know about the problem on
Close that has caused.

--
Scott McPhillips [VC++ MVP]