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. Unsafe DllImport

Unsafe DllImport

Scheduled Pinned Locked Moved C#
data-structuresperformancequestion
4 Posts 2 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.
  • C Offline
    C Offline
    Christian Wikander
    wrote on last edited by
    #1

    Hi. I'm using a function from a dll. It's imported with DllImport and marked as unsafe because the function returns a pointer to a memory array that the function allocates memory for (at least I think this is why). [DllImport("netapi32.dll", ExactSpelling = true, SetLastError = true)] unsafe public static extern int NetWkstaTransportEnum( [MarshalAs(UnmanagedType.LPWStr)] string ServerName, uint level, **uint* pBufptr,** uint prefmaxlen, ref uint entriesread, ref uint totalentries, ref uint resumehandle ); How does it affect my program to compile with the /unsafe switch? Is it possible to define the above DllImport in a way that it will not be unsafe? BR Christian

    A 1 Reply Last reply
    0
    • C Christian Wikander

      Hi. I'm using a function from a dll. It's imported with DllImport and marked as unsafe because the function returns a pointer to a memory array that the function allocates memory for (at least I think this is why). [DllImport("netapi32.dll", ExactSpelling = true, SetLastError = true)] unsafe public static extern int NetWkstaTransportEnum( [MarshalAs(UnmanagedType.LPWStr)] string ServerName, uint level, **uint* pBufptr,** uint prefmaxlen, ref uint entriesread, ref uint totalentries, ref uint resumehandle ); How does it affect my program to compile with the /unsafe switch? Is it possible to define the above DllImport in a way that it will not be unsafe? BR Christian

      A Offline
      A Offline
      alexey N
      wrote on last edited by
      #2

      Try to replace it with IntPtr.

      My english is not so good. Please, correct my errors. Best regards, Alexey.

      C 2 Replies Last reply
      0
      • A alexey N

        Try to replace it with IntPtr.

        My english is not so good. Please, correct my errors. Best regards, Alexey.

        C Offline
        C Offline
        Christian Wikander
        wrote on last edited by
        #3

        The pointer is actually a pointer to an array of structs. How would I do the conversion from IntPtr to the struct array? [System.Runtime.InteropServices.StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct WKSTA_TRANSPORT_INFO_0 { public int wkti0_quality_of_service; public int wkti0_number_of_vcs; public IntPtr wkti0_transport_name; public IntPtr wkti0_transport_address; public int wkti0_wan_ish; }

        1 Reply Last reply
        0
        • A alexey N

          Try to replace it with IntPtr.

          My english is not so good. Please, correct my errors. Best regards, Alexey.

          C Offline
          C Offline
          Christian Wikander
          wrote on last edited by
          #4

          Thanks for the hint with IntPtr. I managed to convert it. Not that I think that the new sollution is less unsafe, but... ;-)

          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