.net - How to make consistent dll binaries across VS versions? -


for instance, winsock libs works great across versions of visual studio. having real trouble provide consistent binary across versions. dll compiled vs 2005 won't work when linked application written in 2008. upgraded both 2k5 , 2k8 sp1, results haven't changed much. works ok. when include c# app, c# app gets access violation errors, classic c++ application works fine.

is there strategy should know when provide dlls ?

first, dont pass other plain old data accross dll boundries. i.e. structs fine. classes not. second, make sure ownership not transferred - i.e. structs passed accross dll boundry never deallocated outside dll. so, if dll exports x* getx() function, there corresponding freex(x*) type function ensuring same runtime allocated responsible de-allocation.

next: dlls link static runtime. putting project comprimising dls several 3rd parties, each linked , expecting different runtimes, potentially different runtime expected app, pain, potentially forcing installer software install runtimes 7.0, 7.1, 8.0 , 9.0 - several of exist in different service packs may or may not cause issues. kind - statically link dll projects.

-- edit: cannot export c++ class directly approach. sharing class definitions between modules means must have homogeneous runtime environment different compilers or versions of compilers generate decorated names differently.

you can bypass restriction exporting class instead com style interface... say, while cannot export class in runtime independent way, can export "interface", can easilly make declaring class containing pure virtual functions...

  struct iexportedmethods {     virtual long __stdcall amethod(void)=0;   };   // win32 macros:   interface iexportedmethods {     stdmethod_(long,amethod)(this)pure;   }; 

in class definition, inherit interface:

  class cmyobject: public iexportedmethods { ... 

you can export interfaces making c factory methods:

  extern "c" __declspec(dllexport) iexportedclass* winapi createmyexportedobject(){     return new cmyobject;    } 

this lightweight way of exporting compiler version , runtime independent class versions. note still cannot delete 1 of these. must include release function member of dll or interface. member of interface this:

  interface iexportedmethods {     stdmethod_(void) release(this) pure; };   class cmyobject : public iexportedmethods {     stdmethodimp_(void) release(){       delete this;     }   }; 

you can take idea , run further - inherit interface iunknown, implement ref counted addref , release methods ability queryinterface v2 interfaces or other features. , finally, use dllcreateclassobject means create object , necessary com registration going. optional however, can easilly away simple interface definition accessed through c function.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -