Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: c++builder
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) {
        ShowMessage(rsa.lastErrorText());
        return;
    }
 
    //  Generate a 1024-bit key.  Chilkat RSA supports
    //  key sizes ranging from 512 bits to 4096 bits.
    success = rsa.GenerateKey(1024);
    if (success != true) {
        ShowMessage(rsa.lastErrorText());
        return;
    }
 
    //  Keys are exported in XML format:
    //const char * publicKey;
    publicKey = rsa.exportPublicKey();
    //printf("Public Key: ");
    ShowMessage(publicKey);
 
    //const char * privateKey;
    privateKey = rsa.exportPrivateKey();
    //printf("Private Key: ");
    ShowMessage(privateKey);
    Form2->Show();
    //Close();
}
 
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 Smile | :)
Posted 25-Feb-13 9:33am
raniam438
Edited 25-Feb-13 9:36am
v2

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
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