0
votes

I am writing a DLL (MS VS13, c++) that is called by an external program and which uses a standardized parameter list:

extern "C" __declspec(dllimport) int TYPE850(double &time, double xin[], double xout[], double &t, double &dtdt, double par[], int info[], int icntrl);

The results of the routine are written into the xout[] array so that the calling program can read and process the results of the DLL.

Now I want to add a dialog to the DLL that can take user input each time the DLL is called. The idea is to use MFC. It turned out that it is not easy to simply add some MFC code to the existing DLL code, so I am trying to do it the other way round: I am creating a regular MFC DLL plus a dialog resource and add the original DLL code. The code then looks like this (without the include-statements):

BEGIN_MESSAGE_MAP(CType850LibApp, CWinApp)
END_MESSAGE_MAP()

CType850LibApp::CType850LibApp()
{
}

CType850LibApp theApp;


BOOL CType850LibApp::InitInstance()
{
    CWinApp::InitInstance();

    CSelDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();

    return FALSE;
}


extern "C" __declspec(dllexport)int TYPE850(double &time,double xin[], double xout[], double &t, double &dtdt,double par[],int info[], int icntrl)

{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
//DLL code ...
return 1;
}

I have added the instance of the dialog to CType850LibApp::InitInstance() only for testing. The code can be compiled and when running the main application the dialog appears when the DLL is called. But the obvious problem is that the C function is never called. The idea was to call the C function within CType850LibApp::InitInstance() but that does not work because the parameter list of the function is then unknown.

So the questions are: 1) How can I call the C function with the correct parameter list so that all the information contained in the parameter list is correctly transferred to the DLL code and the results can be correctly written in the xout[] array? 2) Once I can call the C function, how can I instantiate the dialog within the DLL?

Thanks for your help!

1
Get the modal dialog box out of InitInstance, put it in TYPE850(...)Barmak Shemirani
Does not help because Type850() is never called.MrModest
Also make sure InitInstance is returning TRUE. It's a simple function call. Edit your question to show your 2nd attempt and show relevant error message.Barmak Shemirani
Don't quite understand what you are trying to do. The executable should load (statically or dynamically) the DLL and call an "exported" function (in your case TYPE850() ). Inside that function you can display the dialog, and call the original DLL code. It doesn't make sense to do this in InitInstance() ot theApp object (called during DLL loading I guess?). Arguments can be passed/retrieved to the dialog using member variables and the DDX/DDV feature, or copy them to global variables if you don't want to do this.Constantine Georgiou

1 Answers

0
votes

Sorry for the late response of mine. I think I found my mistake or at least the source of my confusion.

My assumption was that I need this line of code in order to show the dialog:

 m_pMainWnd = &dlg;

This only works if I create and instantiate the app-class

CType850LibApp::CType850LibApp()
{
}

CType850LibApp theApp;

because my understanding is that m_pMainWnd is a member variable of CType850LibApp. However, once "theApp" is intantiated it is NOT the

extern "C" __declspec(dllimport) int TYPE850()

that is executed when the dll is called but it is theApp instead. The TYPE850() function is completely ignored in this case!

So the solution is, not to use the app-class, to leave out the line

m_pMainWnd = &dlg;

and to place

CPartDlg PartDlg;
INT_PTR nResponse = PartDlg.DoModal();

within the TYPE850() function. Then everything works as desired.

Hope this is understandable! Thanks for your help!