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. C#
  4. Problem with delegate for C++ DLL callback function

Problem with delegate for C++ DLL callback function

Scheduled Pinned Locked Moved C#
helpcsharpc++com
2 Posts 1 Posters 0 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.
  • M Offline
    M Offline
    Matthew Page
    wrote on last edited by
    #1

    First, let me say that I am not a C# programmer. Complete newb here... Don't hate me because I'm beautiful... I am trying to create a drop-dead simple application that will set a function callback address in the C++ COM dll to point to a C# function in the demo application. (This is for a sample application to demonstrate our API to potential users.) I have it working for both C++ and Visual Basic, but thus far, C# success is eluding me. Here is what I have done... Created a simple form with three text boxes that will be filled with XYZ values when everything works. Within the namespace of my app (TestCSharpApp... so I lack imagination... sue me...) I have created a delegate function like this. public delegate void coordCallbackFunction(double x, double y, double z, int buttonNumber ); In my public partial class Form1 : Form class, I have the function I want to call.

    public void coordHandler(double x, double y, double z, int buttonNumber)
    {
    xCoord.Text = x.ToString();
    yCoord.Text = y.ToString();
    zCoord.Text = z.ToString();
    }

    Finally, In the form load function I have this code.

    MyCOMInterfaceLib.MyCOMClassClass myApi = new MyCOMInterfaceLib.MyCOMClassClass();
    myApi.coordCallback += new coordCallbackFunction(this.coordHandler);

    I realize that myApi will go away right after setting up the callback, but I am just trying to get it to compile. I'll worry about details later. When I compile the program I get this error... Error 1 Cannot implicitly convert type 'TestCSharpApp.coordCallbackFunction' to 'MyCOMInterfaceLib._IMyCOMClassEvents_coordCallbackEventHandler' C:\Temp\TestCSharpApp\Form1.cs 38 I have included the MyComInterfaceLib in my list of references. I can call functions from the COM dll and that works fine. The problem is setting the callback function using delegates. It may not mean much, but the callback functions work fine in VB.Net, so I don't think the problem is in the COM dll. Thank you for any help.

    M 1 Reply Last reply
    0
    • M Matthew Page

      First, let me say that I am not a C# programmer. Complete newb here... Don't hate me because I'm beautiful... I am trying to create a drop-dead simple application that will set a function callback address in the C++ COM dll to point to a C# function in the demo application. (This is for a sample application to demonstrate our API to potential users.) I have it working for both C++ and Visual Basic, but thus far, C# success is eluding me. Here is what I have done... Created a simple form with three text boxes that will be filled with XYZ values when everything works. Within the namespace of my app (TestCSharpApp... so I lack imagination... sue me...) I have created a delegate function like this. public delegate void coordCallbackFunction(double x, double y, double z, int buttonNumber ); In my public partial class Form1 : Form class, I have the function I want to call.

      public void coordHandler(double x, double y, double z, int buttonNumber)
      {
      xCoord.Text = x.ToString();
      yCoord.Text = y.ToString();
      zCoord.Text = z.ToString();
      }

      Finally, In the form load function I have this code.

      MyCOMInterfaceLib.MyCOMClassClass myApi = new MyCOMInterfaceLib.MyCOMClassClass();
      myApi.coordCallback += new coordCallbackFunction(this.coordHandler);

      I realize that myApi will go away right after setting up the callback, but I am just trying to get it to compile. I'll worry about details later. When I compile the program I get this error... Error 1 Cannot implicitly convert type 'TestCSharpApp.coordCallbackFunction' to 'MyCOMInterfaceLib._IMyCOMClassEvents_coordCallbackEventHandler' C:\Temp\TestCSharpApp\Form1.cs 38 I have included the MyComInterfaceLib in my list of references. I can call functions from the COM dll and that works fine. The problem is setting the callback function using delegates. It may not mean much, but the callback functions work fine in VB.Net, so I don't think the problem is in the COM dll. Thank you for any help.

      M Offline
      M Offline
      Matthew Page
      wrote on last edited by
      #2

      I really don't like answering my own questions... Apparently C#, unlike VB, automatically creates delegates for COM events. So instead of creating my own function I use theirs. Code then becomes,

      myApi = new MyCOMInterfaceLib.MyCOMClassClass();
      myApi.coordCallback += new _IMyCOMClassEvents_coordCallbackEventHandler(this.coordHandler);

      If there is a better or easier way to do this, please let me know. Otherwise, thank you. - Matt

      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