Click here to Skip to main content
15,867,308 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Managed c++ float/double/integer to String and vice-versa? Pin
yehiga146726-May-21 0:12
yehiga146726-May-21 0:12 
GeneralRe: Managed c++ float/double/integer to String and vice-versa? Pin
Victor Nijegorodov26-May-21 0:41
Victor Nijegorodov26-May-21 0:41 
QuestionButton image is not updating to new image when Serial Port received new data Pin
yehiga146725-May-21 1:03
yehiga146725-May-21 1:03 
AnswerRe: Button image is not updating to new image when Serial Port received new data Pin
jsc4225-May-21 2:43
professionaljsc4225-May-21 2:43 
GeneralRe: Button image is not updating to new image when Serial Port received new data Pin
yehiga146725-May-21 19:10
yehiga146725-May-21 19:10 
GeneralRe: Button image is not updating to new image when Serial Port received new data Pin
jsc4225-May-21 22:22
professionaljsc4225-May-21 22:22 
GeneralRe: Button image is not updating to new image when Serial Port received new data Pin
yehiga146725-May-21 22:53
yehiga146725-May-21 22:53 
QuestionHow to pass a function pointer an argument in managed C++ Pin
yehiga146724-May-21 1:25
yehiga146724-May-21 1:25 
I am trying to call a function defined in the parent class via the object. But I am getting the error a pointer to member is not valid for a managed class. How can I achieve what I wanted? My expected output is to display the text "Called from child"


MyChild.h
ref class MyChild
{

       System:Void(*my_func_ptr)(int, char*);
       typedef System:Void(*MyFuncPtrType)(int, char*);
       MyFuncPtrType my_func_ptr;


	public: MyChild ( System:Void(*some_func)(int, char*)){

		my_func_ptr = some_func;
	}

	public: System::Void  Child_ButtonClicked(System::Object^ sender, System::EventArgs^ e){
		my_func_ptr();
	}

}



MyForm.h

#include "MyChild.h"

public ref class MyForm : public System::Windows::Forms::Form
{
.
.
.
	private: static System:Void test(int, char*) { //Update
		MessageBox::Show("Called from child");
	}


	private: System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
		MyChild^ child= gcnew MyChild( MyForm::test); //type incompatible error in this line
		child -> test(1,"random");
	}


}




Update 2, Previous problem for //type incompatible error in this line is solved using the following way below.





MyChild.h
ref class MyChild
{
	   
	   
	private:     System::Void(*my_func_ptr)(int, char*);

	public:	System::Void doFunction(int A, char* B) {
           (*my_func_ptr)(A,B);
       }


	public: MyChild ( System::Void(*func)(int A, char* B)){

		my_func_ptr = func;
	}

	public: System::Void  Child_ButtonClicked(System::Object^ sender, System::EventArgs^ e){
		doFunction(1,"random");  //the arguments here are not used in later program, it is just for testing a function ptr with arguments
	}

}



MyForm.h

#include "MyChild.h"

public ref class MyForm : public System::Windows::Forms::Form
{
.
.
.

	   typedef System::Void (*callback_function)(int, char*); 
	   
	   
	private: static System:Void test(int, char*) {
		MessageBox::Show("Called from child");
	}


	private: System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
	
	
	callback_function disc;

	disc = (callback_function)(MyGUI::MyForm::test);
	
		MyChild^ child= gcnew MyChild( disc);
		child -> doFunction(1,"random");
	}


}


For now, everything is solved but I am still unsure to the reason for incompatibility for argument and parameter having same type. I will do more trails and see if this is a stable way of implementation. Thanks for your help Richard Andrew x64 !

modified 25-May-21 0:07am.

AnswerRe: How to pass a function pointer an argument in managed C++ Pin
Richard MacCutchan24-May-21 4:00
mveRichard MacCutchan24-May-21 4:00 
GeneralRe: How to pass a function pointer an argument in managed C++ Pin
yehiga146724-May-21 16:44
yehiga146724-May-21 16:44 
AnswerRe: How to pass a function pointer an argument in managed C++ Pin
Richard Andrew x6424-May-21 4:28
professionalRichard Andrew x6424-May-21 4:28 
QuestionConvert Native Pointer to IntPtr Pin
Richard Andrew x6422-May-21 4:44
professionalRichard Andrew x6422-May-21 4:44 
AnswerRe: Convert Native Pointer to IntPtr Pin
Richard MacCutchan22-May-21 4:47
mveRichard MacCutchan22-May-21 4:47 
GeneralRe: Convert Native Pointer to IntPtr Pin
Richard Andrew x6422-May-21 4:51
professionalRichard Andrew x6422-May-21 4:51 
QuestionBackgammon Game Pin
basel3322-May-21 2:25
basel3322-May-21 2:25 
AnswerRe: Backgammon Game Pin
Richard MacCutchan22-May-21 3:13
mveRichard MacCutchan22-May-21 3:13 
QuestionSerial Port BytesToRead always 0 Pin
yehiga146721-May-21 0:16
yehiga146721-May-21 0:16 
QuestionRe: Serial Port BytesToRead always 0 Pin
Richard MacCutchan21-May-21 0:59
mveRichard MacCutchan21-May-21 0:59 
AnswerRe: Serial Port BytesToRead always 0 Pin
cibec75455@frnla.com21-May-21 1:29
cibec75455@frnla.com21-May-21 1:29 
GeneralRe: Serial Port BytesToRead always 0 Pin
Richard MacCutchan21-May-21 1:38
mveRichard MacCutchan21-May-21 1:38 
GeneralRe: Serial Port BytesToRead always 0 Pin
cibec75455@frnla.com21-May-21 1:47
cibec75455@frnla.com21-May-21 1:47 
QuestionHow to call System::Windows::Forms::Controls "invoke" method? Pin
Member 1515077819-May-21 19:48
Member 1515077819-May-21 19:48 
AnswerRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Victor Nijegorodov19-May-21 20:28
Victor Nijegorodov19-May-21 20:28 
GeneralRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Member 1515077819-May-21 20:35
Member 1515077819-May-21 20:35 
GeneralRe: How to call System::Windows::Forms::Controls "invoke" method? Pin
Richard MacCutchan19-May-21 21:09
mveRichard MacCutchan19-May-21 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.