Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

problem using delphi dll in vc++
hi,
I am using a delphi DLL in vc++,static linked with ".h"and "lib".
the export functions in DLL are "__stdcall",but the function doesn't
work well,it often returns some weird values.
when I add codes as follows,it suddenly works well. why??

DWORD returnAdd;
__asm
{
mov ecx,[ebp+4]
mov returnAdd, ecx
}

I am ,if there is something wrong with stack,I have already
using "__stdcall",why it is still wrong?
I don't even know what those asm code do,I just add them to get some
status about stack.what else they do to the program that I don't know?

thank you .
Ryan

Report this thread to moderator Post Follow-up to this message
Old Post
Ryanivanka
04-17-08 03:31 AM


Re: problem using delphi dll in vc++
"Ryanivanka" <sarah.hhyy@gmail.com> escribió en el mensaje
news:c1b88a80-b80a-4f9a-a30c-46561ca95d74@y21g2000hsf.googlegroups.com...
> hi,
> I am using a delphi DLL in vc++,static linked with ".h"and "lib".
> the export functions in DLL are "__stdcall",but the function doesn't
> work well,it often returns some weird values.
> when I add codes as follows,it suddenly works well. why??
>
>          DWORD returnAdd;
>          __asm
>          {
>                mov ecx,[ebp+4]
>                mov returnAdd, ecx
>          }
>
>  I am ,if there is something wrong with stack,I have already
> using "__stdcall",why it is still wrong?
> I don't even know what those asm code do,I just add them to get some
> status about stack.what else they do to the program that I don't know?
>

It's seems to be a calling convention problem.
I understad that you are using __stdcall in C++ code. Do you use 'stdcall'
modifier in Delphi functions?
Where do you add the asm code? (Before or after a DLL calling?)
Does Delphi compiler generate the import .lib file? or do you obtain it with
some tool?

--
Cholo Lennon
Bs.As.
ARG



Report this thread to moderator Post Follow-up to this message
Old Post
Cholo Lennon
04-17-08 10:35 AM


Re: problem using delphi dll in vc++
hi,Cholo
thank you for your time. :)

yes ,the "stdcall" is also used in that delphi ".h".
the".lib" which I download free version might be compiled in Delphi
environment.

those asm code was put in front of a function (named A()) which
doesn't work well until the asm codes were added. and it's long after
the delphi dll is loaded(before that function ,lots of function from
dll have already been correctly used ).

and the implementation troubled function A() has something to do with
asm,because it read information from calling stack.

so I wonder if compiler make that call using "fastcall" not
"stdcall",and the registers and stack were wrong. but,as I said before
"stdcall" is used in its ".h". Is that enough to convert a delphi
calling convention to a c++ way? or what I should do to convert
".h",".lib" or".dll"?

thank you.

regards,
Ryan



On 4=D4=C217=C8=D5, =C9=CF=CE=E711=CA=B145=B7=D6, "Cholo Lennon" <chololen..
=
.@hotmail.com> wrote:
> "Ryanivanka" <sarah.h...@gmail.com> escribi=A8=AE en el mensajenews:c1b88a=[/color
]
80-b80a-4f9a-a30c-46561ca95d74@y21g2000hsf.googlegroups.com...
>
>
> 
> 
> 
>
> It's seems to be a calling convention problem.
> I understad that you are using __stdcall in C++ code. Do you use 'stdcall'=[/color
]

> modifier in Delphi functions?
> Where do you add the asm code? (Before or after a DLL calling?)
> Does Delphi compiler generate the import .lib file? or do you obtain it wi=[/color
]
th
> some tool?
>
> --
> Cholo Lennon
> Bs.As.
> ARG


Report this thread to moderator Post Follow-up to this message
Old Post
Ryanivanka
04-17-08 10:35 AM


