Board index » delphi » Am I calling my interface correctly?

Am I calling my interface correctly?

I have the following excerpts from a XXX_TLB.pas file:

    unit XXX_TLB;

    interface

    uses Windows, ActiveX, Classes, Graphics, OleCtrls, StdVCL,
      YYY_TLB, ZZZ_TLB, VBA_TLB;

      _HPFile = interface(IDispatch)
        ['{43F0CE3B-54AC-11D3-803C-00104BB6FB94}']
        procedure Load(const Path: WideString); safecall;
      end;

      CoHPFile = class
        class function Create: _HPFile;
        class function CreateRemote(const MachineName: string): _HPFile;
      end;

    implementation

    uses ComObj;

    class function CoHPFile.Create: _HPFile;
    begin
      Result := CreateComObject(CLASS_HPFile) as _HPFile;
    end;
    end.

When I execute the following code:

    function SomeFunc : Boolean;
    var
      HPFile : _HPFile;

    begin
      HPFile := CoHPFile.Create;
      try
        path := 'c\001.xxx';
        HPFile.Load (path);
        ShowMessage('Success');
      except
        One e.exception do
          ShowMessage (e.message);
      end; //try except
    end;

CoHPFile.Create returns a pointer ($XXXXXX) as _HPFile as I would
expect, but calling the load method generates an Access Violation.
I am able to debug the DLL and the EXE at the same time and have found
that control is not leaving the calling function when the AV occurs.
This seems to indicate that the pointer returned by CoHPFile.Create
is not really a valid pointer to an _HPFile interface.

My questions are:
1.  Am I calling the method correctly?
2.  Am I calling CoHPFile.Create correctly?
3.  When I step into COHPFile.Create (f7) CreateComObject
    is called, however, the compiler doesn't step into that
    function.  Is CreateComObject being called from somewhere
    else?
4.  Is there another step necessary to call methods on the
    interface, _HPfile?
5.  If I declare HPFile as a variant, the file loads without
    incident (almost).  In the IDE I get a couple of inexact
    result errors and then the Success messagebox.  Outside the
    IDE, these errors are apparently ignored, because the
    Success message comes up without incident.

 

Re:Am I calling my interface correctly?


Your code looks ok. I suggest this: Unregister your server. Open your server
project, refresh the type library, then rebuild your server. Register your
server. Open your client project, rebuild it and make sure the XXX_TLB.pas
file it references is the updated one from the server. Then run the client.

have fun
--
Binh Ly
http://www.castle.net/~bly/Programming/Delphi

Quote
Cliff Estes <cliff.es...@sgsi.com> wrote in message

news:7pcneq$2a16@forums.borland.com...
Quote
> I have the following excerpts from a XXX_TLB.pas file:

>     unit XXX_TLB;

>     interface

>     uses Windows, ActiveX, Classes, Graphics, OleCtrls, StdVCL,
>       YYY_TLB, ZZZ_TLB, VBA_TLB;

>       _HPFile = interface(IDispatch)
>         ['{43F0CE3B-54AC-11D3-803C-00104BB6FB94}']
>         procedure Load(const Path: WideString); safecall;
>       end;

>       CoHPFile = class
>         class function Create: _HPFile;
>         class function CreateRemote(const MachineName: string): _HPFile;
>       end;

>     implementation

>     uses ComObj;

>     class function CoHPFile.Create: _HPFile;
>     begin
>       Result := CreateComObject(CLASS_HPFile) as _HPFile;
>     end;
>     end.

> When I execute the following code:

>     function SomeFunc : Boolean;
>     var
>       HPFile : _HPFile;

>     begin
>       HPFile := CoHPFile.Create;
>       try
>         path := 'c\001.xxx';
>         HPFile.Load (path);
>         ShowMessage('Success');
>       except
>         One e.exception do
>           ShowMessage (e.message);
>       end; file://try except
>     end;

> CoHPFile.Create returns a pointer ($XXXXXX) as _HPFile as I would
> expect, but calling the load method generates an Access Violation.
> I am able to debug the DLL and the EXE at the same time and have found
> that control is not leaving the calling function when the AV occurs.
> This seems to indicate that the pointer returned by CoHPFile.Create
> is not really a valid pointer to an _HPFile interface.

> My questions are:
> 1.  Am I calling the method correctly?
> 2.  Am I calling CoHPFile.Create correctly?
> 3.  When I step into COHPFile.Create (f7) CreateComObject
>     is called, however, the compiler doesn't step into that
>     function.  Is CreateComObject being called from somewhere
>     else?
> 4.  Is there another step necessary to call methods on the
>     interface, _HPfile?
> 5.  If I declare HPFile as a variant, the file loads without
>     incident (almost).  In the IDE I get a couple of inexact
>     result errors and then the Success messagebox.  Outside the
>     IDE, these errors are apparently ignored, because the
>     Success message comes up without incident.

Other Threads