Board index » delphi » Calling 16-bit DLL's from Delphi32?

Calling 16-bit DLL's from Delphi32?

Hi there.

I have written this one Delphi program which calls some
16-bit DLL's (made with C, I guess?). The program works just
fine.

Question: Can I just compile my program with
Delphi 2.0 into 32-bit and still call the same
DLL's? I'm aware of the potential function parameter
size-problems and I can handle those,
but will there be any problems with the calling
mechanism itself?

Peace,
       Petri

--
-------------------------------------------------
<><- Petri J. Riipinen   -  Digitronics Ltd. <><-
<><- Rauhantie 3 C 19    -  28400 Ulvila     <><-
<><- Finland - +358-39-5389799 / 940-5476076 <><-
<><-             petr...@sik.ppoy.fi         <><-
-------------------------------------------------

 

Re:Calling 16-bit DLL's from Delphi32?


Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
 question? Thanks.
Quote
Petri Riipinen wrote:

> Hi there.

> I have written this one Delphi program which calls some
> 16-bit DLL's (made with C, I guess?). The program works just
> fine.

> Question: Can I just compile my program with
> Delphi 2.0 into 32-bit and still call the same
> DLL's? I'm aware of the potential function parameter
> size-problems and I can handle those,
> but will there be any problems with the calling
> mechanism itself?

> Peace,
>        Petri

> --
> -------------------------------------------------
> <><- Petri J. Riipinen   -  Digitronics Ltd. <><-
> <><- Rauhantie 3 C 19    -  28400 Ulvila     <><-
> <><- Finland - +358-39-5389799 / 940-5476076 <><-
> <><-             petr...@sik.ppoy.fi         <><-
> -------------------------------------------------

Re:Calling 16-bit DLL's from Delphi32?


It is possible to call 16 bit DLL's from 32 bit progs and the other way
around. It's no fun, and it's not portable (3.1, 95 does it differently,
NT doesn't at all).
Take a look at
"Mix 16-bit and 32-bit Code in Your Applications with the
 Win32s Universal Thunk"
In the November '93 issue of MSJ.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfi.infonet.no
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:Calling 16-bit DLL's from Delphi32?


Quote
zh...@newton.tuns.ca wrote:
>Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
> question? Thanks.

>Petri Riipinen wrote:

>> Hi there.

>> I have written this one Delphi program which calls some
>> 16-bit DLL's (made with C, I guess?). The program works just
>> fine.

>> Question: Can I just compile my program with
>> Delphi 2.0 into 32-bit and still call the same
>> DLL's? I'm aware of the potential function parameter
>> size-problems and I can handle those,
>> but will there be any problems with the calling
>> mechanism itself?

>> Peace,
>>        Petri

>> --
>> -------------------------------------------------
>> <><- Petri J. Riipinen   -  Digitronics Ltd. <><-
>> <><- Rauhantie 3 C 19    -  28400 Ulvila     <><-
>> <><- Finland - +358-39-5389799 / 940-5476076 <><-
>> <><-             petr...@sik.ppoy.fi         <><-
>> -------------------------------------------------

The short answer is yes, but it ain't easy.

I had to explore this for the Win95 product we did last year at work.  We
had some DLLs that we wanted to call that were 16 bit.  Also, we were doing
this in C++/MFC, however, the process would be identical in Delphi.

I'll tell you how to do it, but be warned, it would be easier to just take
everything you have (source), port it, and get 32 bit implementations of
any 3rd party products.

The fundamental problem you're dealing with is tow fold:

  1) differences in type sizes (i.e. 16 bit integers vs 32 bit integers).
When you call a function, you can see that the arguments will get hosed
because of the size differences.

  2) Data segments in the 16 bit world.  Remember that in the 32 bit world,
everything is a flat memory space; in 16 bit domain, everything has a
segment address and offset within that segment.

In the early days of NT, the Evil Empire wanted all developers to switch to
32 bits overnight.  Great howls of rage were vented upon the Remondians.
Seems like the rabble out there didn't share the idea that they should just
port everything at once.  Because of the outcry, MS came up with a
solution.

On the C++ Win95/NT SDK, there is a subdirectory that contains a critter
called the 'Thunk Compiler'.  This is a command line tool (i.e. no windows
hosted UI).

