Hi guys :)

I'm currently trying to do something I don't have any experience with, and I did not really understand what ixQuick found for me .. So I try here, hoping someone shouts 'oh, done this million times, it's a piece of cake' ;)

So, I'm using Visual Studio and want to run 2 projects simultaneously - therefore I put them in one solution, set both as Startup projects (multiple startup projects), and configure the main project as an .exe, the additional project as .dll. Now I want my main project to consider also the results of the second one so I can access them (the second project is a parser putting objects in vectors I want to use) I read I should "add a reference to the Class library". How do I do that?? I'm clueless ..

Thanks a lot in advance :)

Dani AI

Generated

was right that the EXE must load the DLL; the parser DLL does not run by itself. To make the EXE see the parser objects you must explicitly export an API from the parser project and link the EXE against the import library the DLL produces. The usual, robust pattern is:

  • Export a stable C or minimal C++ ABI from the DLL (or export a pure virtual interface plus factory/destroy functions).
  • Put the class/function declarations in a header the EXE can include.
  • Make the EXE depend on the DLL project (build order), add the header folder to the EXE include paths, and add the DLL project’s import .lib to the EXE linker input (or add a project-to-project reference so VS handles it).

A common export macro looks like this:

#ifdef PARSER_EXPORTS
#define PARSER_API __declspec(dllexport)
#else
#define PARSER_API __declspec(dllimport)
#endif

Then either export simple C functions (recommended for stability) or a factory that returns a pointer to an interface implemented inside the DLL. Ensure that any memory allocated in the DLL is freed by the DLL (provide a destroy function) or use shared ownership rules to avoid heap/CRT mismatches.

Important cautions: do not expose STL containers or compiler-specific C++ objects across the DLL boundary unless both projects use the exact same compiler, settings and CRT. Passing std::vector/std::string across the boundary is a common source of subtle crashes; prefer C-style buffers or an iterator/query API if portability is needed (see discussion here: Is it safe to return a std::vector from a DLL interface?). Microsoft walk-throughs on creating/using DLLs and the dllexport/dllimport model are useful references: Walkthrough: Creating and Using a Dynamic Link Library (C++) and Exporting from a DLL.

For debugging, multiple startup projects must be executables; the EXE should be the startup project while the DLL is built and loaded by it (see Visual Studio docs for multiple startup projects). Alternatives include building the parser as a static library, using COM, or loading the DLL manually with LoadLibrary/GetProcAddress if runtime binding is required.

Recommended Answers

All 2 Replies

A DLL can not be used as a start application because it isn't an application. A DLL is nothing more than a collection of functions and classes that can be shared among multiple applications. A DLL itself can do nothing.

You can only set one of the projects in a solution as the Startup applicaiton. Only executable programs (with *.exe file extension) can be declared like that. (This is not the same thing as debugging a DLL).

This article explains VC++ 2010 Solutions in detail.

Hey :)

Thanks a lot, it worked. Or at least I could compile and run without any error messages.

I would have another question though. How do I access the objects parsed from the input file by the second project (the one that is configured as .dll)? Can I simply create an object of a class of the second project in the main of the first one? Like ..

int main(){

   Object1_Of_Class_Project1 = new Class_Project1();
   Object1_Of_Class_Project1->Function1();
   Object1_Of_Class_Project1->Function2();

   Object1_Of_Class_Project2 = new Class_Project2();
   Object1_Of_Class_Project2->Function1_Project2(); // Reads file
   Object1_Of_Class_Project2->Function2_Project2(); // Puts file lines in LineList

   for (item in LineList){
      do anything with line;
   }
}

Or how would I do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.