|
List Info
Thread: (
|
|
| ( |

|
2006-05-18 06:08:24 |
|
> i'm using a Dll wrote in C++Builder.
> I'm growing that Dll (i mean i have the source code of the Dll, so i can
> manipulate it).
>
> I wanted to add in the call of a function of Dll a "pointer to method",
>
> (for example :
>
> typedef __stdcall void (__closure *TmyMethod)(int,int);
>
> #define NO_MAGLE extern "C"
Declaring a function to use "C" linkage doesn't disable name mangling.
It just uses different, less drastic name mangling.
> #define DLL_EXPORT __declspec(dllexport)
> #define DLL_PORT NO_MANGLE DLL_EXPORT
>
> DLL_PORT __stdcall void FuncDll(TmyMethod F){
> ....
> F(1,2);
> ....
> }
>
> )
>
> but i didn't want to trust that C++Builder and Delphi use the same way to
> manipulate TMethod (and similars),
So all this hassle is because you don't trust Borland to make both
products work together? Are you aware that components compiled in Delphi
can be used in C++ Builder without recompiling? Since nearly every
component has event properties, that's clear evidence that C++ Builder
and Delphi use the same format for method pointers.
Note that TMethod is not a method pointer. It's just a record. That
record's structure happens to represent the internal structure of a
method pointer, but Delphi also supports native types that actually
*are* method pointers. They're the "of object" types. C++ Builder uses
the "__closure" keyword for the same purpose. Forget about TMethod. Use
the real method-pointer types.
> so i semplified the type of pointer using
> a:
>
> typedef __stdcall void (*TmyProcedureCallingMethod)(int,int,void * PMethod);
Void* is almost never the correct choice for a type. Figure out what
it's *really* supposed to point at, and then use that type, not void.
> DLL_PORT __stdcall void FuncDll(TmyProcedureCallingMethod P,void * PMethod){
> ....
> P(1,2,PMethod);
> ....
> }
>
> and (in Delphi)
>
> <<
>
> type TMyMethod = procedure(a : integer;b : integer) of object;
> type PTMyMethod = ^TMyMethod;
So you "simplified" the C++ side, but you didn't do anything on the
Delphi side?
> procedure aMyProcedure(a : integer;b : integer;PM : PTMyMethod); stdcall;
> begin
> PTMyMethod^(a,b);
Do you mean PM^(a,b)?
> end;
>
> calling the Dll function in this way:
>
> ...
> FuncDll( aMyObject.aMyMethod);
In your C++ code, FuncDll is declared to take two parameters. Here
you've only passed it one. That's going to cause problems no matter what
the rest of your code is.
> Well, before to use this way to work, i tried a little code avoiding the
> Dll, to check how
> to use the pointers to methods, to make it "traveling" fine between
> functions, so i just used a
> little code in delphi. In fact all worked already fine with other types of
> pointers (between Delphi
> and my Dll) that i had to use in the last days.
>
> The little experimental code in Delphi is :
>
>
> ....
>
> procedure TForm1.App(a : integer);
> var pSelf : Pointer;
> begin
> pSelf := Self; //only for debug goal
>
> if (Self.Height < 543) or (pSelf = Pointer(52)) then
If this App method is supposed to be a do-nothing method merely to let
you confirm that the method gets called, then don't have it do weird
things like get a pointer to Self. Do *simple* things in your functions
(like MessageBeep, or OutputDebugString). That way, you can be confident
that they're not interfering with the part of your program you're
actually trying to test.
> Self.Height := Self.Height div 2;
> end;
>
> type TP = procedure(a : integer) of object;
> type P_TP = ^TP;
>
> procedure TForm1.Button1Click(Sender: TObject);
> var
> m : procedure(a : integer) of object;
> a : integer;
> m1 : P_TP;
> begin
> App(0); //for debug goal
> m := App;
> m1 := m;
Applying a single " " operator to a method pointer merely tells the
compiler to treat it as a pointer instead of trying to call the method.
Double the operator to tell Delphi you really want a pointer to the m
variable:  m.
Also, turn on the "typed operator" compiler option.
> a := 1027;
>
> m(0);
>
> P_TP(m1)^(a); //this call raises an exception.
Which exception?
--
Rob
|
[1]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|