Submitting C++ callback to .NET C++/CLI Wrapper -
i've been searching web solution following problem cannot head around it.
we have big monolithic application med in c++. "upgrade" new world embed wpf views in generated in c++/cli managed wrapper. initial call made c++ via smartpointer.
if (succeeded(ptr.createinstance(__uuidof(cloudservices)))) { crect rect; pwndcontainer->getwindowrect(rect); ptr->initializecontrol((long)pwndcontainer->getsafehwnd(), id_ais_balancematrix, bstr.m_str, rect.width(), rect.height()); }
and in wrapper class interface declared like
interface icloudservices : idispatch{ [id(1)] hresult initializecontrol([in] long hwndparent, [in] long controltypeid, [in] bstr parameters, [in] long width, [in] long height);
and implemented in wrapper
stdmethodimp ccloudservices::initializecontrol(long hwndparent, long controltypeid, bstr parameters, long width, long height) { ... }
problem: works fine , wpf view rendered within c++ app. need send information c++ .net code.
how can submit unmanaged callback function wrapper argument initializecontrol , how use/convert relevant .net delegate?
we did not seem find working solution problem ended sending information using wm_datacompy. not effecient way works.
public void sendcommand(command command) { // find target window handle. intptr htargetwnd = nativemethod.findwindow("ourappnameincpp", null); if (htargetwnd == intptr.zero) { throw new exception("sending '{0}'. unable find \"ourappnameincpp\" window".args(command.asxmlmessage)); } // prepare copydatastruct struct data sent. mystruct mystruct; mystruct.message = command.asxmlmessage; // marshal managed struct native block of memory. int mystructsize = marshal.sizeof(mystruct); intptr pmystruct = marshal.allochglobal(mystructsize); try { marshal.structuretoptr(mystruct, pmystruct, true); copydatastruct cds = new copydatastruct(); cds.cbdata = mystruct.message.length + 1; cds.lpdata = marshal.stringtohglobalansi(mystruct.message); // send copydatastruct struct through wm_copydata message // receiving window. (the application must use sendmessage, // instead of postmessage send wm_copydata because receiving // application must accept while guaranteed valid.) nativemethod.sendmessage(htargetwnd, wm_copydata, intptr.zero, ref cds); int result = marshal.getlastwin32error(); if (result != 0) throw new exception("sendmessage(wm_copydata) failed w/err 0x{0:x}".args(result)); } { marshal.freehglobal(pmystruct); } }
Comments
Post a Comment