c++builder Cannot acces variable on other form (dll)

47 Views Asked by At

I cannot access a function/variable or component on another form. I created a dll with two forms. POSFORM is called from Host application. After calling the dll, POSFORM is shown. On buttonclick PAYFORM is created and too without problems. PAYFORM buttonclick should access a variable and a label in POSFORM.

There the AV rises.

FPosForm->abc;
FPosForm->Label1->Caption = "test";

Everything compiles just fine

POSFORM.CPP (first form)


#include <vcl.h>
#pragma hdrstop

#include "PosForm.h"
#include "payForm.h"
// ---------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

__fastcall TFPosForm::TFPosForm(TComponent* Owner) : TForm(Owner)
{

}

// ---------------------------
void __fastcall TFPosForm::Button1Click(TObject * Sender)
{


abc=999;
TFormPay *FormPay;
FormPay = new TFormPay(this);
FormPay->ShowModal();

}


POSFORM.H
// ---------------------------

#ifndef PosFormH
#define PosFormH
// ---------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>

// ---------------------------
class PACKAGE TFPosForm : public TForm
{
__published: // IDE-managed Components

TPanel *Panel1;
TButton *Button1;
TLabel *label1;

void __fastcall Button1Click(TObject *Sender);

private: // User declarations
public : // User declarations

int abc;

__fastcall TFPosForm(TComponent* Owner);
};

// ---------------------------
extern PACKAGE TFPosForm *FPosForm;
// ---------------------------
#endif







Second Form
Pay.cpp
// ---------------------------

#include <vcl.h>
#pragma hdrstop

#include "pay.h"
#include "PosForm.h"
// ---------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormPay *FormPay;

// ----------------------------
__fastcall TFormPay::TFormPay(TComponent* Owner) : TForm(Owner)
{
}

// ---------------------------

void __fastcall TFormPay::Button1Click(TObject *Sender)
{
FPosForm->abc = 1000;
FPosForm->Label1->Caption = "test";
}

Pay.h


#ifndef payH
#define payH
// ---------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>

// ---------------------------
class TFormPay : public TForm
{
__published: // IDE-managed Components
TButton *Buttton1;
void __fastcall Button1(TObject *Sender);

private: // User declarations
public : // User declarations

__fastcall TFormPay(TComponent* Owner);
};

// ---------------------------
extern PACKAGE TFormPay *FormPay;
// ---------------------------
#endif
1

There are 1 best solutions below

0
N.K. On

AV: Access violation at address 055A241E in module POSDLL.DLL Read ata of address 0000059C

This happens when I try to change the label text

FPosForm->Label1->Text = "TEST"; or when I change the integer value FPosForm->abc = 1000;