So to start this explanation, let's say you have a 32 bit exe you just
wrote and you want to call a 16 bit dll.  What you need to do is as
follows:

1)  Create a Thunk script file that declares all procedures and functions
in the 16 bit dll that you want to call.

2) Run the thunk compiler against this script.  It will produce 2 asm
files: one for the 32 bit world and one for the 16 bit world.

The assembly files contain bits of code that adjust the size types of
parameters in the function calls and take care of the segment addressing
problem.  It does this bi-directionally.

3)  Run MASM on the asm files to generate OBJ files.

4) Now here's where it gets tricky.  Remember that you started with a 32
bit exe and a 16 bit dll.  You need to create two more files: a 32 bit dll
and a 16 bit dll.  So you end up with four files.  These new dlls call a
"Thunk" function that sets up the thunks needed to pull off the variable
and segment mapping.  The obj files are linked into the new dll projects.

5)  Once all that is built, your original 32 bit exe can call the original
16 bit dll, albiet through the intermediary 32 bit and 16 bit dlls.

I told you this wouldn't be pretty.

Also, the thunk compiler is virtually brain dead.  We had function pointers
in our headers... turns out it can't handle it.  It give informative
messages like 'Syntax error' for just about everything that it bombs on.
And it bombs often!

There is a txt file on the SDK that explains all of this.  The doc number
is Q125715.  You might find it at the MS ftp site under the Win32 SDK
Knowledge Base.

It's been months since I've done this (early last summer) so this is all
off the top of my head.  But as I said, I found it easier to just port to
32 bits.  Once it's done, it's done.

later

david sampson

//*************************************************************
  David Sampson
  Attachmate Corp.
  (770) 422-4244

  dsamp...@dca.com     or    dsamp...@atlanta.com
//*************************************************************

Re:Calling 16-bit DLL's from Delphi32?


Quote
zh...@newton.tuns.ca wrote:
>Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
> question? Thanks.

Yes they can. Check CALL32 on the Delphi Superpage.
Francis Pauwelyn
Francis.Pauwe...@ping.be
100572....@compuserve.com
Http://ourworld.compuserve.com/homepages/Francis_Pauwelyn
----------
If you want it World, and you want it Wide, don't forget that Netscape's not the only ride...

Re:Calling 16-bit DLL's from Delphi32?


