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. How to use a function pointer in C#

How to use a function pointer in C#

Scheduled Pinned Locked Moved C#
questionhelpcsharpjsontutorial
5 Posts 3 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
    Member 1033907
    wrote on last edited by
    #1

    Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is LRESULT SendMessage(...), which I map as IntPtr SendMessage(...). The function whose pointer is returned by SendMessage has the form int fn(void*, int); so I created a delegate delegate int RemoteFunction(IntPtr a, int b) and I try to convert the IntPtr (originally LRESULT) returned by SendMessage to an 'instance' of this delegate, like RemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H.

    R M 2 Replies Last reply
    0
    • M Member 1033907

      Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is LRESULT SendMessage(...), which I map as IntPtr SendMessage(...). The function whose pointer is returned by SendMessage has the form int fn(void*, int); so I created a delegate delegate int RemoteFunction(IntPtr a, int b) and I try to convert the IntPtr (originally LRESULT) returned by SendMessage to an 'instance' of this delegate, like RemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H.

      R Offline
      R Offline
      Rolando CC
      wrote on last edited by
      #2

      maybe this helps:

      using System;
      using System.Runtime.InteropServices;
      namespace com_example
      {
      class Program
      {
      [DllImport("user32.dll")]
      private static extern int MessageBox(IntPtr hWnd, String
      text, String caption, uint type);

      static void Main(string[] args)
      {
      MessageBox(
      new IntPtr(0), "Hello, world!", "My box", 0);
      }
      }
      }

      M 1 Reply Last reply
      0
      • M Member 1033907

        Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is LRESULT SendMessage(...), which I map as IntPtr SendMessage(...). The function whose pointer is returned by SendMessage has the form int fn(void*, int); so I created a delegate delegate int RemoteFunction(IntPtr a, int b) and I try to convert the IntPtr (originally LRESULT) returned by SendMessage to an 'instance' of this delegate, like RemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H.

        M Offline
        M Offline
        MidwestLimey
        wrote on last edited by
        #3

        Not tried it myself, but you could look into: System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer. Supposedly converts an unmanaged fp into a delegate.

        10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

        M 1 Reply Last reply
        0
        • R Rolando CC

          maybe this helps:

          using System;
          using System.Runtime.InteropServices;
          namespace com_example
          {
          class Program
          {
          [DllImport("user32.dll")]
          private static extern int MessageBox(IntPtr hWnd, String
          text, String caption, uint type);

          static void Main(string[] args)
          {
          MessageBox(
          new IntPtr(0), "Hello, world!", "My box", 0);
          }
          }
          }

          M Offline
          M Offline
          Member 1033907
          wrote on last edited by
          #4

          Thanks for you effort but this actually explains how to call an API function whose exact name I know at the design time. My question was rather how to call an external function that is returned dynamically (at runtime) in form of a function pointer. The post bellow explains this perfectly. Thanks anyway, H.

          1 Reply Last reply
          0
          • M MidwestLimey

            Not tried it myself, but you could look into: System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer. Supposedly converts an unmanaged fp into a delegate.

            10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

            M Offline
            M Offline
            Member 1033907
            wrote on last edited by
            #5

            Cool, I am sure this is it. Thank you very much! H.

            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