代码之家  ›  专栏  ›  技术社区  ›  HX_unbanned

从DPR或Delphi中的其他函数/过程访问子函数/过程

  •  3
  • HX_unbanned  · 技术社区  · 16 年前

    有没有办法从“外部世界”访问它们-dpr或单元中的其他功能/程序?

    另外-哪种方式需要更多的计算和空间来编译文件?

    function blablabla(parameter : tparameter) : abcde;
     procedure xyz(par_ : tpar_);
     begin
      // ...
     end;
    begin
     // ...
    end;
    
    procedure albalbalb(param : tparam) : www;
    begin
     xyz(par_ : tpar_); // is there any way to make this function public / published to access it therefore enabling to call it this way?
    end;
    
    // all text is random.
    
    // also, is there way to call it from DPR in this manner?
    
    // in C++ this can be done by specifing access mode and/or using "Friend" class .. but in DELPHI?
    
    4 回复  |  直到 16 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    注:嵌入式例程<&燃气轮机;私有/受保护的方法。

    procedure DoThis;
    
    function DoThat : Boolean;
    begin
      // This Routine is embedded or internal routine.
    end;
    begin
    
    // DoThat() can only be accessed from here no other place.
    
    end;
    

    不管可见性如何,都可以通过RTTI使用delphi2010调用类上的方法。我已经详细说明了如何在 this article .

    This Question 有更多的细节和很好的例子代码 accepted answer .

    如果你在两个不同的单位,你可以使用受保护的方法黑客访问受保护的方法。详情见 this article

        2
  •  7
  •   user160694 user160694    16 年前

    C++中的“朋友”类/函数是不同的——它们是类访问方法,而在示例中,您使用的是简单的过程/函数。 在Delphi中,在同一单元中声明的所有过程/类都自动成为“friend”,除非 严格保密

      type
        TExample = class
        private
          procedure HelloWorld;
        public
        ...
        end;
    
      implementation
    
        function DoSomething(AExample: TExample);
        begin
          // Calling a private method here works
          AExample.HelloWordl;
        end;
    
        3
  •  3
  •   Kryvich    13 年前

    是的,您可以从外部世界访问嵌套在其他(父)子例程中的子例程。虽然有点棘手。我在网上找到了这个方法。

    如何将嵌套例程作为过程参数传递(32位)

    Delphi通常不支持将嵌套例程作为过程参数传递:

    // This code does not compile:
    procedure testpass(p: tprocedure);
    begin
      p;
    end;
    procedure calltestpass;
     procedure inner;
     begin
       showmessage('hello');
     end;
    begin
      testpass(inner);
    end;
    

    // This code compiles and runs OK
    procedure testpass(p: pointer);
    begin
      tProcedure(p);
    end;
    procedure calltestpass;
     procedure inner;
     begin
       showmessage('hello');
     end;
    begin
      testpass(@inner);
    end;
    

    但是,上面的示例中有一个陷阱-如果“内部”例程引用在从testpass调用“内部”过程之前推送到堆栈上的任何变量(calltestpass参数-如果有,或calltestpass中的局部变量-如果有),则系统很可能崩溃:

    // This code compiles OK but generates runtime exception (could even be
    //  EMachineHangs :-) )
    procedure testpass(p: pointer);
    begin
      tProcedure(p);
    end;
    procedure calltestpass;
    var msg: string;
     procedure inner;
     begin
       msg := 'hello';
       showmessage(msg);
     end;
    begin
      testpass(@inner);
    end;
    

    被对testpass例程和“内部”过程的调用“破坏”了 错误地计算参数和局部变量的位置 (请不要责怪德尔福)。 解决方法是在开始之前设置正确的堆栈上下文 从“testpass”内部调用“inner”。

    // This code compiles and runs OK
    {$O-}
    procedure testpass(p: pointer);
    var callersBP: longint;
    begin
      asm // get caller's base pointer value at the very beginning
        push dword ptr [ebp]
        pop callersBP
      end;
    // here we can have some other OP code
      asm // pushes caller's base pointer value onto stack and calls tProcedure(p)
        push CallersBP
        Call p
        Pop  CallersBP
      end;
    // here we can have some other OP code
    end;
    {$O+}
    
    procedure calltestpass;
    var msg: string;
     procedure inner;
     begin
       msg := 'hello';
       showmessage(msg);
     end;
    begin
      testpass(@inner);
    end;
    

        4
  •  2
  •   Rob Kennedy    16 年前

    不,没有办法按你的要求去做。这个 xyz 只有 由封闭 blablabla xyz公司 不在范围内,无法命名。如果C++允许嵌套函数,也就没有任何方法可以引用它,就像没有办法从当前的翻译单元外部引用静态链接的函数一样。

    xyz公司 从外面 布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉 功能,然后移动 外面。如果需要从当前单元外部调用它,那么需要在单元的接口部分声明该函数。然后,将该单元添加到外部代码的 uses 你可以打电话给我 xyz公司 无论你想从哪里,甚至是DPR文件。

    如果 xyz公司 是指 布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉 xyz公司 否则将无法访问它们。

    概念 访问权限修饰词 因为我们不是在谈论课程,所以在这里并不重要。单位有 接口 分区,实际上与 公众的 私有的

    推荐文章