Windows – FMX – Invalid Class Typecast C++

  

I’m facing a little trouble when trying to run a function through a DLL.
I get the following error when executing the function (it compiles normally):

Invalid Class Typecast

Here is the code:

#include <fmx.h>
#define CRIPTNSDLL_EXPORTS
#include “CriptLib.h”
#include <FMX.Memo.hpp>
#include <System.Classes.hpp>
#include <string.h>

#pragma hdrstop
#pragma argsused

//—————————————————————————

void Cript_BasicoM (String Original, String Chave, TMemo* Memo){

if (Original.Length() != Chave.Length()) {
throw Exception (L”Não é possível encriptar o conteúdo.\nO tamanho das palaras chave e original não podem ser diferentes.”);
}

for (int i = 0; i < Chave.Length(); i++) {
for (int p = 0; p < Chave.Length(); p++) {
if (Chave.c_str()[i] == Chave.c_str()[p] && p != i) {
throw Exception (L”Não é possível encriptar o conteúdo.\nA chave não pode ter dois caracteres iguais.”);
}
}
}

for (int i = 0; i < Original.Length(); i++) {
for (int p = 0; p < Original.Length(); p++) {
if (Original.c_str()[i] == Original.c_str()[p] && p != i) {
throw Exception (L”Não é possível encriptar o conteúdo.\nA palavra original não pode ter dois caracteres iguais.”);
}
}
}

String Linha = NULL;

Memo->BeginUpdate();

for (int i = 0; i < Original.Length(); i++) {
for (int k = 0; k < Memo->Lines->Count; k++) {
Linha = Memo->Lines->Strings[k];
for (int l = 0; l < Linha.Length(); l++) {
if (Linha.c_str()[l] == Original.c_str()[i]) Linha.c_str()[l] = Chave.c_str() [i];
else if (Linha.c_str()[l] == Chave.c_str()[i]) Linha.c_str()[l] = Original.c_str() [i];
}
Memo->Lines->Strings[k] = Linha;
}
}

Memo->Lines->Add(Original);
Memo->Lines->Add(“CriptB”);

Memo->EndUpdate();
Memo->Repaint();

}

Header file:

#ifndef CriptLibH
#define CriptLibH

#ifdef CRIPTNSDLL_EXPORTS
#define CRIPTLIB_API __declspec(dllexport)
#else
#define CRIPTLIB_API __declspec(dllimport)
#endif

#ifdef __cplusplus

#include <fmx.h>
#include <FMX.Dialogs.hpp>
#include <System.Classes.hpp>
#include <string.h>
#include <FMX.Memo.hpp>

extern “C” {
#endif

CRIPTLIB_API void Cript_BasicoM(String Original, String Chave, TMemo* Memo);
CRIPTLIB_API void Cript_BasicoL(String Original, String Chave, TStringList* Lista);
CRIPTLIB_API void Cript_BasicoS(String Original, String Chave, String Linha);
CRIPTLIB_API void Decript_BasicoM(String Chave, TMemo* Memo);
CRIPTLIB_API void Decript_BasicoL(String Chave, TStringList* Lista);
CRIPTLIB_API void Decript_BasicoS(String Original, String Chave, String Linha);
CRIPTLIB_API String VerProg();

#ifdef __cplusplus
}

namespace CriptLib
{
class Encriptar
{
public:
static void Subst(String Original, String Chave, TMemo* Memo) { Cript_BasicoM(Original, Chave, Memo); }
static void Subst(String Original, String Chave, TStringList* Lista) { Cript_BasicoL(Original, Chave, Lista); }
static void Subst(String Original, String Chave, String Linha) { Cript_BasicoS(Original, Chave, Linha); }
};

class Decriptar
{
public:
static void Subst(String Chave, TMemo* Memo) { Decript_BasicoM(Chave, Memo); }
static void Subst(String Chave, TStringList* Lista) { Decript_BasicoL(Chave, Lista); }
static void Subst(String Original, String Chave, String Linha) { Decript_BasicoS(Original, Chave, Linha); }
};

class Info
{
public:
static String Ver() { return VerProg(); }
};

}
#endif

#endif

Evoking the function (also where the exception is thrown):

CriptLib::Encriptar::Subst(edtOriginal->Text, edtKey->Text, memoContent);

Debuger description:

First chance exception at $77303E28. Exception class EInvalidCast with
message ‘Invalid class typecast’. Process Project1.exe (7476)

I’ve found the exact position of the problem. It is when using Memo->Lines->Count for comparison into the for structure. To make sure I created an integer variable and tried to equalize with the Count property and using it into for (even casting like int(Memo->Lines->Count)). However I also got the error. (Now using debugger). I strongly believe it shouldn’t be happening, once the property Count returns an integer value (and I used this expresion many times).

Since now, THANKS A LOT.

Comments are closed.