1

I have a dynamically loaded dll that has functions called on it, and i would like to pass it a struct. It is dynamically loaded as i do not know which version of the dll i will be using untill runtime.

Firstly, should i pass by value or reference?

Secondly, where do I declare this struct so it is available to both the dll and its caller (i assume it has to be available to both).

If i declare it in the same dll that accepts it as a parameter, then surly any potential user of the dll will have to statically link against the dll so it has access to the struct declaration, and this is what i originally wanted to avoid.

pingu
  • 8,719
  • 12
  • 50
  • 84
  • I would have thought that the struct declaration would be in a header file? – Nick Jan 16 '12 at 14:09
  • You are basically asking "How to ensure my code crashes quickest when the wrong DLL version gets loaded?" By value is best, it imbalances the stack pointer. – Hans Passant Jan 16 '12 at 15:13

1 Answers1

2

Firstly, should i pass by value or reference?

That depends, see e.g. this question.

Secondly, where do I declare this struct so it is available to both the dll and its caller

You declare it in a header file that both use. The struct is declared at compile-time, no linking against any library is needed for that.

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • Thanks Georg, could you elaborate on this header i declare the struct in please? would it be the same header that declares the c interface for my dll? Do i just create a standalone header file which i include in both projects? As both projects are c++, do i put the declaration in a extern "C" directive? please forgive my ignorant questions. – pingu Jan 16 '12 at 14:25
  • @pingu: Where to put the struct depends a bit on your project - but it should go in a header of the host; the header where you define the interface would be one possibility. The struct needs no `extern "C"`. As both sides are C++, depending on your setup, you may only need `extern "C"` for one function. See e.g. [here](http://stackoverflow.com/a/431578/168225) for a question on such a setup. – Georg Fritzsche Jan 16 '12 at 14:41