-
C++ with DLL problem!
Ok yesterday I tried getting Vb to work with my C++ dll, but that just didn't work. So now I am trying to get my C++ program to cal on my dll. However hen I compile my C++ test program with my DLL library I get a link error saying
DllTest1 error LNK2001: unresolved external symbol "void __cdecl Test(void)" (?Test@@$$FYAXXZ)
My Dll code for the Test() function is
extern "C" void __cdecl Test(){
MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
}
What am I doing wrong? Thanks!!!!!
-
>>extern "C" void __cdecl Test(){
I'm guessing, but should it be:
>>extern "C" void __cdecl Test(void){
-
Nope. I am still getting the same error. Any other ideas? Thanks!
-
Hi!
Try:
int _stdcall Test(){
MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
return 0;
}
Also, create a file with the name of your .cpp file. For example:
MyDLL.def! The file should have .def extension. Add it to your project (the .def file) from your compiler. In the .def file write:
LIBRARY MyDLL
EXPORTS
Test @1
That should work! At least it works with me!
-
I saw your other post. You're going around in circles, you had the C part right yesterday. Have you forgotten the __declspect bit?
I've pasted some code of mine from an import/export header file. This file is used both in building the DLL & importing it into an app. See the #def LEXI_BUILD_API, which causes it to switch between dllexport and dllimport.
You can use either __cdelc (the C calling convention) or __stdcall. Which one depends on who you want to be able use your DLL. If you use __cdecl, VB apps won't be able to use it.
Why don't you find a site which has a simple example of building & linking a DLL. Get that working & use it as a template to do what you want.
Code:
...
#ifdef LEXI_BUILD_API
#define LEXI_DLLIMEX dllexport
#else
#define LEXI_DLLIMEX dllimport
#endif
//---------------------------------------------------------------------------
// TARA LEXI EXPORT/IMPORT FUNCTIONS
//---------------------------------------------------------------------------
extern "C"
{
...
...
// Debugging
__declspec(LEXI_DLLIMEX) int __cdecl LexiVersionMajor();
__declspec(LEXI_DLLIMEX) int __cdecl LexiVersionMinor();
__declspec(LEXI_DLLIMEX) int __cdecl LexiErcToStr(int ierc, char* strBuf, int maxLen);
__declspec(LEXI_DLLIMEX) void __cdecl LexiSetShowErc(const char* appTitle);
__declspec(LEXI_DLLIMEX) bool __cdecl LexiDebug(const char* strKey, bool debugOn);
}
-
The reason I keep posting Davros, is because I can't find any sites that actually work. What I mean by that is their tutorials, they aren't working. I keep getting the same errors. I am using VC++ .NET
I just tried you idea. Here is my entire code for my dll.
#include "stdafx.h"
#include <mmsystem.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef DREAMLIB_API
#define DREAMLIB dllexport
#else
#define DREAMLIB dllimport
#endif
extern "C" __declspec(DREAMLIB) int __cdecl Test();
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return TRUE;
}
extern "C" int __cdecl Test(){
MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
return 0;
}
Along with my DLL code I added a .def to my project which says
LIBRARY MyDLL
EXPORTS
Test @1
Here is the C++ sample app I wrote to test the dll. In this project, I included the DreamLib.Lib that was exported with my dll.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
int Test();
void main()
{
Test();
}
Here is the only error I get when compiling his Test app.
DllTest1 error LNK2001: unresolved external symbol "int __cdecl Test(void)" (?Test@@$$FYAHXZ)
I hope this helps you help me =) Thanks again!
-
How are you linking to your DLL. You either have link against an object file created when you build your DLL, OR load the library explicitly using LoadLibrary. Personally, I prefer the latter approach, but most people seem to prefer the former.
Where are you including the import header file?
int Test();
Edit : Hangon - I see it now. You're linking against the obj file.
-
So Davros, how do I not link against the object file? Thanks!
-
You call LoadLibrary at runtime & assign function pointers to hook addresses in the DLL. You get the addresses by calling GetProcAddress. Linking against an object library (to access a dll) is called implicit linking, while doing this at runtime is called explicit linking. For once, the MSDN documentation covers this subject really well.
-
Davros, can I see an example of that? Do you know a site with a tutorial like that.
I don't understand why all that is nessicary though. I see so many other people compiling dlls with just one function not having of do a lot of extra work and they say it is successful for them. Why not with me? You said it is because I'm linking against the object Well is there another I can link without having to do all the extra work?