LPVoid and Beta2
-
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...
-
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...
Try using IntPtr instead.
-
Try using IntPtr instead.
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.
-
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.
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.
-
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.