Solved

dll w/o .def

Posted on 1998-11-02
10
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

732 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