c# - 如何在 C# 中从 IFileDialogCustomize GetEditBoxText() 获取字符串

标签 c# c++ shell com

我正在尝试通过 COM 调用在 C# 中使用 IFileDialogCustomize 接口(interface)。我调用 GetEditBoxText(id, buffer) 定义为:

    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    HRESULT GetEditBoxText([In] int dwIDCtl, [Out] IntPtr ppszText);

我从 https://msdn.microsoft.com/en-us/library/windows/desktop/bb775908(v=vs.85).aspx 得到的

我写的代码是:

        IntPtr buffer = Marshal.AllocCoTaskMem(sizeof(int));
        string textboxString;
        var customizeDialog = GetFileDialog() as IFileDialogCustomize;
        if (customizeDialog != null)
        {
         HRESULT result = customizeDialog.GetEditBoxText(id, buffer);
            if (result != HRESULT.S_OK)
            {
                throw new Exception("Couldn't parse string from textbox");
            }
        }
        textboxString = Marshal.PtrToStringUni(buffer);
        Marshal.FreeCoTaskMem(buffer);
        return textboxString;

字符串总是返回奇数字符,例如벰∔。

我刚开始使用 Com 接口(interface)并且从未进行过 C++ 编程,所以我在这里有点迷路。为什么我没有从此处的文本框中获取实际字符串?

最佳答案

我想通了。它结合了 Jacob 所说的和更改 Com 调用的签名。而不是

    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    void GetEditBoxText([In] int dwIDCtl, [Out] IntPtr ppszText);

我需要签名:

    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    HRESULT GetEditBoxText([In] int dwIDCtl, out IntPtr ppszText);

这实际上正确地传递了 IntPtr 并且我能够使用

获取字符串
    Marshal.PtrToStringUni(buffer);

关于c# - 如何在 C# 中从 IFileDialogCustomize GetEditBoxText() 获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35786491/

相关文章:

c++ - 在类的私有(private)部分声明数组

shell - HBase、hadoop、hive通过hive访问Hbase的正确设置方法是什么?

带排除的 PHP shell 执行

linux - 如何将函数名称显示到函数本身

c# - Braintree Drop-In UI Paypal 集成未加载

c# - 基于 bool 方法的自定义 MVC4 验证

c# - 如何使用 ASP.Net C# 更改 CKEditor 的输入语言

c# - 使用 ADPlus 调试消失的 .NET 应用程序,获取 CONTRL_C_OR_Debug_Break 异常

c++ - 修改结构后应用程序崩溃

c# - .NET 任务/TPL 测试和模拟? (或者用法不正确?)