Thread: Link DLL with Visual C++ not working

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Link DLL with Visual C++ not working

    I have the following problem:
    I have a dll compiled with MinGW and a header file that describes the DLL exports. I want to use this DLL in a Visual C++ projects. Since i don't have a LIB file and i don't want to use LoadLibrary function i created a DEF file. The contents of the EXPORTS section of the DEF file i toke from DUMPBIN /EXPORTS [the_dll], so it should be ok.
    After i created the lib file i included it in my Visual C++ project and the project has compiled successfully. But when i executed it, the application crashed. This application is working when compiled with GCC and the same DLL.
    So is the problem somewhere in MinGW or what?
    May be i must pass some special parameters to cl.exe or link.exe
    Experts please help!

    In more details:
    So in the header file i have a definition of an abstract class wich has onlu pure virtual methods.
    In the DLL i have only one export function which is returning a pointer to an instance of this class or more precisely some class that inherits this abstract class.
    The program is something like:

    Code:
    SomeClass* c = ImportedMethod();
    c->someMethod(); // Here the program crashes
    It looke strange i know, the problem is that i don't hava the source of the DLL neither i have any more information about it. Only those header:

    Code:
    class SomeClass
    {
    public:
    virtual void someMethod() = 0;
    }
    extern "C"
    __declspec (dllexport) SomeClass * ImportedMethod();

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I think you might have to make your function stdcall

    Code:
    virtual void WINAPI someMethod() = 0;

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    No, this doesn't work. The error is that the calling convention is wrong so it's definitely not stdcall

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well. This worked for me:

    VC6 main.cpp
    Code:
    int main()
    {
    	HMODULE h = 
    		::LoadLibrary("C:\\Development\\Dev\\Project1.dll");
    
    	if(!h) return 0;
    
    	SomeClass* (*t)() = 
    		(SomeClass* (*)()) ::GetProcAddress(h, "ImportedMethod");
    
    	if(!test) return 0;
    
    	SomeClass * someClass = (*t)();
    	someClass->someMethod();
    
    	return 0;
    }
    Dev C++ 4.9.9.2 dll.h
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #include <iostream>
    
    class SomeClass
    {
    public:
           virtual void someMethod() = 0;
    };
    
    class RawrClass : public SomeClass
    {
    public:
          virtual void someMethod() { std::cout << "Test"; }
    };
    
    extern "C" __declspec (dllexport) SomeClass * ImportedMethod();
    
    
    #endif
    Dev C++ 4.9.9.2 dllmain.cpp
    Code:
    #include "dll.h"
    #include <windows.h>
    
    SomeClass * ImportedMethod()
    {
              return new RawrClass();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to link dll
    By liang in forum Windows Programming
    Replies: 5
    Last Post: 07-14-2005, 11:55 PM
  2. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. MS Visual C++ DLL Examples
    By G Fresh in forum C++ Programming
    Replies: 0
    Last Post: 03-16-2002, 12:24 AM