Re: problem using delphi dll in vc++
Ryanivanka wrote:
> hi,Cholo
> thank you for your time. :)
>
> yes ,the "stdcall" is also used in that delphi ".h".
> the".lib" which I download free version might be compiled in Delphi
> environment.
>
>
> those asm code was put in front of a function (named A()) which
> doesn't work well until the asm codes were added. and it's long after
> the delphi dll is loaded(before that function ,lots of function from
> dll have already been correctly used ).
>
> and the implementation troubled function A() has something to do with
> asm,because it read information from calling stack.
>
> so I wonder if compiler make that call using "fastcall" not
> "stdcall",and the registers and stack were wrong. but,as I said before
> "stdcall" is used in its ".h". Is that enough to convert a delphi
> calling convention to a c++ way? or what I should do to convert
> ".h",".lib" or".dll"?
>

You can't convert Delphi calling convention from C++. You have to define the
same calling convention on both sides (Delphi and C++). Be warned that ms
fastcall is distinct to borland fastcall
(http://en.wikipedia.org/wiki/X86_calling_conventions)

Can you show us the function prototype? (delphi & C++). Also would be
interesting to see how VC++ generate the calling (with and without the asm
code). Use the dissasembly window to cut the code.

BTW, Which versions of VC and Delphi are you using?


--
Cholo Lennon
Bs.As.
ARG




Report this thread to moderator Post Follow-up to this message
Old Post
Cholo Lennon
04-18-08 12:33 AM


Re: problem using delphi dll in vc++
Ryanivanka wrote:

> so I wonder if compiler make that call using "fastcall" not
> "stdcall",and the registers and stack were wrong. but,as I said before
> "stdcall" is used in its ".h". Is that enough to convert a delphi
> calling convention to a c++ way? or what I should do to convert
> ".h",".lib" or".dll"?

In a function call both sides have to agree about the "protocol"
details. It's not enough to add __stdcall to the .h file, you also have
to do it on the Delphi side. For example,

procedure SayHi; stdcall;
begin
ShowMessage("Hi");
end;

This would be declared as void __stdcall SayHi(); in the .h file. If you
miss the stdcall from either side, it won't work. Optionally, you could
use cdecl on both sides, Delphi can handle that. As Cholo said, don't
use fastcall, it's not compatible across compilers.

In addition to making the calling conventions compatible, you also have
to make sure that the argument sizes and alignments match. For example,
Borland's enumerations have the size of 1 byte by default, while in VC++
an enum is treated as an integer (4 bytes). String arguments must be
passed as PChar/char*. Some Delphi floating point types aren't
compatible with VC++ floating point types. Delphi functions are able to
return types not allowed in C/C++ (such as arrays). If you allocate
memory in Delphi, you must delete it from Delphi, too.

I think you should show us your C and Delphi declarations, and we should
be able to compare them for you.

Delphi classes must be flattened into C-style functions before you can
use them from VC++, or they can be wrapped into COM objects.

Tom

Report this thread to moderator Post Follow-up to this message
Old Post
Tamas Demjen
04-18-08 12:33 AM


Re: problem using delphi dll in vc++
hi,

you  two both asked about the prototype  in delphi and c++,they are as
follows.
------------------------in c++ .h-------------------------
extern "C" {
__declspec(dllimport) HMODULE  __stdcall func1();
}
------------------------------------------------------------

-----------------------in delphi-----------------------------
function func1: dword; stdcall;
----------------------------------------------------------------

and I can't change anything about the delphi dll,but I thought it's in
right prototype in delphi,right?

in c++, I just use the function as "  HMODULE hmod=func1();",then the
answer is wrong ,but  the stack is ok(the function could return to
right place).

with the asm codes,
------------------------------------------
DWORD returnAdd;
__asm
{
mov ecx,[ebp+4]
mov returnAdd, ecx
}
-----------------------------------------
it change the register "ecx" value,then the func1() would works well.
and the implementation of fun1() in delphi might be inline asm too.(I
expect that because the function is reading stack values and I don't
know if it use any register.)

and I am not sure the version of delphi,but I use VS.net as the C++
IDE. If I call the delphi dll in a MFC program,would that be something
different?

should I disassemble the delphi dll to get some details?

thank you all,

regards,
Ryan

Report this thread to moderator Post Follow-up to this message
Old Post
Ryanivanka
04-18-08 10:03 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

VC++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:04 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.