Importing a class with dllimport

This is a discussion on Importing a class with dllimport within the C++ Programming forums, part of the General Programming Boards category; I am trying to import a class from a dll I created. The class seems to import fine. I can ...

  1. #1
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47

    Question Importing a class with dllimport [Resolved]

    I am trying to import a class from a dll I created. The class seems to import fine. I can access data members of the class but when I try to access a member function of the class then I get errors. Here is the code in my .h file:
    Code:
    #ifndef MYLIBAPI
    #define MYLIBAPI _declspec(dllimport)
    #endif
    
    MYLIBAPI int func1();
    
    class MYLIBAPI MyClass
    {
    	public:
    		void func();
    		int value;
    };
    Here is the code in my .cpp file:
    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;
    
    		ClassTest.value = 3;
    		ClassTest.func();
    		printf("The value of 3 after MyClass.func() is %i.\n", ClassTest.value);
    
    		FreeLibrary(hDll);
    	}
    	else
    		printf("Failed to load library!\n");
    }
    Here is the error I get:
    --------------------Configuration: asdf - Win32 Debug--------------------
    Compiling...
    stuff.cpp
    Linking...
    stuff.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall MyClass::func(void)" (__imp_?func@MyClass@@QAEXXZ)
    Debug/asdf.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    asdf.exe - 2 error(s), 0 warning(s)
    If anyone knows why I am getting this link error or what I can do please let me know. Any help would be appreciated.
    Last edited by mepaco; 08-21-2002 at 08:19 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Where's the dll's code?

    Did you use the same header in both the same module? If do, you need to specify it as __declspec(dllexport) for the dll

    Did you include the lib file created with your dll in the project you have above?

  3. #3
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Thanks for your reply Fordy. I'll try to answer your questions the best I can. For my simple console app to test the dll I only have the code above. I am very new to C++ and to writing dlls so I don't know about including .lib files. I did use the same header in the dll. I defined MYLIBAPI in the .cpp before including the .h so it would be 'dllexport'. Here is all the code from the dll project:

    Here is the code in my .h:
    Code:
    #ifndef MYLIBAPI
    #define MYLIBAPI _declspec(dllimport)
    #endif
    
    MYLIBAPI int func1();
    
    class MYLIBAPI MyClass
    {
    	public:
    		void func();
    		int value;
    };
    Here is the code in my .cpp:
    Code:
    #include <windows.h>
    #define MYLIBAPI _declspec(dllexport)
    #include "test.h"
    
    BOOL WINAPI DllMain( HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
    {
    	switch (dwReason) 
    	{
    		case DLL_PROCESS_ATTACH:
    			break;
    
    		case DLL_THREAD_ATTACH:
    			break;
    
    		case DLL_THREAD_DETACH:
    			break;
    
    		case DLL_PROCESS_DETACH:
    			break;
    	}
    	return TRUE;
    }
    
    MYLIBAPI int func1()
    {
    	return 5;
    }
    
    void MyClass::func()
    {
    	value += 1;
    }
    I hope this helps.

  4. #4
    Shadow12345
    Guest
    I had the exact same problem with a similiar .dll project, it was even named asdf. I wish I knew what was wrong..

    Also
    what does it mean when you put
    typedef int (*DllFunc)();

  5. #5
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47

    Shadow12345,

    Well, there might be a better way but when dynamically linking to a library at run time you have to get the addresses of any functions you might use from the dll. The way I do it is by creating a pointer to a function of that type. The 'int func1()' function in the dll is what I use that code for. 'typedef int (*DllFunc)()' creates a typedef DllFunc that is a pointer to a function that returns an int and takes no parameters. Then I create an instance of DllFunc and call it func1. I then used GetProcAddress() to point the pointer to the func1() function in the dll. Hope this helps explain it.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    OK.......
    You need the lib file as it tells the linker where to find the function in your dll...add it in the same folder as the code in your console app

    I notice you have VC++, so go to project->settings->link->Object/Library Modules and add your lib in there....

    Or more simply use

    Code:
    #pragma comment(lib,"Test2.lib")
    in your code (normally I put it at the top with the #includes & #defines)

    I did a few fixes on the code and attached it for you....

    Few changes;

    The global func (func1) - I added an extern "C" statement (the compiler seemed unhappy without it, and unless you are using classes, you may find it easier).

    I changed the way you declare __declespec()...have a look...its a more versitile method....you can use the same header...all you need to do is add a define in the dll code to make it export....

    I added a #pragma to include the lib
    Attached Files Attached Files

  7. #7
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47

    Sweet

    Thanks Fordy. I had been misinformed and was told that explicit linking was the way to go. This way is much better. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21