Solved

dll w/o .def

Posted on 1998-11-02
10
371 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

705 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

19 Experts available now in Live!

Get 1:1 Help Now