visual c++ - Use C++ managed Form from unmanaged C++ project -



visual c++ - Use C++ managed Form from unmanaged C++ project -

please need help, googled lot , reply problem....

i have solution includes various unmanaged c++ projects, need utilize visual c++ form (that managed) main unmanaged project, , comunicate it. it's possible it? via wrapper, dll.... how?

i googled lot cases found talk how utilize unmanaged managed, or incomplete examples. found illustration , seems works, doesn't compile.

http://www.codeproject.com/articles/9903/calling-managed-code-from-unmanaged-code-and-vice

i'm using visual studio 2012.

thanks lot.

finally found solution...

1 - when want have form command non managed code (for create, show, hide, set controls value...) exported static methods __declspec(dllexport)

2 - when want send info form non managed code using callbacks.

i'm going explain it....

firstly... create clr project dll, create windows form (myform.h) controls:

1 exit button 1 button send info non managed code and 1 checkbox receive new status non managed code

(i ommit gode generated visual studio)

namespace testunmanaged { ... code ommitted ... private: system::void quitbtn_click(system::object^ sender, system::eventargs^ e) { this->close(); } private: system::void button1_click(system::object^ sender, system::eventargs^ e) { //call non managed function!!!! calltocallback(); } }; }

then, create static (non managed) class has pointer form, , manage it's creation, etc..

//type of specific callback function typedef std::function<bool (int& _msg)> t_callback_func; public class unmanagedclass { public: unmanagedclass(); ~unmanagedclass(); void createform( void ); hwnd getformhandle(void) const; void setcallbackfunction(t_callback_func _callbackfuntion); void calltocallbackfunction(int _value); //public fast tests gcroot<testunmanaged::myform^> p; private: hwnd m_hwnd; t_callback_func m_funcioncallback; }; unmanagedclass s_unmanaged;

here's .cpp implementation...

////////////////////////////////////////////////////////////////////////// // static functions dll_export ////////////////////////////////////////////////////////////////////////// void closeform( void ) { application::exit(); } void calltocallback( void ) { int valor = 5; s_unmanaged.calltocallbackfunction(valor); } ////////////////////////////////////////////////////////////////////////// // methods unmanaged class ////////////////////////////////////////////////////////////////////////// hwnd unmanagedclass::getformhandle( void ) const { homecoming m_hwnd; } unmanagedclass::unmanagedclass() { p = gcnew(testunmanaged::myform); } unmanagedclass::~unmanagedclass() { delete (p); } void unmanagedclass::createform( void ) { application::run(p); } void unmanagedclass::setcallbackfunction( t_callback_func _callbackfuntion ) { m_funcioncallback = _callbackfuntion; } void unmanagedclass::calltocallbackfunction( int _value ) { m_funcioncallback(_value); }

now, create static functions exported dllexport:

__declspec(dllexport) void createformdll(void) { s_unmanaged.createform(); } __declspec(dllexport) void closeformdll(void) { s_unmanaged.p->close(); } __declspec(dllexport) void showformdll(void) { s_unmanaged.p->show(); } __declspec(dllexport) void hideformdll(void) { s_unmanaged.p->hide(); } __declspec (dllexport) void setcallbackfunctiondll( t_callback_func& _dofunction) { s_unmanaged.setcallbackfunction(_dofunction); } __declspec (dllexport) void setcheckboxdll( bool _newvalue ) { s_unmanaged.p->getcam01chkbox()->checked = _newvalue; } __declspec (dllexport) hwnd getformhandledll( void ) { homecoming (hwnd)s_unmanaged.p->handle.topointer(); }

now.... create win32 console app project , link managed project. main process, , class "managedwrapper". here source....

class managedwrapper { public: managedwrapper(void); ~managedwrapper(void); void init(void); void createform(void); void showform(void); void closeform(void); void setcam01chkbox(const bool _newvalue); private: void createformthread(void); static bool callbackreceiver(int a); }; #include "managedwrapper.h" #include <thread> __declspec(dllimport) void createformdll(void); __declspec(dllimport) void closeformdll(void); __declspec(dllimport) void showformdll(void); __declspec(dllimport) void hideformdll(void); typedef std::function<bool (int& _msg)> t_callback_func; __declspec (dllimport) void setcallbackfunctiondll( t_callback_func& _dofunction); __declspec (dllimport) void setcheckboxdll( bool _newvalue ); managedwrapper::managedwrapper(void) { } managedwrapper::~managedwrapper(void) { } void managedwrapper::createform( void ) { std::thread showthread (&managedwrapper::createformthread, this); showthread.detach(); } void managedwrapper::createformthread( void ) { createformdll(); } void managedwrapper::closeform( void ) { closeformdll(); } void managedwrapper::showform( void ) { showformdll(); } void managedwrapper::init( void ) { setcallbackfunctiondll(t_callback_func(&managedwrapper::callbackreceiver)); } bool managedwrapper::callbackreceiver( int ) { printf("hello world. parameter value = %d\n",a); homecoming true; } void managedwrapper::setcam01chkbox( const bool _newvalue ) { setcheckboxdll( _newvalue ); }

and finally.... simplest utilize main(), create form, set checkbox values, if user click on button callback function writes on console... , close.

int _tmain(int argc, _tchar* argv[]) { managedwrapper umwrap; umwrap.init(); umwrap.createform(); system("pause"); umwrap.setcam01chkbox(true); system("pause"); umwrap.setcam01chkbox(false); system("pause"); umwrap.closeform(); homecoming 0; }

sorry long answer, believe may usefull somebody.

c++ visual-c++ c++-cli unmanaged managed

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -