简体   繁体   English

P调用WPF HWND和const void *

[英]PInvoke WPF HWND and const void*

I need to use the following C++ function in my WPF application: 我需要在WPF应用程序中使用以下C ++函数:

/****************************************
* BOOL WINAPI SetWindowFeedbackSetting(
*  _In_      HWND hwnd,
*  _In_      FEEDBACK_TYPE feedback,
*  _In_      DWORD dwFlags,
*  _In_      UINT32 size,
*  _In_opt_  const VOID *configuration
*);
*****************************************/

But I'm not sure how to Marshal it and how to PInvoke it. 但是我不确定如何封送它以及如何调用它。 This is what I have so far: 这是我到目前为止的内容:

public enum FEEDBACK_TYPE : uint { 
    FEEDBACK_TOUCH_CONTACTVISUALIZATION  = 1,
    FEEDBACK_PEN_BARRELVISUALIZATION     = 2,
    FEEDBACK_PEN_TAP                     = 3,
    FEEDBACK_PEN_DOUBLETAP               = 4,
    FEEDBACK_PEN_PRESSANDHOLD            = 5,
    FEEDBACK_PEN_RIGHTTAP                = 6,
    FEEDBACK_TOUCH_TAP                   = 7,
    FEEDBACK_TOUCH_DOUBLETAP             = 8,
    FEEDBACK_TOUCH_PRESSANDHOLD          = 9,
    FEEDBACK_TOUCH_RIGHTTAP              = 10,
    FEEDBACK_GESTURE_PRESSANDTAP         = 11,
    FEEDBACK_MAX                         = 0xFFFFFFFF
}

[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)] // (1)
public static extern bool SetWindowFeedbackSetting(
    [In] IntPtr hwnd,
    [In] FEEDBACK_TYPE feedback,
    [In] uint dwFlags,
    [In] uint size,           // (2)
    [In, Optional] IntPtr configuration // (3)
 );

So first, is (1),(2),(3) correct? 那么首先,(1),(2),(3)是否正确?

Second, if I want to PInvoke this function according to this C++ function call: 其次,如果我想根据此C ++函数调用PInvoke此函数:

BOOL fEnabled = FALSE;
BOOL res = SetWindowFeedbackSetting(
              hwnd,
              FEEDBACK_TOUCH_CONTACTVISUALIZATION,
              0,
              sizeof(fEnabled),
              &fEnabled
           );

What it the way to send the HWND and the boolean? 发送HWND和布尔值的方式是什么?

I'm asking this question to better understand the pinvoke\\Marshal mechanism and also since the way I tried is not working. 我问这个问题是为了更好地理解pinvoke \\ Marshal机制,而且由于我尝试的方法不起作用。

Thanks 谢谢

Your translation is accurate. 您的翻译是准确的。 But personally, for convenience, I'd make one overload for each type you need to use. 但就我个人而言,为方便起见,我会为每种需要使用的类型做一个重载。 For a boolean: 对于布尔值:

[DllImport("user32.dll"])
public static extern bool SetWindowFeedbackSetting(
    IntPtr hwnd,
    FEEDBACK_TYPE feedback,
    uint dwFlags,
    uint size,          
    [In] ref bool configuration 
);

I removed the attributes that were not needed because they restate default values. 我删除了不需要的属性,因为它们重新声明了默认值。

Remember that the Win32 BOOL type, the default marshalling option for C# bool is a 4 byte type. 请记住,Win32 BOOL类型(C# bool的默认编组选项为4字节类型)。 So you must pass 4 as the size. 因此,您必须传递4作为大小。

See this question to learn how to obtain a window handle for your WPF window: How to get the hWnd of Window instance? 请参阅以下问题,以了解如何为WPF窗口获取窗口句柄: 如何获取Window实例的hWnd?

From what I remember, P/Invoke mechanism has problems with passing/returning .NET's bool. 据我所知,P / Invoke机制在传递/返回.NET布尔值方面存在问题。 I'd do: 我会做:

[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)] // (1)
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowFeedbackSetting(
    [In] IntPtr hwnd,
    [In] FEEDBACK_TYPE feedback,
    [In] uint dwFlags,
    [In] uint size,           // (2)
    [In, Optional] IntPtr configuration // (3)
);

Then, you have to get HWND for current window in WPF. 然后,您必须在WPF中获取当前窗口的HWND。 This question: How to get the hWnd of Window instance? 这个问题: 如何获取Window实例的hWnd? should help in this matter. 在这件事上应该有所帮助。

For your convenience: 为了您的方便:

Window window = Window.GetWindow(this);
var wih = new WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;

(credit goes to Drahcir) (信用归德拉西尔所有)

Finally, to get address of the boolean, I'd do the following: 最后,要获取布尔值的地址,请执行以下操作:

int myBool; // Win32's BOOL is actually an 4-byte integral number, 0 or non-zero
GCHandle handle = GCHandle.Alloc(myBool, GCHandleType.Pinned);
try
{
    IntPtr ptr = handle.AddrOfPinnedObject();

    // use ptr
}
finally
{
    handle.Free();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从GetProcessId(.. hWnd)中提取processID(pInvoke) - Unable to extract processID from GetProcessId(.. hWnd) (pInvoke) 带有void *的PInvoke与带有IntPtr的结构 - PInvoke with a void * versus a struct with an IntPtr 如何使用图形对象渲染WPF Hwnd - How to render a WPF Hwnd with Graphics Object 如何在 WPF(不是 WinForm)中获取句柄(hWnd) - How to get handle (hWnd) in WPF (not WinForm) WPF:调用PInvoke函数已使堆栈不平衡 - WPF: A call to PInvoke function has unbalanced the stack 是否可以通过 PInvoke 指定 WPF 窗口的 MaximizedBounds? - Is it possible to specify the MaximizedBounds of a WPF window via PInvoke? C#中的PInvoke.Net,PostMessage方法不接受FindWindow(IntPtr hWnd)处理指针 - PInvoke.Net in C#, PostMessage method not accepting FindWindow (IntPtr hWnd) handle Pointer C#Pinvoke在第一次列表计数为0后找不到控件的集合 - C# Pinvoke can't find the Hwnd of Controls after List count was 0 at first time Marshal以“const void * key,void * out”作为参数? - Marshal with “const void * key, void * out” as parameter? 使用双数组和void指针的PInvoke函数调用中的AccessViolationException - AccessViolationException in PInvoke function call with double arrays and void pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM