Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to pass a function pointer an argument in managed C++

How to pass a function pointer an argument in managed C++

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestionc++testingbeta-testing
4 Posts 3 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yehiga1467
    wrote on last edited by
    #1

    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

    L Richard Andrew x64R 2 Replies Last reply
    0
    • Y yehiga1467

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In non-managed C/C++ you cannot use a pointer to a non-static member in that way. Make the test method to static, and it should work.

      Y 1 Reply Last reply
      0
      • Y yehiga1467

        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

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Look into the function Marshal.GetFunctionPointerForDelegate Method (System.Runtime.InteropServices) | Microsoft Docs[^]

        The difficult we do right away... ...the impossible takes slightly longer.

        1 Reply Last reply
        0
        • L Lost User

          In non-managed C/C++ you cannot use a pointer to a non-static member in that way. Make the test method to static, and it should work.

          Y Offline
          Y Offline
          yehiga1467
          wrote on last edited by
          #4

          Hello, I have made to test() to static and now I have a new error at the same line that doesn't make sense to me. argument of type "void (*)(int, char*)" is incompatible with parameter of type "void (*)(int, char*)" Both the types are identical. I have also tried to change all types to void, and the error message is still the same i.e. argument of type "void (*)()" is incompatible with parameter of type "void (*)()" latest update: problem solved. Please see the first post! thanks!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups