【问题标题】:DllImport ERROR_MORE_DATA UNMANAGED Call C++ in C#DllImport ERROR_MORE_DATA UNMANAGED 在 C# 中调用 C++
【发布时间】:2012-05-30 13:34:44
【问题描述】:
   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
    );

我的代码

[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 问题可能出在错误的 StringBuilder 大小或 FILETIME 第二我应该如何从 C# 调用 PWSTR 参数? [MarshalAs(UnmanagedType.LPWStr)]out StringBuilder lpName对吗?

【问题讨论】:

  • 这会给你一个更好的签名吗? clrinterop.codeplex.com/releases/view/14120
  • 我已经看到 stackoverflow.com/questions/2624373/… 但是有 PVOID,在我的示例中看起来像 PWSTR 中的问题
  • 我刚刚尝试了 clrinterop.codeplex.com 并使用了他们的签名/同样的错误,看起来是 microsoft bug
  • 有什么问题?它报告该值太长,无法放入您传入的缓冲区中。当您传递 0 的长度时,它表示它太小并将 lpcName 更改为所需的值。
  • 您也可以尝试在您给出的描述中添加一些标点符号,这几乎没有意义,我猜测实际问题是什么。

标签: c# winapi registry unmanaged dllimport


【解决方案1】:

这是一个非常标准的 Windows 错误代码,这意味着您调用了一个 winapi 函数并且您没有传递足够大的缓冲区。解决问题的唯一方法是传递更大的缓冲区。

这看起来很像 RegQueryKeyEx() 的包装器,这使得您很可能将错误数据传递给函数。 lpcName 参数实际上是ref int,而不是 IntPtr。你应该传递一个变量来存储你传递的缓冲区的大小,在你的例子中是 255。 lpcClass 参数同样是无聊的。这应该可以解决它:

[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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多