Conflict with SystemParametersInfo signatures

99封情书 提交于 2020-01-06 23:51:44

问题


Simply as this, to run this instruction:

NativeMethods.SystemParametersInfo(SPI.SPI_SETKEYBOARDCUES, 
                                   0UI, 
                                   True, 
                                   SPIF.None)

I need this signature:

<DllImport("user32.dll")>
Private Shared Sub SystemParametersInfo(
               ByVal uiAction As NativeMethods.SPI,
               ByVal uiParam As UInteger,
               ByVal pvParam As Boolean,
               ByVal fWinIni As NativeMethods.SPIF)

End Sub

But to run this other instruction:

Dim MyBoolean As Boolean = False
NativeMethods.SystemParametersInfo(SPI.SPI_GETKEYBOARDCUES, 
                                   0UI, 
                                   MyBoolean, 
                                   SPIF.None)

...Of course I need to ByRef the Boolean parameter to retrieve the value:

<DllImport("user32.dll")>
Private Shared Sub SystemParametersInfo(
               ByVal uiAction As NativeMethods.SPI,
               ByVal uiParam As UInteger,
               ByRef pvParam As Boolean,
               ByVal fWinIni As NativeMethods.SPIF)

End Sub

So how I need to manage those signatures? I can't just choose one.

There is a way to keep both signatures as Boolean to don't change one of them to Integer?

And yes the first instruction don't works with a ByRef parameter, then how I can fix this?


回答1:


In C# you would declare both versions as overloaded methods. And then let the compiler select the appropriate one depending on whether you pass the parameter by value or by ref. However, VB syntax does not allow you to specify whether the parameter is by ref or by value. In C# you have to include ref or out and so the overload resolver can use that information to pick the appropriate method.

I think this means that you need to define two methods with different names. For instance:

<DllImport("user32.dll", EntryPoint:="SystemParametersInfo")>
Private Shared Sub SystemParametersInfoByValBoolean(
               ByVal uiAction As NativeMethods.SPI,
               ByVal uiParam As UInteger,
               ByVal pvParam As Boolean,
               ByVal fWinIni As NativeMethods.SPIF)

End Sub

<DllImport("user32.dll", EntryPoint:="SystemParametersInfo")>
Private Shared Sub SystemParametersInfoByRefBoolean(
               ByVal uiAction As NativeMethods.SPI,
               ByVal uiParam As UInteger,
               ByRef pvParam As Boolean,
               ByVal fWinIni As NativeMethods.SPIF)

End Sub


来源:https://stackoverflow.com/questions/21099943/conflict-with-systemparametersinfo-signatures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!