Click here to Skip to main content
13,194,662 members (68,980 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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:    // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:    // User declarations
    const char * publicKey;
    const char * privateKey;
public:     // User declarations
    __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) {
    //  Generate a 1024-bit key.  Chilkat RSA supports
    //  key sizes ranging from 512 bits to 4096 bits.
    success = rsa.GenerateKey(1024);
    if (success != true) {
    //  Keys are exported in XML format:
    //const char * publicKey;
    publicKey = rsa.exportPublicKey();
    //printf("Public Key: ");
    //const char * privateKey;
    privateKey = rsa.exportPrivateKey();
    //printf("Private Key: ");

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();
const char* PrivateKey = Form1->GetPrivateKey();

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 :)
Posted 25-Feb-13 8:33am
Updated 25-Feb-13 8:36am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171018.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100