|
-
Aug 20th, 2002, 10:53 AM
#1
Thread Starter
Addicted Member
dllimport/dllexport vs. .def files
I am pretty new to creating DLLs and have come across two methods of exporting functions, classes, etc. I was wondering if anyone could tell me advantages/disadvantages of using either a .def file or dllimport/dllexport with _declspec()?
mepaco, hater of all copy machines!
-
Aug 20th, 2002, 12:04 PM
#2
Frenzied Member
Depends on how you are going to use the DLL. If you are simply going to be using your DLL with another MSVC++ project, using a .lib file, you should use __declspec(dllimport/dllexport). If you need to use the DLL in another language, or you need to load the DLL with LoadLibrary(), you should use the .def file. The reason is that when you export with __declspec(), the function names are mangled in the DLL's export table, but when you export with a .def file, you get a normally named file.
Z.
-
Aug 20th, 2002, 02:08 PM
#3
Thread Starter
Addicted Member
hmm
Thanks for the reply Zaei. You say that I need to use a .def file if I am going to use LoadLibrary(). I am using this function to load my dll at run time. I am trying to export a class from a dll and am having problems currently. Do you think that the fact that I am using _declspec() and LoadLibrary could be causing the following errors?
Here is the code in my .h:
Code:
#ifndef MYLIBAPI
#define MYLIBAPI _declspec(dllimport)
#endif
MYLIBAPI int func1();
class MYLIBAPI MyClass
{
public:
int func(int x);
private:
int value;
};
Here is the code in my .cpp:
Code:
#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include "test.h"
typedef int (*DllFunc)();
void main(void)
{
HINSTANCE hDll;
DllFunc func1;
hDll = LoadLibrary("Test2.dll");
if (hDll != NULL)
{
printf("Library successfully loaded!\n");
func1 = (DllFunc)GetProcAddress(hDll, "func1");
if (!func1)
printf("Error loading function!\n");
else
printf("The returned value is %i!\n", func1());
MyClass ClassTest;
int i;
i = ClassTest.func(3);
printf("The value of 3 after MyClass.func() is %i.\n", i);
FreeLibrary(hDll);
}
else
printf("Failed to load library!\n");
}
And here are the errors I recieve:
--------------------Configuration: asdf - Win32 Debug--------------------
Compiling...
stuff.cpp
Linking...
stuff.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: int __thiscall MyClass::func(int)" (__imp_?func@MyClass@@QAEHH@Z)
Debug/asdf.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
asdf.exe - 2 error(s), 0 warning(s)
If that is my problem, how do you export/import classes with a .def file?
mepaco, hater of all copy machines!
-
Aug 20th, 2002, 09:03 PM
#4
Frenzied Member
You need two projects. In the first project, the DLL, you place the test.h file, and then implement them in a test.cpp file. Build the DLL. Put the code in your .cpp into the second (EXE) project. Also build. Now, your code wont work, because the exported function name is mangled in the DLL. THis is why you need the .def file. The .def file causes the function names to be exported unmangled.
Z.
-
Aug 22nd, 2002, 06:04 AM
#5
If you have a class in a dll, you have to use __declspec, a lib and the compiler you created it with, as name mangling may differ from compiler to compiler.
def file syntax doesn't support classes.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 22nd, 2002, 08:22 AM
#6
Thread Starter
Addicted Member
Thanks
I think I've got it all worked out now. I was told to use explicit linking when there was really no reason to do so. I am now using implicit linking with the _declspec and the inclusion of the lib file. I have to implement this in a class so I guess I have to do it this way. Thanks again for all the help.
mepaco, hater of all copy machines!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|