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. void* interop?

void* interop?

Scheduled Pinned Locked Moved C#
questioncsharpcom
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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    how do I translate into C#: extern "C" int f(void*); I know about "unsafe", but I'd like it to be a normal DLLImport

    J 1 Reply Last reply
    0
    • A Anonymous

      how do I translate into C#: extern "C" int f(void*); I know about "unsafe", but I'd like it to be a normal DLLImport

      J Offline
      J Offline
      Jeff J
      wrote on last edited by
      #2

      The IntPtr type can handle void pointers. In fact, it has static methods specifically for dealing with them. Incidentally, "unsafe" doesn't mean "not safe to do", as the name implies. It just means the runtime cannot verify the types and such for us. I read somewhere that the name was chosen to scare unseasoned programmers away from using it. Internally, the CLR uses unsafe all the time. Cheers

      A 1 Reply Last reply
      0
      • J Jeff J

        The IntPtr type can handle void pointers. In fact, it has static methods specifically for dealing with them. Incidentally, "unsafe" doesn't mean "not safe to do", as the name implies. It just means the runtime cannot verify the types and such for us. I read somewhere that the name was chosen to scare unseasoned programmers away from using it. Internally, the CLR uses unsafe all the time. Cheers

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)

        L J 2 Replies Last reply
        0
        • A Anonymous

          Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Anonymous wrote: Jeff J wrote: it has static methods specifically for dealing with which one? Ones, the ones in the Marshal class :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

          1 Reply Last reply
          0
          • A Anonymous

            Jeff J wrote: it has static methods specifically for dealing with which one? forgot the 2nd signature I am using extern "C" int f2(void**); I managed to make it work witch "ref uint" unfortunatly the first function does not take return value. DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(uint s); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(ref uint s); After I use return value for "f()" in "f2()" I got something like "'System.Runtime.InteropServices.MarshalDirectiveException' Can not marshal parameter #1: Invalid managed/unmanaged type combination (Int/UInt must be paired with I or U)" Any suggestions are welcome (I'll definitely try unsafe)

            J Offline
            J Offline
            Jeff J
            wrote on last edited by
            #5

            As leppie posted, there are many useful functions in the Marshal class, which is good to become familiar with if you are interfacing with external C or C++ functions. IntPtr itself also has useful methods, such as ToPointer() and ToInt32(), along with the statics Zero and operator overloads that can convert between void pointers and IntPtr types. Which ones to use depends on how you plan on calling the extern functions. It is hard to explain the usage details without seeing a code example. If you could post one with how the calls are intended to be made, and how the parameters are to be passed, I'm sure I could explain a solution. Regarding using the extern funcs you currently have, if the calling C# func is declared unsafe, then you can just cast your "ints" as in regular C code (including pointers and taking addresses). Or you can prototype the externs as unsafe from the start like: DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern unsafe int f(void *pVoid); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern unsafe int f2(void **ppVoid); If you are looking to do this using the non-unsafe runtime interop features, you could do: DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f(IntPtr pVoid); DLLImport("rv.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)) public static extern int f2(IntPtr ppVoid);

            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