0
   DWORD OREnumKey(
      __in         ORHKEY Handle,
      __in         DWORD dwIndex,
      __out        PWSTR lpName,
      __inout      PDWORD lpcName,
      __out_opt    PWSTR lpClass,
      __inout_opt  PDWORD lpcClass,
      __out_opt    PFILETIME lpftLastWriteTime
    );

My code

[DllImport("offreg.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint OREnumKey(IntPtr Handle, IntPtr dwIndex, [MarshalAs(UnmanagedType.LPWStr)]out StringBuilder lpName, ref IntPtr lpcName, [MarshalAs(UnmanagedType.LPWStr)]out StringBuilder lpClass, ref IntPtr lpcClass, out System.Runtime.InteropServices.ComTypes.FILETIME lpftLastWriteTime);
 IntPtr myKey = hiveid;
    IntPtr dwindex=(IntPtr)0;
    StringBuilder lpName=new StringBuilder("",255);
    IntPtr lpcName = (IntPtr)0;
    StringBuilder  lpClass=new StringBuilder("",255);
    IntPtr lpcClass = (IntPtr)11;
    System.Runtime.InteropServices.ComTypes.FILETIME lpftLastWriteTime;
    uint ret3 = OREnumKey(myKey, dwindex, out lpName, ref lpcName, out lpClass, ref lpcClass, out lpftLastWriteTime);

ret3=ERROR_MORE_DATA 234 Problem can be in wrong StringBuilder Size, or FILETIME 2nd How i should call PWSTR param from C#? [MarshalAs(UnmanagedType.LPWStr)]out StringBuilder lpName is it correct?

John
  • 864
  • 1
  • 11
  • 26
  • 1
    does this give you a better signature? http://clrinterop.codeplex.com/releases/view/14120 – Mike Miller May 30 '12 at 13:36
  • I already saw http://stackoverflow.com/questions/2624373/error-more-data-pvoid-and-c-sharp-unmanaged-types But there are PVOID, in my sample looks like problem in PWSTR – John May 30 '12 at 13:38
  • i just tried clrinterop.codeplex.com and used their signature/ The same error, looks like it is microsoft bug – John May 30 '12 at 13:55
  • And what's the issue? It's reporting that the value is too long to fit in the buffer you've passed in. As your passing 0 for the length it says it's too small and changes lpcName to the value required. – Deanna May 30 '12 at 14:19
  • You may also try adding some punctuation to the description you've given, it barely makes sense, and I'm guessing what the actual problem is. – Deanna May 30 '12 at 14:21

1 Answers1

1

This is a pretty standard Windows error code, it means that you called a winapi function and you didn't pass a big enough buffer. The only way to fix the problem is to pass a bigger buffer.

This looks a lot like a wrapper for RegQueryKeyEx(), which makes it very likely that you are passing bad data to the function. The lpcName argument is actually ref int, not IntPtr. And you are supposed to pass a variable that stores the size of the buffer you passed, 255 in your case. The lpcClass argument is similarly borked. This ought to fix it:

[DllImport("offreg.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint OREnumKey(
    IntPtr Handle, 
    int dwIndex,
    StringBuilder lpName, 
    ref int lpcName, 
    StringBuilder lpClass, 
    ref int lpcClass, 
    out System.Runtime.InteropServices.ComTypes.FILETIME lpftLastWriteTime);

    ...   
    StringBuilder lpName=new StringBuilder("",255);
    int nameSize = lpName.Capacity;
    StringBuilder  lpClass=new StringBuilder("",255);
    int classSize = lpClass.Capacity;
    System.Runtime.InteropServices.ComTypes.FILETIME lpftLastWriteTime;
    uint ret3 = OREnumKey(hiveid, 0, lpName, ref nameSize, lpClass, ref classSize, out lpftLastWriteTime);
    if (ret3 != 0) throw new Exception("kaboom");
    string name = lpName.ToString();
    string className = lpClass.ToString();
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536