I am a beginner at programming, and I should create an application for my final thesis. My app is created as SDI in MFC. I made a modal dialog that is opened from View. In the dialog, after button push, a measuring device should be connected to the app and should register several events. After the dialog is closed, coordinates from the connected device should be displayed in View in Static Text.
I can receive coordinates from the device, but there is a problem if I want to change Static text to show those coordinates with ShowWindowTextW(). If I understand, when DoModal() is called, the dialog is closed, and I cannot receive any values or functions from the dialog (therefore, I cannot change Static Text in View).
I found some examples of using pointers, but I don't know how to make it work. I am stuck here for some time and got several types of errors trying to solve them. My code:
MainWindow.cpp - View
void CMainWindow::OnFileConnect()
{
CConnect dlgConnect;
dlgConnect.DoModal();
}
// Displays measurement
void CMainWindow::UpdatePosition() {
if (ManagedWrapper::LMFTracker) {
LMF::Tracker::MeasurementResults::SingleShotMeasurement3D^ position = ManagedWrapper::newPosition;
m_dro_x.SetWindowTextW((CString)position->Position->Coordinate1->ToString());
m_dro_y.SetWindowTextW((CString)position->Position->Coordinate2->ToString());
m_dro_z.SetWindowTextW((CString)position->Position->Coordinate3->ToString());
}
}
Connect.cpp - dialog
// CConnect dialog
IMPLEMENT_DYNAMIC(CConnect, CDialogEx)
CConnect::CConnect(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_CONNECTION, pParent)
{
}
CConnect::~CConnect()
{
}
void CConnect::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SIMULATOR, m_Simulator);
DDX_Control(pDX, IDC_CONNECT, m_Connect);
DDX_Control(pDX, IDC_IPADRESS, m_IPAdress);
}
BEGIN_MESSAGE_MAP(CConnect, CDialogEx)
ON_BN_CLICKED(IDC_CONNECT, &CConnect::OnBnClickedButtonConnect)
ON_BN_CLICKED(IDC_SIMULATOR, &CConnect::OnBnClickedButtonConnectsimulator)
END_MESSAGE_MAP()
void CConnect::OnBnClickedButtonConnect()
{
CString ipAdressString;
m_IPAdress.GetWindowText(ipAdressString);
ConnectTo(ipAdressString);
}
void CConnect::OnBnClickedButtonConnectsimulator()
{
ConnectTo(_T("Simulator"));
}
void CConnect::ConnectTo(CString ipAddress)
{
if (ManagedWrapper::LMFTracker)
{
ManagedWrapper::LMFTracker->Disconnect();
}
Connection^ con = gcnew Connection();
if (ipAddress != "0.0.0.0") {
ManagedWrapper::LMFTracker = con->Connect(gcnew System::String(ipAddress));
// Register some Events...
}
}
In the variable position in CMainWindow.cpp is correct value so this part should be ok. If I try to debbug the code I will get error in winocc.cpp: m_hWnd Unable to read memory
void CWnd::SetWindowText(LPCTSTR lpszString)
{
ENSURE(this);
ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); // exception thrown in this line
if (m_pCtrlSite == NULL)
::SetWindowText(m_hWnd, lpszString);
else
m_pCtrlSite->SetWindowText(lpszString);
}
I've got "Exception thrown: read access violation. this was 0x250." in line commented above. In Call Stack there is:> mfc140ud.dll!CWnd::SetWindowTextW(const wchar_t * lpszString=0x00e5b200) Line 242 C++
next one is: Aplikacia.exe!CMainWindow::UpdatePosition() Line 95 C++
that is the line with m_dro_x in MainWindow.cpp (m_dro_x has value hWnd=???).
And another: Aplikacia.exe!ManagedWrapper::OnTargetPostionChanged(LMF::Tracker::Tracker^ sender={LMF::Tracker::AT960Tracker^}, LMF::Tracker::MeasurementResults::SingleShotMeasurement3D^ position={LMF::Tracker::MeasurementResults::SingleShotMeasurement3D^}) Line 66 C++
This is from Aplikacia.h file - here is the code:
class CAplikaciaApp : public CWinApp
{
public:
CAplikaciaApp() noexcept;
// Overrides
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
// Implementation
afx_msg void OnAppAbout();
DECLARE_MESSAGE_MAP()
};
extern CAplikaciaApp theApp;
ref class ManagedWrapper {
public:
static CMainWindow* pMainWindow = nullptr;
static LMF::Tracker::Tracker^ LMFTracker = nullptr;
static LMF::Tracker::MeasurementResults::SingleShotMeasurement3D^ newPosition = nullptr;
static void OnTargetPostionChanged(LMF::Tracker::Tracker^ sender, LMF::Tracker::MeasurementResults::SingleShotMeasurement3D^ position) {
newPosition = position;
pMainWindow->UpdatePosition();
}
};
I found solution to my problem, very helpfull was this article: static Pointer to Custom Type stays nullptr after initialization with static not-null pointer of same Type
I added this line into MainWindow.h:
extern CMainWindow* pMainWindow;
Don't know if this is the best possible approach, but it works.
I found solution to my problem, very helpfull was this article: static Pointer to Custom Type stays nullptr after initialization with static not-null pointer of same Type
I added this line into MainWindow.h:
Don't know if this is the best possible approach, but it works