Solved

Call Watcom 32 bit DLL from Delphi 16 bit

Posted on 1997-04-08
7
641 Views
Last Modified: 2013-12-03
I have a 16-bit Dephi Main program and I want to call a 32-bit DLL written in Watcom C.  The provider of the DLL used special Watcom Compiler feature to generate a Thunk DLL to handle the environment crossing, but it does not appear to work with Delphi.  The DLL writer doesn't have a clue since 'it works with other C main programs'.  They or I need help in getting this to work since they say it will take to long to recode their 32-bit DLL to 16-bit code, and will be less efficient.
0
Comment
Question by:jeffg2
7 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 1334479
If you provide more information, may be...

1. How you call thunk DLL - at run time using LoadLibrary & GetProcAddress or at link time using LIB file?
2. How many functions exported by DLL?
3. What kind of work DLL does?
4. Is there any manuals for thunk DLL?

Writting your own thunk DLL is not simple, but possible.
May be better try to use provider's thunk DLL.
0
 

Author Comment

by:jeffg2
ID: 1334480
1. I use the standard Delphi method of calling the DLL which
creates a link at compile time, not LoadLibrary

2. 4 functions open, close, encode, decode

3. Voice compression/decompression

4. For now I cannot release info due to Non Disclosure Agreement.

Other info:

The DLL uses calloc() to create a block of storage upon
opening, and stores 32-bits of info (the flat address?)
in a Long whose address I provide in the open call.

The THUNK was written by the developer of the 32-bit DLL
using a special tool provided by Watcom.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1334481
Please post headers for the C functions, and the 'external' declarations you use in Delphi.

If it works with 16-bit C, it also works with the Delphi. The only exception are functions taking variable number of parameters, which is a bit more tricky.

Regards,

Erik.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:sperling
ID: 1334482
Please post headers for the C functions, and the 'external' declarations you use in Delphi.

If it works with 16-bit C, it also works with the Delphi. The only exception are functions taking variable number of parameters, which is a bit more tricky.

Regards,

Erik.
0
 

Expert Comment

by:jnewlin
ID: 1334483
There are different thunking mechanisms, for the
different platforms 3.1, 95 and NT.  You need to know
which platform he created it for, and make sure that it
is the same as the one that you are using.  The different
mechanisms are mutually exclusive.
0
 

Author Comment

by:jeffg2
ID: 1334484
The Problem is solved.  Thanks for trying. For all of your info the problem was related to floating point traps.  Delphi enables them, most other languages apparently do not.  The DLL caused floating underflows which normally had no affect, but with a Delphi Main, caused a trap.

Case Closed.

0
 

Accepted Solution

by:
Shrif earned 200 total points
ID: 1334485
You don't want to use the thunk compiler that's been so far discussed.  Here's why.  There are three types of thunking that has to do with 32-bit to 16-bit and vice versa calls: Generic thunking, Flat thunking and Universal thunking.  Universal thunking is for Win32s so we won't even go into that.  Generic thunking is a clean interface, first introduced with Windows NT 3.1.  It allows 16-bit EXE's to call 32-bit DLL's.  It does not, however, allow 32-bit EXE's to call 16-bit DLL's.  Generic thunking is available on all Win32 platforms (NT 3.1, 3.5, 3.51, 40 and Windows 95).  Thus, if you are attempting to call 32-bit DLL's from 16-bit EXE's, generic thunking is the best choice because: It is easier and the program will work with NT and Windows 95.  The third thunking method, flat thunking, is Windows 95 only.  It does not work under NT.  Flat thunking lets 32-bit EXE's call 16-bit DLL's AND 16-bit EXE to call 32-bit DLL's.  Flat thunking requires that you use the MIDL compiler to create proxy DLL's on both sides -- Assemlby code is created for you.  It takes a bit of doing to get right and requires that you be be able to link in objs and compile assembly.  So, for your application, it is best to Generic thunk.  I've done this to call 32-bit DLL's from VB3(which is 16-bit only).  Here's some sample code.  The following code was written to call the 32-bit DLL "kernel32.dll", function GetShortPathName so that I could convert a long pathname that the user entered into something a 16-bit program can use.  extern "C" {
      DWORD FAR PASCAL LoadLibraryEx32W( LPCSTR, DWORD, DWORD );
      LPVOID FAR PASCAL GetProcAddress32W( DWORD, LPCSTR );
      DWORD FAR PASCAL GetVDMPointer32W( LPVOID, UINT );
      BOOL FAR PASCAL FreeLibrary32W( DWORD );
      DWORD FAR CallProcEx32W(DWORD nParams, DWORD fAddressConvert,
            LPVOID lpProcAddress, ...);
}


// GetShortPathName
extern "C" void WINAPI BezDeek( LPSTR lpszLongName, LPSTR lpszShortName )
{
      DWORD      ghLib = NULL;
      LPVOID      hProc;
      DWORD      dw;

      if( NULL == (ghLib = LoadLibraryEx32W( "kernel32.dll", NULL, 0 )) )
            {
            MessageBox( NULL, "Cannot load Kernel32", 0, MB_OK );
            goto leave;
            }

      if( NULL == (hProc = GetProcAddress32W( ghLib, "GetShortPathNameA" )))
            {
            MessageBox( 0, "Cannot get procedure address", "GetShortPathNameA", MB_OK );
            goto leave;
            }

      *lpszShortName = 0;
      dw = CallProcEx32W( 3, 3, hProc, lpszLongName, lpszShortName, 256L );

leave:
      if ( ghLib )
            FreeLibrary32W( ghLib );

} // BezDeek

See how much easier this is than flat thunks.  No extra files needed.  You just do it all in source code!Let me know if you have any more questions about this.  You should be able to get far with this.  If you don't, I can explain further.Shrif
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now