how to write a callback function to be invoked by C++ DLL?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wen

    how to write a callback function to be invoked by C++ DLL?

    hello,
    now, i wanna port a c++ program into C# project. a DLL written by C++ and
    keep it no change, and UI wirtten by C#.

    my C++ UI code is as below:

    // error handlers --global function callback function
    // C++ dll would invoke this function if needed
    void ImageConv_Callb ack_Handler(con st char *szInfo)
    {
    AfxMessageBox(s zInfo);
    }

    CXXXDlg::OnInit Dialog(...)
    {
    ....
    // Set Callback Function
    SetImageConvCal lbackFunc(Image Conv_Callback_H andler);
    ....
    }

    { // work flow
    .....
    }

    and it works fine.

    i wrote C# code for the same function, so that i need 2 write a callback
    function in C#. my C# code is as below:

    namespace ImageConvTest
    {

    partial class Form1 : Form
    {
    .....
    public delegate void delegateCH(stri ng szInfo);

    private void ImageConv_Callb ack_Handler(str ing szInfo)
    {
    MessageBox.Show (szInfo);
    }

    // error functions
    [DllImport("Imag eConv.dll", EntryPoint = "SetImageConvCa llbackFunc")]
    public static extern void
    SetImageConvCal lbackFunc(deleg ateCHpFuncCallb ackProc);// in this case, is
    delegateCHcorre ct? is it equal to "void *"???

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    InitImageConvLi brary();
    //
    delegateCH delegate_Callba ck_Handler = new
    delegateCH(Imag eConv_Callback_ Handler);
    SetImageConvCal lbackFunc(deleg ate_Callback_Ha ndler);
    }

    here, strange things happened to the C# project.
    compiled it and got no errors. run it with no errors too. once i trigger the
    function which is in C++ DLL, runtime exception occurs :-(

    and if comments //SetImageConvCal lbackFunc(deleg ate_Callback_Ha ndler);
    the program works fine.


    is there any method to solve this problem? thank u very much.

    --
    With my kind regards,
    Wen


  • Roland Polzer

    #2
    RE: how to write a callback function to be invoked by C++ DLL?

    Hi Wen,

    Greetings form GarbageCollecti on!!!

    you got in trouble cause of you lost the instance of your delegate (it's
    only in your Form_Load).
    [color=blue]
    > public delegate void delegateCH(stri ng szInfo);[/color]

    It's not an instance member, but a type declaration somewhat like this (the
    compiler internally generates a type!!!)

    public class delegateCH : Delegate{...}


    So introduce in your Form a member of the delegate

    partial class Form1 : Form
    {

    pivate static delegateCH _delegate_Callb ack_Handler = null;

    [color=blue]
    > private void Form1_Load(obje ct sender, EventArgs e)
    > {[/color]
    ....

    _delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);

    SetImageConvCal lbackFunc(_dele gate_Callback_H andler);

    ....
    }

    I think it should work

    Roland

    Comment

    • Wen

      #3
      Re: how to write a callback function to be invoked by C++ DLL?

      [color=blue]
      > Hi Wen,
      >
      > Greetings form GarbageCollecti on!!!
      >
      > you got in trouble cause of you lost the instance of your delegate (it's
      > only in your Form_Load).
      >[color=green]
      > > public delegate void delegateCH(stri ng szInfo);[/color]
      >
      > It's not an instance member, but a type declaration somewhat like this[/color]
      (the[color=blue]
      > compiler internally generates a type!!!)
      >
      > public class delegateCH : Delegate{...}
      >
      >
      > So introduce in your Form a member of the delegate
      >
      > partial class Form1 : Form
      > {
      >
      > pivate static delegateCH _delegate_Callb ack_Handler = null;
      >
      >[color=green]
      > > private void Form1_Load(obje ct sender, EventArgs e)
      > > {[/color]
      > ...
      >
      > _delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);
      >
      > SetImageConvCal lbackFunc(_dele gate_Callback_H andler);
      >
      > ...
      > }
      >
      > I think it should work
      >
      > Roland[/color]

      yes, i did and it works now, but when i trigger the function which is
      implemented in C++ dll, the app will disappear :-(

      these are APIs of my C++ DLL:
      // Init & DeInit functions
      IMAGECONV_API
      void DLL_CALLCONV InitImageConvLi brary();
      IMAGECONV_API
      void DLL_CALLCONV DeInitImageConv Library();

      // error functions
      typedef void (*IMAGECONV_CAL LBACK)(const char *);
      IMAGECONV_API

      //void DLL_CALLCONV SetImageConvCal lbackFunc(void *pFuncCallbackP roc);
      void DLL_CALLCONV SetImageConvCal lbackFunc(IMAGE CONV_CALLBACK
      pFuncCallbackPr oc);

      // do convert images functions

      IMAGECONV_API
      bool DLL_CALLCONV BMPtoGIF(int phoneType, const char *in_filename, const
      char *out_filename);
      IMAGECONV_API
      bool DLL_CALLCONV BMPtoPNG(int phoneType, const char *in_filename, const
      char *out_filename);

      .....


      in C# project, the related code is as following:
      partial class Form1 : Form
      {
      ... ...
      public delegate void delegateCH(stri ng szInfo);

      public void ImageConv_Callb ack_Handler(str ing szInfo)
      {
      MessageBox.Show (szInfo);
      }

      private static delegateCH _delegate_Callb ack_Handler = null;

      // Init & Deinit
      [DllImport("Imag eConv.dll", EntryPoint = "InitImageConvL ibrary")]
      public static extern void InitImageConvLi brary();

      [DllImport("Imag eConv.dll", EntryPoint = "DeInitImageCon vLibrary")]
      public static extern void DeInitImageConv Library();

      // error functions
      [DllImport("Imag eConv.dll", EntryPoint =
      "SetImageConvCa llbackFunc")]
      public static extern void SetImageConvCal lbackFunc(deleg ateCH
      pFuncCallbackPr oc);

      // do convert images functions
      [DllImport("Imag eConv.dll", EntryPoint = "BMPtoGIF")]
      public static extern bool BMPtoGIF(int phoneType, string
      in_filename, string out_filename);

      [DllImport("Imag eConv.dll", EntryPoint = "BMPtoPNG")]
      public static extern bool BMPtoPNG(int phoneType, string
      in_filename, string out_filename);

      .... ....

      private void Form1_Load(obje ct sender, EventArgs e)
      {
      InitImageConvLi brary();
      //
      _delegate_Callb ack_Handler = new
      delegateCH(Imag eConv_Callback_ Handler);
      SetImageConvCal lbackFunc(_dele gate_Callback_H andler); // i must
      comment this sentence to make the app run properly, and i cannot use
      callback function to support more details for users :-(
      }

      private void Form1_FormClose d(object sender, FormClosedEvent Args e)
      {
      DeInitImageConv Library();
      }

      private void ImageConvert_Cl ick(object sender, EventArgs e)
      {
      ....
      if (strFileExtensi on.CompareTo(". BMP") == 0)
      {
      BMPtoPNG(m_nPho neTypeConversat ion, strSrcPath,
      strDestPath);
      }
      else if (strFileExtensi on.CompareTo(". JPG") == 0)
      {
      JPEGtoPNG(m_nPh oneTypeConversa tion, strSrcPath,
      strDestPath);
      }
      ...
      }


      }
      }


      very strange! :-(


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: how to write a callback function to be invoked by C++ DLL?


        "Wen" <wenming_hu2002 @hotmail.com> wrote in message
        news:eCcm82xCFH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
        >[color=green]
        >> Hi Wen,
        >>
        >> Greetings form GarbageCollecti on!!!
        >>
        >> you got in trouble cause of you lost the instance of your delegate (it's
        >> only in your Form_Load).
        >>[color=darkred]
        >> > public delegate void delegateCH(stri ng szInfo);[/color]
        >>
        >> It's not an instance member, but a type declaration somewhat like this[/color]
        > (the[color=green]
        >> compiler internally generates a type!!!)
        >>
        >> public class delegateCH : Delegate{...}
        >>
        >>
        >> So introduce in your Form a member of the delegate
        >>
        >> partial class Form1 : Form
        >> {
        >>
        >> pivate static delegateCH _delegate_Callb ack_Handler = null;
        >>
        >>[color=darkred]
        >> > private void Form1_Load(obje ct sender, EventArgs e)
        >> > {[/color]
        >> ...
        >>
        >> _delegate_Callb ack_Handler = new delegateCH(Imag eConv_Callback_ Handler);
        >>
        >> SetImageConvCal lbackFunc(_dele gate_Callback_H andler);
        >>
        >> ...
        >> }
        >>
        >> I think it should work
        >>
        >> Roland[/color]
        >
        > yes, i did and it works now, but when i trigger the function which is
        > implemented in C++ dll, the app will disappear :-(
        >
        > these are APIs of my C++ DLL:
        > // Init & DeInit functions
        > IMAGECONV_API
        > void DLL_CALLCONV InitImageConvLi brary();
        > IMAGECONV_API
        > void DLL_CALLCONV DeInitImageConv Library();
        >
        > // error functions
        > typedef void (*IMAGECONV_CAL LBACK)(const char *);
        > IMAGECONV_API
        >[/color]

        How is IMAGECONV_CALLB ACK defined (typedef)?

        Willy.


        Comment

        Working...