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. Dll Callback and shared memory [modified]

Dll Callback and shared memory [modified]

Scheduled Pinned Locked Moved C#
performancehelpquestion
5 Posts 2 Posters 2 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.
  • P Offline
    P Offline
    Paolo Vernazza
    wrote on last edited by
    #1

    I've a third party DLL that exports this function:

    typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);

    __declspec(dllimport) void SetCallback(RESPONSE_FUNC pfResponse, UCHAR* responseBuffer);

    I must allocate the buffer, call the SetCallback function, and read the modified data when the callback function is called. I tried with this code:

    static byte[] responseBuffer = new byte[10];

    delegate bool RESPONSE_FUNC(byte eventID);

    [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
    private static extern void ANT_SetCallback (RESPONSE_FUNC callback, byte[] responseBuffer);

    public void test
    {
    ANT_SetCallback (EventCallback, responseBuffer);
    }

    static bool EventCallback(byte eventID)
    {
    return true;
    }

    When the dll doesn't try to modify the buffer then the Callback function is called without errors(the first event doesn't access the buffer). But when the DLL tries to modify the data in the buffer, the program crashes with a 0xc0000409 exception. I'm stuck on this, any suggestion ?

    modified on Sunday, November 9, 2008 1:01 PM

    M 1 Reply Last reply
    0
    • P Paolo Vernazza

      I've a third party DLL that exports this function:

      typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);

      __declspec(dllimport) void SetCallback(RESPONSE_FUNC pfResponse, UCHAR* responseBuffer);

      I must allocate the buffer, call the SetCallback function, and read the modified data when the callback function is called. I tried with this code:

      static byte[] responseBuffer = new byte[10];

      delegate bool RESPONSE_FUNC(byte eventID);

      [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
      private static extern void ANT_SetCallback (RESPONSE_FUNC callback, byte[] responseBuffer);

      public void test
      {
      ANT_SetCallback (EventCallback, responseBuffer);
      }

      static bool EventCallback(byte eventID)
      {
      return true;
      }

      When the dll doesn't try to modify the buffer then the Callback function is called without errors(the first event doesn't access the buffer). But when the DLL tries to modify the data in the buffer, the program crashes with a 0xc0000409 exception. I'm stuck on this, any suggestion ?

      modified on Sunday, November 9, 2008 1:01 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      This

      typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);

      should be

      typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);

      Also, make sure you pass a buffer large enough to accomodate whatever writing the DLL is doing in it :) Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      P 1 Reply Last reply
      0
      • M Mark Salsbery

        This

        typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);

        should be

        typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);

        Also, make sure you pass a buffer large enough to accomodate whatever writing the DLL is doing in it :) Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        P Offline
        P Offline
        Paolo Vernazza
        wrote on last edited by
        #3

        Mark Salsbery wrote:

        This typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID); should be typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);

        It's a third party library, I'm not able to change it :( And yes, I'm sure the buffer is bigh enough... it should be 3 bytes, I'm passing 10 ;)

        M 1 Reply Last reply
        0
        • P Paolo Vernazza

          Mark Salsbery wrote:

          This typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID); should be typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);

          It's a third party library, I'm not able to change it :( And yes, I'm sure the buffer is bigh enough... it should be 3 bytes, I'm passing 10 ;)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Paolo Vernazza wrote:

          I'm not able to change it

          Oops, sorry. On the C# side...

          [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
          delegate bool RESPONSE_FUNC(byte eventID);

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          P 1 Reply Last reply
          0
          • M Mark Salsbery

            Paolo Vernazza wrote:

            I'm not able to change it

            Oops, sorry. On the C# side...

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            delegate bool RESPONSE_FUNC(byte eventID);

            Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            P Offline
            P Offline
            Paolo Vernazza
            wrote on last edited by
            #5

            Great, it worked!

            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