Hi, I'm using C++ Builder XE3, and I've generated a public/private key pair in Form1, and I want to use it in Form2 (make it Public), so I've tried the following:
in Form1.h, I've written:
class TForm1 : public TForm
{
__published:
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private:
const char * publicKey;
const char * privateKey;
public:
__fastcall TForm1(TComponent* Owner);
const char* GetPublicKey(){return publicKey;}
const char* GetPrivateKey(){return privateKey;}
And in Form1.cpp, which is responsible for generating the public/private keys, I've written:
void __fastcall TForm1::Button1Click(TObject *Sender)
{ CkRsa rsa;
bool success;
success = rsa.UnlockComponent("***************");
if (success != true) {
ShowMessage(rsa.lastErrorText());
return;
}
success = rsa.GenerateKey(1024);
if (success != true) {
ShowMessage(rsa.lastErrorText());
return;
}
publicKey = rsa.exportPublicKey();
ShowMessage(publicKey);
privateKey = rsa.exportPrivateKey();
ShowMessage(privateKey);
Form2->Show();
}
And in Form2.cpp, I've written:
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include "GenPubPriKeysFORM.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm2::Button1Click(TObject *Sender)
{
const char* PublicKey = Form1->GetPublicKey();
ShowMessage(PublicKey);
const char* PrivateKey = Form1->GetPrivateKey();
ShowMessage(PrivateKey);
Close();
}
This was my first solution, but i keep getting an error message
"ACCESS VIOLATION AT ADDRESS 50062A73IN MODULE 'rtl170.bpl'. READ OF ADDRESS 00CC54E0" whenever the complier reaches Form2.cpp, which tries to implement the GetPublicKey and GetPrivateKey.
Another solution i've tried was to define the publicKey and PrivateKey as global in Form1.cpp, and then use the extern in the form2.cpp as follows:
extern const char * PublicKey;
extern const char * PublicKey;
But I also get the same error message!
So, can you please help, since I can't understand the problem!
kind regards :)