Solved

dll w/o .def

Posted on 1998-11-02
10
376 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

838 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