DLL methods not exported when defined in a source file (VC++)

I have a Main program linking with a DLL in VC++. It turns out that if I declare the class methods outside the header file DLL everything compiles and links fine, but the Main fails to access them.

//mydll.h 
#if XXX_EXPORTS
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif


class CLASS_DECLSPEC COrbitPropagator
{
public:
  int test(double initime, std::vector<double> inivector);  
}

If I define the test method in the header file (inline or below this declaration) the Main works. However, If I define this method in a .cpp source file the Main fails when calling this method and it prints this message:

HEAP[Main.exe]: Invalid Address specified to RtlFreeHeap( 003A0000, 003C2CE8 )

Both runtimes (dll and main) are set to Debug Multi-threaded DLL, but I've tried other combinations. Is there any solution? I'd like to avoid writing all my DLL code in the header file!

EDITED: This only happens when the method uses the stl (e.g. std::vector, std::string)

Upvotes: 0

Views: 553

Answers (1)

rodrigo
rodrigo

Reputation: 98496

When you write the code in the header, it works because the call is inlined, and so it never goes to the DLL to search for it.

You can check whether the test function is imported/exported using the essential utility: Dependency Walker.

Anyway, you are passing a std::vector by copy to a DLL. It is thus a requisite that the DLL and the EXE are compiled and linked using exactly the same C++ headers, libraries, compiler version and compiler options.

If you do not follow this rule, for example, the DLL is debug and the EXE is release, then you will get very strange effects: random corruptions, crashes, etc.

If you need to mix DLLs and EXEs compiled differently then the interface has to be severely restricted (plain C with no cross-dynamic-memory).

Upvotes: 3

Related Questions