Quote
Martin Larsson <martin.larsson@delfi_data.msmail.telemax.no> wrote:
>It is possible to call 16 bit DLL's from 32 bit progs and the other way
>around. It's no fun, and it's not portable (3.1, 95 does it differently,
>NT doesn't at all).

I have succesfully called the NT newshell commdlg from 16 bit Delphi.
So I guess NT does it after all...

Francis Pauwelyn
Francis.Pauwe...@ping.be
100572....@compuserve.com
Http://ourworld.compuserve.com/homepages/Francis_Pauwelyn
----------
If you want it World, and you want it Wide, don't forget that Netscape's not the only ride...

Re:Calling 16-bit DLL's from Delphi32?


Quote
petr...@sik.ppoy.fi (Petri Riipinen) wrote:
>Hi there.

>I have written this one Delphi program which calls some
>16-bit DLL's (made with C, I guess?). The program works just
>fine.

>Question: Can I just compile my program with
>Delphi 2.0 into 32-bit and still call the same
>DLL's? I'm aware of the potential function parameter
>size-problems and I can handle those,
>but will there be any problems with the calling
>mechanism itself?

>Peace,
>       Petri

The following is possible:

Win32s:  16-bit to 32-bit
         32-bit to 16-bit

Win95:   16-bit to 32-bit
         32-bit to 16-bit

WinNT:   16-bit to 32bit only

Even worse is that the methods are different for all three cases,
except for the fact that the NT (generic thunks) method can be used
under 95 (95 also support a second method for 16 to 32-bit calls).

Best option is probably to use the generic thunks (works on NT and 95,
and who cares about Win32s anyway) for 16 to 32-bits, and then write a
16-bit wrapper EXE for the 16-bit that uses DDE or OLE Automation to
handle the interface between the 32-bit application and the 16-bit
DLL. Note that this must be an EXE file, since the 16-bit side cannot
be in the same address space as the 32-bit application.

When all said and done, if you have the source then convert everything
to 32-bits, else try your supplier to get 32-bit versions, otherwise
start crying ;->.

Good luck!
===========================================================
Simon Wilson                  Technisoft Ltd.
si...@techsoft.demon.co.uk    Macclesfield
Consultancy Services          England
Windows (NT), OOP, OOA        +44 1625 434533
===================================================

Re:Calling 16-bit DLL's from Delphi32?


Will this work under WIN95?  I thought WIN95 did not support THUNKING?

   Thanks in advance,

        Jason

Re:Calling 16-bit DLL's from Delphi32?


Quote
zh...@newton.tuns.ca wrote:

> Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
>  question? Thanks.

Yes, you can.  There are a variety of thunking techniques available under
different Win32 platforms (Win32s, Win 95, Win NT).  For calling 32-bit
DLLs from 16-bit EXEs under Win 95 and Win NT, the easiest solution I've
found is the Generic Thunk.  You can get info on this from the MSDN, or
(to insert a shameless plug) I'm developing code that does this which will
appear in the 2nd edition of Delphi Developer's Guide.
--

        -Steve Teixeira
         steix...@borland.com

Re:Calling 16-bit DLL's from Delphi32?


Quote
Steve Teixeira wrote:

> zh...@newton.tuns.ca wrote:

> > Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
> >  question? Thanks.

> Yes, you can.  There are a variety of thunking techniques available under
> different Win32 platforms (Win32s, Win 95, Win NT).  For calling 32-bit
> DLLs from 16-bit EXEs under Win 95 and Win NT, the easiest solution I've
> found is the Generic Thunk.  You can get info on this from the MSDN, or
> (to insert a shameless plug) I'm developing code that does this which will
> appear in the 2nd edition of Delphi Developer's Guide.
> --

Oh, BTW, you need to use a Flat Thunk (using the MS Thunk Compiler) to call
16-bit DLLs from 32-bit EXEs under 95 and NT.
--

        -Steve Teixeira
         steix...@borland.com

Re:Calling 16-bit DLL's from Delphi32?


Quote
Jason Irby <Ja...@mps.lfwc.lockheed.com> wrote:
>Will this work under WIN95?  I thought WIN95 did not support THUNKING?

>   Thanks in advance,

>        Jason

Yes it will.  There are differences between NT and Win95 thunking.

I was doing this in the Win95 environment, but our target was both
platforms.  When I was first learning how to do this, I did it on my NT
box.  Then I moved the exact code over to the Win95 box and recompiled.
Worked fine.

david sampson
//*************************************************************
  David Sampson
  Attachmate Corp.
  (770) 422-4244

  dsamp...@dca.com     or    dsamp...@atlanta.com
//*************************************************************

Re:Calling 16-bit DLL's from Delphi32?


Quote
zh...@newton.tuns.ca wrote:
>Also, can 16bit appication calls 32 bit DLL? Can some expert answer such
> question? Thanks.

One other thing about 32 bit  calling 16 bit stuff...  I was shocked to
find out that Win95 apparently has a bunch of 16 bit code out there.  So
that proves it can be done.  Look at this site for details:

http://www.ora.com/windows/feat/dirty.html

david sampson

//*************************************************************
  David Sampson
  Attachmate Corp.
  (770) 422-4244

  dsamp...@dca.com     or    dsamp...@atlanta.com
//*************************************************************

Re:Calling 16-bit DLL's from Delphi32?


Quote
Jason Irby wrote:

> Will this work under WIN95?  I thought WIN95 did not support THUNKING?

There are three types of thunking available under different Win32
platforms:

Universal Thunking, available under Win32s, allows you to call 16-bit code
from 32-bit code.  While this type of thunking is API-based, it's not of
much use to Delphi programmers since it doesn't work under Win95 or NT.

Generic Thunking, available under Win95 and NT, allows calling of 32-bit
DLLs from 16-bit EXEs.  You can also perform callbacks back into the 16-bit
code.  This type of thunking is also API-based.

Flat thunking is available only under Win95.  While this type of thunking
allows you to call 16-bit from 32-bit *and* 32-bit from 16-bit, it is also
more difficult to implement because it is not API-based.  Flat thunks
require use of a thunk compiler to create OBJ modules to be linked on both
sides of the equation.
--

        -Steve Teixeira
         steix...@borland.com

Other Threads