Solved

dll w/o .def

Posted on 1998-11-02
10
375 Views
Last Modified: 2010-04-02
I need to write a dll in visual C++ which calls a dll that I have writen in Delphi, and can be called from Delphi.  This means that the Entry points need to be in the dll without ref to a .def file.  I have been able to call the first dll with delphi, VB, fortran, and an excell spread sheet.  Visual C has been a tough one though.
0
Comment
Question by:negone
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:TheGrinch
ID: 1176889
When you build your DLL define the fuctions to be exported as...

   __cdeclspec(dllexport) extern "C"

.and remove the DEF file from the project.

When you use the functions (i.e., in your Delphi program) the header file needs to say...

   __cdeclspec(dllimport) extern "C"




0
 

Author Comment

by:negone
ID: 1176890
Not only is this the canned answer from the help but you have not even checked to see if it works.  Delphi can not link into one created this way.

0
 

Author Comment

by:negone
ID: 1176891
Not only is this the canned answer from the help but you have not even checked to see if it works.  Delphi can not link into one created this way.

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:negone
ID: 1176892
Not only is this the canned answer from the help but you have not even checked to see if it works.  Delphi can not link into one created this way.

0
 
LVL 1

Expert Comment

by:TheGrinch
ID: 1176893
Don't know about "canned answer from the help", because this is the way I do it, and not something I copied from help.

Sorry you didn't find it helpful.
0
 
LVL 1

Expert Comment

by:payn
ID: 1176894
It looks like TheGrinch was answering the opposite of your problem anyway. It looks to me like you're trying to call, from C++, entry points in a Delphi-built DLL. And, for some reason, you don't have an export library for the DLL. Am I right?

Not knowing Delphi too well, I'm not sure how much you can help. I'd suggest using dumpbin or your favorite third-party tool to see the list of exports in your DLL, and make sure it matches the header file you're using to import things from the DLL.

If worst comes to worst, you can write explicit stubs for each function that get the entry point (by name or by index)
explicitly get the entry points by name in your code, cast it to the right type, and call it.

For example, if you have an export whose C++ prototype looks like this:

__stdcall int DoStuff(char c);

Then write an inline stub like this:

typedef __stdcall int (*CharIntFuncPtr)(char);

inline HMODULE GetMyDllModule()
{
  return (HMODULE)LoadLibrary("MyDll.dll");
}

inline int DoStuff(char c)
{
  HMODULE hMod = GetMyDllModule();
  FARPROC pFun = GetProcAddress(hMod, "DoStuff");
  return (CharIntFuncPtr(pFun))(c);
}

Obviously you'll want some better error checking--if the first LoadLibrary fails, the subsequent calls will be a waste of time, not to mention the fact that GetProcAddress will either crash, or return NULL, causing the next line to crash. And you might want to wrap the DLL handling in a class. But this should do it.

0
 

Author Comment

by:negone
ID: 1176895
No I was trying to call a c DLL from Delphi, the problem is that visual C puts information about the entry points in a library file which Delphi can not access.  The info needs to be contained in the Dll itself.  I know that this can be done because we have done it using Symantec C.  The only reason that we don't want to use Symantec is that they are out of the game and the compiler is not supported any more.  The code segment offered by TheGrinch for use in Delphi is not accepted by Delphi.
0
 
LVL 1

Expert Comment

by:payn
ID: 1176896
OK, so I'd again suggest using dumpbin to compare the actual DLLs created by Symantec C++ and MSVC for the same code (or as close as possible) and see what's different between the two of them. I could do this, except that I don't have Symantec C++.

If you can't do this for whatever reason, send me the Symantec-created DLL and either the various MSVC output files or the source, and I'll compare them.

However, it's not the .def that's the problem--the .def is not the export library, it's the file that the compiler uses to decide which functions to export.

You're probably talking about either the .lib or the .exp.

0
 

Accepted Solution

by:
Cyanide320 earned 200 total points
ID: 1176897
 This is an example that calls a function using the __stdcall convention and returns an int:

int __stdcall MyFunc(int x)
{
 return x++;
};

If you want to use another convention, such as cdecl, then replace __stdcall with __cdecl.

0
 

Author Comment

by:negone
ID: 1176898
this is just like the other responses.  I'm only giving a grade to avoid getting more inadequite responses.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 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