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. LPVoid and Beta2

LPVoid and Beta2

Scheduled Pinned Locked Moved C#
htmlcombeta-testingtutorialannouncement
5 Posts 2 Posters 3 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
    A A 0
    wrote on last edited by
    #1

    Does anyone know what happened to UnmanagedType.LPVoid in beta 2. After spending many hours/frustration trying to figure it out I concluded its not in beta 2. Though it was in an earlier version, an example on CP even uses it. The only reference I found on MSDN was here Hmmm, I wonder if it was replaced by something else or something...

    E 1 Reply Last reply
    0
    • A A A 0

      Does anyone know what happened to UnmanagedType.LPVoid in beta 2. After spending many hours/frustration trying to figure it out I concluded its not in beta 2. Though it was in an earlier version, an example on CP even uses it. The only reference I found on MSDN was here Hmmm, I wonder if it was replaced by something else or something...

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      Try using IntPtr instead.

      A 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        Try using IntPtr instead.

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

        I tried using IntPtr using several tricks none of which works. What I am trying to do is call a win32 api, using C#. I did what I wanted easily in managed C++ with the features I wanted, though I thought it would be better if I can get it in the same project and hence use C# rather than using an external dll. Lets take an example NetServerEnum, which I adapted from here to use beta2: -------------- [DllImport("netapi32.dll")] unsafe private static extern uint NetServerEnum( [MarshalAs(UnmanagedType.LPWStr)] string ServerName, uint level, //In my test I try to change this next line //with something appropriate, though no LPVoid //I tried some of the other types with no //success [MarshalAs(UnmanagedType.LPVoid)]uint* bufptr, uint prefmaxlen, ref uint entriesread, ref uint totalentries, uint servertype, [MarshalAs(UnmanagedType.LPWStr)] string domain, uint resume_handle); -------------- At the end of the day I get one of two results depending how I approach it. A.It compiles, but the function returns error 87 which stands for 'The parameter is incorrect.' B.An exception -------------- Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: C annot marshal parameter #3: Invalid managed/unmanaged type combination pointers must be paired with LPVoid). -------------- I made a managed C++ component to do what I want(didn't take more than a couple of minutes), but was trying to compact things by not using managed C++ if I don't have. And since something similar was done in beta1 I was thinking it should be easily done in beta2, though I am not sure what I am overlooking here. Thanks.

        E 1 Reply Last reply
        0
        • A A A 0

          I tried using IntPtr using several tricks none of which works. What I am trying to do is call a win32 api, using C#. I did what I wanted easily in managed C++ with the features I wanted, though I thought it would be better if I can get it in the same project and hence use C# rather than using an external dll. Lets take an example NetServerEnum, which I adapted from here to use beta2: -------------- [DllImport("netapi32.dll")] unsafe private static extern uint NetServerEnum( [MarshalAs(UnmanagedType.LPWStr)] string ServerName, uint level, //In my test I try to change this next line //with something appropriate, though no LPVoid //I tried some of the other types with no //success [MarshalAs(UnmanagedType.LPVoid)]uint* bufptr, uint prefmaxlen, ref uint entriesread, ref uint totalentries, uint servertype, [MarshalAs(UnmanagedType.LPWStr)] string domain, uint resume_handle); -------------- At the end of the day I get one of two results depending how I approach it. A.It compiles, but the function returns error 87 which stands for 'The parameter is incorrect.' B.An exception -------------- Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: C annot marshal parameter #3: Invalid managed/unmanaged type combination pointers must be paired with LPVoid). -------------- I made a managed C++ component to do what I want(didn't take more than a couple of minutes), but was trying to compact things by not using managed C++ if I don't have. And since something similar was done in beta1 I was thinking it should be easily done in beta2, though I am not sure what I am overlooking here. Thanks.

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          I think you'd want to define the bufptr parameter as: ref IntPtr bufptr With your current code, you're just passing a pointer, rather than a reference to a pointer. Once you get the IntPtr out, it's fairly easy to cast it to whatever pointer type you want.

          A 1 Reply Last reply
          0
          • E Eric Gunnerson msft

            I think you'd want to define the bufptr parameter as: ref IntPtr bufptr With your current code, you're just passing a pointer, rather than a reference to a pointer. Once you get the IntPtr out, it's fairly easy to cast it to whatever pointer type you want.

            A Offline
            A Offline
            A A 0
            wrote on last edited by
            #5

            Thanks. Thats the way I tried it, though something you said made me think about the size and hence error 87. I tried a couple of pointer tricks and it worked! Again thanks for the tip on the change from LPVoid to IntPtr(If it was indeed a change :) )

            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