Board index » delphi » Accessing a C++ class via pointer from a Delphi DLL

Accessing a C++ class via pointer from a Delphi DLL

Here's one for the gurus:

I want to access a C++ class from a Delphi DLL - the C++ portion is a
server that passes a pointer to an abstract class via a DLL call.  For
example, the server calls Process(pclass:pointer) in the DLL where
pclass is the pointer to an already initialized class.  The DLL can
then use the methods within the class to perform tasks such as reading
and writing data to/from the server.

What I have found is that the C++ class (which I believe is MS Visual
C++) has essentially the same structure as a Delphi class.  That is,
the pointer to the class points to a memory structure that contains a
pointer to the class methods and then the variables of the class.  The
class methods are a table of pointers to each method.

<classpointer>----><methods table pointer>----><methods table
(pointers to all methods)>

What I have so far is code that can get the address of any of the
methods I want to use.  The problem has been the passing of parameters
to the methods.  I have tried declaring the methods CDECL and STDCALL

What happens is, for example, I call a method that takes two LPCSTRs
as parameters.  The first parameter shows up as "garbage" on the
screen.  The second passed parameter shows the first parameter that
was passed.  For example - proc(a,b) where a='testa' and b='testb'
displays:

<some garbage> as the first parameter and "testa" as the second
parameter.

If I use BASM to make a call to the method it works but I can only
call the method once.  If I call it twice the server generates an
error (what I think is happening is that the DLL is aborting with a
runtime error after the first call).  Here's an example of the BASM
when passing a single parameter:

var s:string;

begin

<set up pointer to method here>
   s:='test';
   asm

      push s
      call pmethod
      pop s

   end;
end;

Again, this displays the correct string once - subsequent calls blow
up.

BTW - if I set up a Delphi program to call the DLL it works flawlessly
(even if I set up the Delphi methods to be STDCALL).

If anyone can shed some light on this I would appreciate it.  

Howie

 

Re:Accessing a C++ class via pointer from a Delphi DLL


Quote
In article <34e452ec.232024...@news.zippo.com>, howie-nos...@nospam-hoot.com wrote:
>Here's one for the gurus:
>What happens is, for example, I call a method that takes two LPCSTRs
>as parameters.  The first parameter shows up as "garbage" on the
>screen.  The second passed parameter shows the first parameter that
>was passed.  For example - proc(a,b) where a='testa' and b='testb'
>displays:

Just wondering - if these are *methods* you're calling here, wouldn't the
first parameter passed to them actually be the 'Self' (in Delphi) or 'this'
(in C++) parameter?  The method has to know what object it's being required to
handle.  Might be that that's the 'garbage' you're encountering :)

  -- Ritchie Annand

(Anti-spam measure - e-mail address contains neither hyphens nor the
words 'at' or 'dot' :)

Other Threads