• 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.

pdk addon to display weather data

Messages
25
Country
india
Hello,

I attempted to make a small weather add-on which would write the variable values accessible through WeatherSystem interface to a text file. But I could not get it to work - while the addon is added, the text file is not created on load. Could you guide me on what i am missing?
The c++ code looks like this:

#include "stdafx.h"
#include "initpdk.h"
#include "PdkPlugin.h"

#include <WinError.h>

#include <string>
#include<iostream>
using std::cerr;
using std::endl;
#include<fstream>
using std::eek:fstream;
#include<cstdlib>

using namespace P3D;

class SystemReadyCallback : public ICallbackV400
{
public:
SystemReadyCallback() :
m_RefCount(1)
{
}

virtual void Invoke(IParameterListV400* pParams) override;

DEFAULT_REFCOUNT_INLINE_IMPL()
DEFAULT_IUNKNOWN_QI_INLINE_IMPL(ICallbackV400, IID_ICallbackV400)


};

void SystemReadyCallback::Invoke(IParameterListV400* pParams)
{
if (!pParams)
{
return;
}

const static wchar_t* S_NAME = L"Virtual Cockpit - View 00";

// This callback indicate that Prepar3D finish loading
UINT64 messageID = pParams->GetParameter(0).Value;
if (messageID == EVENT_MESSAGE_LOADING_COMPLETE)
{
ofstream outdata;

// Get the window plugin system
//CComPtr<IWindowPluginSystem> spPluginSystem = PdkServices::GetWindowPluginSystem();
CComPtr<IWeatherSystemV430> weatherSystem = PdkServices::GetWeatherSystem();
if (weatherSystem != NULL)
{
outdata.open("weatherdata.txt");
outdata << weatherSystem->GetGlobalBaroPressure() << endl;
outdata.close();
}
}

}

static SystemReadyCallback* s_pSystemReadyCallback = nullptr;

extern "C" __declspec(dllexport) void __stdcall DLLStart(__in __notnull IPdk* pPdk)
{
PdkServices::Init(pPdk);
printf("P3d initialized");


s_pSystemReadyCallback = new SystemReadyCallback();

PdkServices::GetEventService()->RegisterCallback(EVENTID_Message, s_pSystemReadyCallback);


}


extern "C" __declspec(dllexport) void __stdcall DLLStop(void)
{
if (s_pSystemReadyCallback != nullptr)
{
s_pSystemReadyCallback = nullptr;
}

PdkServices::Shutdown();
}

My add-on.xml file:

<?xml version="1.0" encoding="utf-8" ?>


- <SimBase.Document Type="AddOnXml" version="4,3" id="add-on">


<AddOn.Name>MyAddOn</AddOn.Name>


<AddOn.Description>My Add-on developed by My Company.</AddOn.Description>


- <AddOn.Component>


<Category>Weather</Category>


<Path>weather</Path>


</AddOn.Component>


- <AddOn.Component>


<Category>DLL</Category>


<Path>C:\Program Files\Lockheed Martin\Prepar3D v4 SDK 4.3.29.25520\PDK\x64\Debug\weather.dll</Path>


<DLLType>PDK</DLLType>


</AddOn.Component>


</SimBase.Document>

Thank you
 
Messages
87
Country
russia
EVENT_MESSAGE_LOADING_COMPLETE is emitted once when loading of scenario is completed, i.e. you are in a plane on the runway.
I predict that you will find your weatherdata.txt in Prepar3D working directory. (D:\Program Files\Lockheed Martin\Prepar3D v4). By default ofstream create and open files in the relative path to the working directory of applicatoin. By default that is directory where application exe file is located (Prepar3D.exe), not where you have placed your dll.
To write file to the Add-ons folder I use this part of code
Code:
std::string logfile_path = "";
LPTSTR userpath = (LPTSTR)malloc(100 * sizeof(TCHAR));
if (GetEnvironmentVariable("USERPROFILE", userpath, 100) != 0)
    logfile_path = std::string(userpath) + "\\Documents\\Prepar3D v4 Add-ons\\YourMagicAddon\\";

outdata.open(logfile_path + "Log.txt");

Or you can look at the DataHarvester in PDK examples. It will search for directory where dll file is located.

P.S. Please formate code in the future posts.
 
Last edited:
Messages
25
Country
india
I am facing another difficulty. I am unable to call and use the IWeatherStation interface through the GetWeatherStation function. My code:
Code:
if (weatherSystem != NULL)
        {
            P3D::IWeatherStationV430* weatherStation=nullptr;   
            HRESULT station = weatherSystem->GetWeatherStation("GLOB", weatherStation);
            
           P3D::IWeatherStationV430 *weatherStationPointer = *weatherStation;
            outdata.open("weatherdata.txt");
            outdata << "Global Pressure: "<<weatherSystem->GetGlobalBaroPressure() << " millibars" <<endl;
            outdata <<"Global Temp:"<< weatherSystem->GetGlobalTemp() << " degree Celsius" << endl;
            outdata <<"Global Vis Range: "<< weatherSystem->GetGlobalVisRange() << " meters" << endl;
            outdata << "Global Horizontal Wind Speed:"<<weatherSystem->GetGlobalHorizWindSpeed() * 1.94384 << " knots " << endl;
            outdata << "Global wind direction: "<<weatherSystem->GetGlobalWindDirection() << " degrees" << endl;
            outdata << "Global Dew point: "<<weatherSystem->GetGlobalDewPoint() << endl;
            outdata << "Weather Station: " << station << endl;
            outdata << "Cloud layer count:" << weatherStationPointer->GetCloudLayerCount();
            outdata.close();
        }

causes P3d to shutdown on loading, I think the cause is a null pointer exception. How do I access correctly?
 
Messages
87
Country
russia
I think that you are using pointers wrong. Be caution with them in C. I recommend to use smart pointers in C++ and CComPtr for PDK interface classes that implement COM interfacaces.
C++:
        CComPtr<IWeatherSystemV430> weatherSystem = PdkServices::GetWeatherSystem();
        if (weatherSystem != NULL)
        {
            CComPtr<IWeatherStationV430> weatherStation;
            HRESULT station = weatherSystem->GetWeatherStation("EHAM", &weatherStation);

            outdata.open("weatherdata.txt");
            outdata << "Global Pressure: " << weatherSystem->GetGlobalBaroPressure() << " millibars" << endl;
            outdata << "Global Temp:" << weatherSystem->GetGlobalTemp() << " degree Celsius" << endl;
            outdata << "Global Vis Range: " << weatherSystem->GetGlobalVisRange() << " meters" << endl;
            outdata << "Global Horizontal Wind Speed:" << weatherSystem->GetGlobalHorizWindSpeed() * 1.94384 << " knots " << endl;
            outdata << "Global wind direction: " << weatherSystem->GetGlobalWindDirection() << " degrees" << endl;
            outdata << "Global Dew point: " << weatherSystem->GetGlobalDewPoint() << endl;
            outdata << "Weather Station: " << station << endl;
            if (SUCCEEDED(station) && weatherStation->IsValid())
                outdata << "Cloud layer count:" << weatherStation->GetCloudLayerCount();
            outdata.close();
        }
Sorry, but I can't test it now.
 
Messages
87
Country
russia
If you are using Visual Studio try to debug you dll's code.
1. Set your project settings to start Prepar3D.exe and working directory accordingly. (See picture).
2. Set breakpoints in your code.
3. Push F5 to start debug.
VS_debug.jpg

P3D_SDK is the user Environment Variable I use to simplify switching for new SDK version.
 
Top