Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top