André123
asked on
error C2491: 'func_1': definition of dllimport function not allowed
I tried to compile several dll-files (using Visual c++ 6.0), and I always get the same error:
error C2491: 'Function_1' : definition of dllimport function not allowed
does anyone knows how this can be solved ?
error C2491: 'Function_1' : definition of dllimport function not allowed
does anyone knows how this can be solved ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>there is no __declspec(dllimport) in the code.
I assume that 'RLIB_API' expands to that. Try
#include <windows.h>
#include <stdio.h>
#include "rlib.h"
extern "C" RLIB_API int Func_1(void);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
int Func_1(void)
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
I assume that 'RLIB_API' expands to that. Try
#include <windows.h>
#include <stdio.h>
#include "rlib.h"
extern "C" RLIB_API int Func_1(void);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
int Func_1(void)
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
ASKER
thanks, but then it says:
warning C4273: 'Func_1' : inconsistent dll linkage. dllexport assumed.
warning C4273: 'Func_1' : inconsistent dll linkage. dllexport assumed.
Ooops,
extern "C" RLIB_API int Func_1(void);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
extern "C" RLIB_API int Func_1(void)
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
(stooopid of me :o)
extern "C" RLIB_API int Func_1(void);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
extern "C" RLIB_API int Func_1(void)
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
(stooopid of me :o)
ASKER
doesn't works. It gives again the first error (error C2491: 'Func_1' : definition of dllimport function not allowed)
To open the source code I only opened the "func_dll.cpp" file. Maybe I should change sth under "settings" ??
To open the source code I only opened the "func_dll.cpp" file. Maybe I should change sth under "settings" ??
RLIB_API should be defined as __declspec(dllexport) inside of Dll and as __declspec(dllimport) outside of it. Usually this is done by the following way: some preprocessor constant (let's say, _MYDLL_) is defined in the DLL (C++, precompiler constants). In main Dll h-file RLIB_API is defined as:
#ifdef _MYDLL_
#define RLIB_API __declspec(dllexport)
#else
#define RLIB_API __declspec(dllimport)
#endif
As result of this, all RLIB_API functions are exported in Dll project, and imported in Dll clients.
#ifdef _MYDLL_
#define RLIB_API __declspec(dllexport)
#else
#define RLIB_API __declspec(dllimport)
#endif
As result of this, all RLIB_API functions are exported in Dll project, and imported in Dll clients.
ASKER
thanks for your reply, but that's already done:
here is my header file:
////////////////
#ifdef RLIB_EXPORTS
#define RLIB_API __declspec(dllexport)
#else
#define RLIB_API __declspec(dllimport)
#endif
extern "C" RLIB_API int RemoteFunction(void);
////////////////////////
here is my header file:
////////////////
#ifdef RLIB_EXPORTS
#define RLIB_API __declspec(dllexport)
#else
#define RLIB_API __declspec(dllimport)
#endif
extern "C" RLIB_API int RemoteFunction(void);
////////////////////////
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
not entirely (since I'm rather new to c++, and never made a dll).
could you tell me how to adapt the code (which I posted above) please??
could you tell me how to adapt the code (which I posted above) please??
Maybe this help you:
#ifdef RLIB_EXPORTS
RLIB_API int Func_1(void) // This DLL is exporting Func_1
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
#else // This DLL is importing Func_1
extern "C" RLIB_API int Func_1(void);
#endif
#ifdef RLIB_EXPORTS
RLIB_API int Func_1(void) // This DLL is exporting Func_1
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
#else // This DLL is importing Func_1
extern "C" RLIB_API int Func_1(void);
#endif
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
jcgd, I tried it but it doesn't seems to work.
AlexFM: now it compiles without problems, but I have now the following link-errrors:
////////////////
Creating library Debug/rlib.lib and object Debug/rlib.exp
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/rlib.exe : fatal error LNK1120: 1 unresolved externals
////////////////
AlexFM: now it compiles without problems, but I have now the following link-errrors:
////////////////
Creating library Debug/rlib.lib and object Debug/rlib.exp
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/rlib.exe : fatal error LNK1120: 1 unresolved externals
////////////////
ASKER
when I add a main function myself, it compiles and it makes an exe.
Where can I declare that I want a dll to be created (vc++ 6.0) ?
Where can I declare that I want a dll to be created (vc++ 6.0) ?
Create new Dll project using Application Wizard and move your code to it. It is difficult to convert existing exe project to Dll.
Select Win32 Dynamic-Link Library project type. On the next step you can select simple Dll project. You can select also Dll that exports some symbols, in this case Visual Studio creates Dll with exported class and function. Read the code generated by Wizard, it is self-explanatory and shows how Dll should be written.
Select Win32 Dynamic-Link Library project type. On the next step you can select simple Dll project. You can select also Dll that exports some symbols, in this case Visual Studio creates Dll with exported class and function. Read the code generated by Wizard, it is self-explanatory and shows how Dll should be written.
In your Project-Settings-C/C++, select Preprocessor Category and add to Preprocessor definitions: _USRDLL,RLIB_EXPORTS
>>Where can I declare that I want a dll to be created (vc++ 6.0) ?
Go to the project settings, pick the link tab, and locate '/subsystem:console' in the text area at the bottom. Change that to read '/dll'
Go to the project settings, pick the link tab, and locate '/subsystem:console' in the text area at the bottom. Change that to read '/dll'
ASKER
thanks all for your help. I could now compile the dll. I'll test it, and if it doesn't works I'll start a new thread :)
I increased the points to 300. Since it was the answer of AlexFM that helped me the most, I'll accept his answer.
I increased the points to 300. Since it was the answer of AlexFM that helped me the most, I'll accept his answer.
ASKER
///////
#include <windows.h>
#include <stdio.h>
#include "rlib.h"
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
extern "C" RLIB_API int Func_1(void)
{
MessageBox(NULL, "test", "test", MB_OK);
return(0);
}
//////////////
so what should I do ?? there is no __declspec(dllimport) in the code.