• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX SimConnect_CallDispatch in CALLBACK WndProc

KWB

Messages
69
Country
germany
C++, unmanaged code: Can I call SimConnect_CallDispatch in a WndProc callback?

My code is a .dll started in FSX under DLLs (dll.cfg). In DllMain / DLL_PROCESS_DLL_PROCESS_ATTACH I do attach my WndProc. Hence I do not need a own message loop.

This is why using (abusing?) this loop would be very convenient. But is this a good idea? Otherwise I would go ahead with TOWSIM's approach (see answers here) - message loop for SimConnect in own thread.
 
This approach seems to be impossible, moved on with the proposed own thread for SimConnect message loop. If someone has something similar running (one loop for WndProc and SimConnect message loop), please let me know.
 
The example below works completely in an own thread. It even works in parallel to the window example I sent before. It is a must, if you contact SimConnect out of a DLL. Otherwise the DLL would block the program execution if you enter a loop in the DLL.

Code:
/*****************************************************************************
 COMMENT:   Opens a SimConnect for a specific activ thread or process

 GROUP:     ESP

 ENTRY:     void

 EXIT:      HRESULT should be S_OK if successful

*/


HRESULT ESPinterface::OpenESPconnection()
{
	HRESULT rval;
	EvHdl = CreateEvent(NULL,FALSE,FALSE,NULL);
	rval = SimConnect_Open(&SChdl,"ARIES",NULL,0,EvHdl,0);
	if(rval == S_OK)
		rep.Protocoll(BIT3,"SimConnect_Open successful");
	else
		rep.Protocoll(BIT3,"SimConnect_Open failed with %d",rval);

	ESPcon = SChdl;
	return(rval);
}

/*****************************************************************************
 COMMENT:   This thread keeps in contact with ESP an requests all necessary data

 GROUP:     ESP

 ENTRY:     ESPinterface*   a pointer to the existing class

 EXIT:      Always 0

*/


unsigned int WINAPI ESPthread(void* ESPclassPtr)
{
    HRESULT hr;
	double cycleTime,delta;

    DWORD nh,lpCnt = 0;
    ESP = (ESPinterface*)ESPclassPtr;
    ESP->ESPstatus |= ESP_THREAD_ACTIVE;
    hr = ESP->OpenESPconnection();
	ESPwx.SimCnct = ESP->ESPcon;						// share this handle with the WX class

    rep.Protocoll(BIT3,"In ESP thread");
    if(S_OK == hr)
    {
        rep.Protocoll(BIT3,"Connected to ESP");
        ESP->PreparerequestSets();						 // all type of requests like	SimConnect_AddToDataDefinition and	SimConnect_SubscribeToSystemEvent
        while(ESP->ESPstatus & ESP_THREAD_ACTIVE)
        {
            WaitForSingleObject(ESP->EvHdl,INFINITE);
            if(ESP->ESPstatus & ESP_THREAD_ACTIVE)
            {
                hr = SimConnect_RequestDataOnSimObjectType(ESP->ESPcon, REQUEST_1, RUN_DATA_DEF,0, SIMCONNECT_SIMOBJECT_TYPE_USER);	// runtime data
                if(!lpCnt)
                    rep.Protocoll(BIT3,"Received First ESP data");
                lpCnt++;
                if(!(lpCnt % 10))						// every 200 ms(  5 times per second)
                {
                    hr = SimConnect_RequestDataOnSimObjectType(ESP->ESPcon, REQUEST_2, SLOW_DATA_DEF, 0, SIMCONNECT_SIMOBJECT_TYPE_USER); // slow time data
                }
                if(!(lpCnt % 100) && !( rxMsgs & METAR_RECEIVED))   // every 2000 ms
                {																													// wx data
                    SimConnect_WeatherRequestObservationAtStation(ESP->ESPcon,REQUEST_3,"GLOB");
                }
                SimConnect_CallDispatch(ESP->ESPcon,(DispatchProc)&ESP->ESPdispatcher, NULL);
            }
            Sleep(ESP->ThreadCycleTimeMS);				// normally 20 ms  or whatever is convenient 
        }
 		  
    }
    ESP->CloseESPconnection();
	ESP->EvHdl = NULL;
	ESP->ESPcon = NULL;
    return 0;
}
Regards,
Mike
 
Yep, this is basically what I am doing right now. Thanks for all your support so far.
 
Back
Top