c++ cli - Why can't i see some C++ DLL ( support CLR ) in C# project ? -
i wrote simple c++ dll ( win32 ) , change properties 'common language runtime support (/clr)' -
i created new simple winform project ( c# 4.0 ) , created reference c++ project ( c++ dll ).
now can't see c++ dll in c# project - cant use , dont know why.
if have, example, unmanaged function:
bool foofunction(int firstparameter, int secondparameter); if want make visible managed code have wrap (as first, simple, step) managed class:
public ref class myclass abstract sealed { public: static bool foo(int firstparameter, int secondparameter) { return foofunction(firstparameter, secondparameter); } }; this simple exam, if have interop complex type may need wrap them all. example if have interop function accepts string have manage this. use class this:
ref class unmanagedstring sealed { public: unmanagedstring(string^ content) : _content(content) { _unicodeptr = _ansiptr = intptr::zero; } ~unmanagedstring() { free(); } operator lpwstr() { if (_content == nullptr) return null; free(); _unicodeptr = marshal::stringtohglobaluni(_content); return reinterpret_cast<lpwstr>(_unicodeptr.topointer()); } operator lpcstr() { if (_content == nullptr) return null; free(); _ansiptr = marshal::stringtohglobalansi(_content); return reinterpret_cast<lpcstr>(_ansiptr.topointer()); } virtual system::string^ tostring() override { return _content; } virtual int gethashcode() override { return _content->gethashcode(); } virtual bool equals(object^ obj) override { return _content->equals(obj); } private: intptr _unicodeptr, _ansiptr; string^ _content; void free() { if (_unicodeptr != intptr::zero) { marshal::freehglobal(_unicodeptr); _unicodeptr = intptr::zero; } if (_ansiptr != ntptr::zero) { marshal::freehglobal(_ansiptr); _ansiptr = intptr::zero; } } }; using class can call function void foo(lpcstr psztext) foo(unamangedstring(mymanagedstring)). more complex calls have , more code have write interop between them.
note: exposing 1:1 managed interface unmanaged functions make c# code harder read, suggest write true oo interface hide underlying implementation.
Comments
Post a Comment