2

我需要访问受保护的 TControlItem.InternalSetLocation。我德尔福你会做

type
  THackControlItem = class(TControlItem);

你如何在 C++ Builder 中做到这一点?

4

2 回答 2

1

This is a nice trick I think Remy Lebeau showed me but can not find the QA anymore...

//---------------------------------------------------------------------------
#ifndef _TDirectMemoryStream
#define _TDirectMemoryStream
class TDirectMemoryStream:TMemoryStream // just for accessing protected SetPointer
    {
public:
    void SetMemory(BYTE *ptr,DWORD siz) { SetPointer(ptr,siz); Position=0; };
    };
#endif
//---------------------------------------------------------------------------

You simply create new class that is descendant of the class you want to access. Now just add get/set functions for the protected members ...

Now usage:

TMemoryStream *mem=new TMemoryStream(); // original class instance you want to access

// overtype to our new class and access/use you get/set ...
((TDirectMemoryStream*)(mem))->SetMemory(hdr->lpData,hdr->dwBytesUsed);

delete mem; // release if not needed anymore

I am using it btw to feed a memory stream with custom memory data hdr coming from vfw camera so I can properly decode it using TJPEGImage class instead of writing the data into file and loading it back each frame ...

Here another example:

class A
    {
protected:
    int x;
public:
    int getx(){ return x; }
    };

class hack_A:A
    {
public:
    void setx(int _x){ x=_x; }
    };

void test()
    {
    A a;
    hack_A *ha=(hack_A*)&a;
    ha->setx(10);
    a.getx(); // print the x somwhere
    }

However this will not work for private members ... In such case its doable too but requires access to A source code:

class A
    {
protected:
    int x;
private:
    int y;
public:
    int getx(){ return x; }
    int gety(){ return y; }
    friend class hack_A;        // but this one requires access to A soourcecode
    };

class hack_A:A
    {
public:
    void setx(int _x){ x=_x; }
    void sety(int _y){ y=_y; }
    };

void test()
    {
    A a;
    hack_A *ha=(hack_A*)&a;
    ha->setx(10);
    ha->sety(20);
    a.getx(); // print the x somwhere
    a.gety(); // print the x somwhere
    }
于 2018-11-21T10:29:14.250 回答
1

与在 Delphi 中一样,您需要继承该类,但还需要覆盖并公开受保护的函数。但是,我不建议在生产代码中使用它。

class THackControlItem : public TControlItem
{
public:
    void __fastcall InternalSetLocation(int AColumn, int ARow, bool APushed, bool MoveExisting)
    {
        TControlItem::InternalSetLocation(AColumn, ARow, APushed, MoveExisting);
    }
};

在节目中

TControlItem* ci = ...;
static_cast<THackControlItem*>(ci)->InternalSetLocation(...);
于 2018-11-21T15:12:04.793 回答