473,587 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 confused,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
Jun 27 '08 #1
7 2063

"Ryanivanka " <sa********@gma il.comescribió en el mensaje
news:c1******** *************** ***********@y21 g2000hsf.google groups.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 confused,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
Jun 27 '08 #2
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",a nd 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ÔÂ17ÈÕ, ÉÏÎç11ʱ45·Ö, "Cholo Lennon" <chololen....@h otmail.comwrote :
"Ryanivanka " <sarah.h...@gma il.comescribi¨® en el mensajenews:c1* *************** *************** ***@y21g2000hsf .googlegroups.c om...
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 confused,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
Jun 27 '08 #3
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",a nd 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

Jun 27 '08 #4
Ryanivanka wrote:
so I wonder if compiler make that call using "fastcall" not
"stdcall",a nd 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
Jun 27 '08 #5
hi,

you two both asked about the prototype in delphi and c++,they are as
follows.
------------------------in c++ .h-------------------------
extern "C" {
__declspec(dlli mport) 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
Jun 27 '08 #6
Ryanivanka wrote:
------------------------in c++ .h-------------------------
extern "C" {
__declspec(dlli mport) HMODULE __stdcall func1();
}
------------------------------------------------------------

-----------------------in delphi-----------------------------
function func1: dword; stdcall;
----------------------------------------------------------------
They look compatible.

I just don't understand how you were able to link a Delphi DLL
statically. Borland/CodeGear uses OMF-type LIB files, VC++ uses the COFF
format. They're not compatible. Not even for simple import libraries.

So you either have a bug in the Delphi function, or in the import
library. I recommend that you forget about that LIB, and just load the
DLL dynamically:

HINSTANCE dll = LoadLibrary("c: \\path\\to\\my. dll");
typedef HMODULE (__stdcall * PtrFunc1)();
PtrFunc1 func1 =
reinterpret_cas t<PtrFunc1>(Get ProcAddress(dll , "func1"));
....
// use func1 as if it was a regular C function
....
FreeLibrary(dll );

Of course you have to use GetProcAddress for all the other functions
that the DLL might export as well. It may be a little bit of an extra
work (lots of typing), but you can be 100% sure that the functions are
imported correctly.

I just don't know where your LIB file is coming from, it can't be coming
from Delphi. LIB and OBJ files are generally not portable enough.

Tom
Jun 27 '08 #7
thank you all.....I found out there are something wrong about the
function.
So I just want to realize it by myself.Thank you all.
On 4ÔÂ19ÈÕ, ÉÏÎç5ʱ20·Ö, Tamas Demjen <tdem...@yahoo. .comwrote:
Ryanivanka wrote:
------------------------in c++ .h-------------------------
extern "C" {
__declspec(dlli mport) HMODULE __stdcall func1();
}
------------------------------------------------------------
-----------------------in delphi-----------------------------
function func1: dword; stdcall;
----------------------------------------------------------------

They look compatible.

I just don't understand how you were able to link a Delphi DLL
statically. Borland/CodeGear uses OMF-type LIB files, VC++ uses the COFF
format. They're not compatible. Not even for simple import libraries.

So you either have a bug in the Delphi function, or in the import
library. I recommend that you forget about that LIB, and just load the
DLL dynamically:

HINSTANCE dll = LoadLibrary("c: \\path\\to\\my. dll");
typedef HMODULE (__stdcall * PtrFunc1)();
PtrFunc1 func1 =
reinterpret_cas t<PtrFunc1>(Get ProcAddress(dll , "func1"));

...
// use func1 as if it was a regular C function
...
FreeLibrary(dll );

Of course you have to use GetProcAddress for all the other functions
that the DLL might export as well. It may be a little bit of an extra
work (lots of typing), but you can be 100% sure that the functions are
imported correctly.

I just don't know where your LIB file is coming from, it can't be coming
from Delphi. LIB and OBJ files are generally not portable enough.

Tom
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3970
by: Hafez | last post by:
Hi there every body I'm new in programming in windows. I know how to program with C and C++ in DOS but I don't know select which one for programming in windows: Visual C++ ,C# or Delphi. thanks.
102
5638
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in...
1
3484
by: Piotr Nabielec | last post by:
Hi, I have very big problem with importing delphi dll into C# project... I got a Velleman K8000 card with k8d.dll that makes all communication for me. Now I'm trying to import it into C#, but I cannot get it worked... (I'm not quite sure if it is delphi dll!) Everything about it you can see at: http://zeus.polsl.gliwice.pl/~eloy/csharp/
5
2332
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and run this works fine. but if build in unicode release or release this does't work. IS THERE ANY PROBLEM OF INSTANTIATING TEMPLATE CLASSES
22
4827
by: glenn | last post by:
I have a COM Server that I've written based on information from the book ..NET and COM / the complete Interop Guide. I have gotten the project to compile and I've located the regasm.exe program that I was suppose to run against my dll in order to generate a TLB file for the purposes of importing the COM object into other programming lanugages...
2
2505
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter...
0
1179
axtens
by: axtens | last post by:
G'day everyone Recently I used Delphi to wrap libiconv for use in VB6 as a COM object. I'm now using VC++6 to wrap Bill Poser's libuninum and am pretty close to completion. This issue I have is that the Delphi DLL is visible to VBScript but the VC++ one isn't. What changes do I have to make, or what settings are required when using the ATL...
6
3517
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
1729
by: BornTOCode | last post by:
Hello, I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006. The DLL works fine when called from a win32 app. The problem I am encountering is that the string being returned to the caller is in Chinese (No, I'm not...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.