From: Charles Stoner Date: Sat, 4 May 2019 05:53:18 +0000 (-0700) Subject: Port several Interaction methods (dotnet/corefx#37436) X-Git-Tag: submit/tizen/20210909.063632~11031^2~1673 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6fe8b330783afbf2bfe9a1d6c1e2fd19da7d72e9;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Port several Interaction methods (dotnet/corefx#37436) Commit migrated from https://github.com/dotnet/corefx/commit/fcaec7c79abef572985d9b7973e4a8c6a6384f41 --- diff --git a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs index 1b62f90..00fde3f 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs @@ -461,7 +461,10 @@ namespace Microsoft.VisualBasic public sealed partial class Interaction { internal Interaction() { } + public static void Beep() { } public static object CallByName(object ObjectRef, string ProcName, CallType UseCallType, params object[] Args) { throw null; } + public static object CreateObject(string ProgId, string ServerName = "") { throw null; } + public static object IIf(bool Expression, object TruePart, object FalsePart) { throw null; } } public enum MsgBoxResult { diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Helpers/UnsafeNativeMethods.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Helpers/UnsafeNativeMethods.vb index fea3048..f5d5b38 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Helpers/UnsafeNativeMethods.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Helpers/UnsafeNativeMethods.vb @@ -44,6 +44,10 @@ Namespace Microsoft.VisualBasic.CompilerServices ByVal vt As Int16) End Sub + + Friend Shared Function MessageBeep(ByVal uType As Integer) As Integer + End Function + Friend Shared Function SetLocalTime(ByVal systime As NativeTypes.SystemTime) As Integer End Function diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb index 97d8c5e..2b67161 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/Interaction.vb @@ -3,12 +3,36 @@ ' See the LICENSE file in the project root for more information. Imports System +Imports System.Runtime.InteropServices + +Imports Microsoft.VisualBasic.CompilerServices +Imports Microsoft.VisualBasic.CompilerServices.ExceptionUtils Imports Microsoft.VisualBasic.CompilerServices.Utils Namespace Microsoft.VisualBasic Public Module Interaction + '============================================================================ + ' User interaction functions. + '============================================================================ + + Public Sub Beep() +#If PLATFORM_WINDOWS Then + UnsafeNativeMethods.MessageBeep(0) +#Else + Throw New PlatformNotSupportedException() +#End If + End Sub + + Public Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, ByVal FalsePart As Object) As Object + If Expression Then + Return TruePart + End If + + Return FalsePart + End Function + Friend Function IIf(Of T)(ByVal condition As Boolean, ByVal truePart As T, ByVal falsePart As T) As T If condition Then Return truePart @@ -17,6 +41,52 @@ Namespace Microsoft.VisualBasic Return falsePart End Function + Public Function CreateObject(ByVal ProgId As String, Optional ByVal ServerName As String = "") As Object + 'Creates local or remote COM2 objects. Should not be used to create COM+ objects. + 'Applications that need to be STA should set STA either on their Sub Main via STAThreadAttribute + 'or through Thread.CurrentThread.ApartmentState - the VB runtime will not change this. + 'DO NOT SET THREAD STATE - Thread.CurrentThread.ApartmentState = ApartmentState.STA + + Dim t As Type + + If ProgId.Length = 0 Then + Throw VbMakeException(vbErrors.CantCreateObject) + End If + + If ServerName Is Nothing OrElse ServerName.Length = 0 Then + ServerName = Nothing + Else + 'Does the ServerName match the MachineName? + If String.Compare(Environment.MachineName, ServerName, StringComparison.OrdinalIgnoreCase) = 0 Then + ServerName = Nothing + End If + End If + + Try + If ServerName Is Nothing Then + t = Type.GetTypeFromProgID(ProgId) + Else + t = Type.GetTypeFromProgID(ProgId, ServerName, True) + End If + + Return System.Activator.CreateInstance(t) + Catch e As COMException + If e.ErrorCode = &H800706BA Then '&H800706BA = The RPC Server is unavailable + Throw VbMakeException(vbErrors.ServerNotFound) + Else + Throw VbMakeException(vbErrors.CantCreateObject) + End If + Catch ex As StackOverflowException + Throw ex + Catch ex As OutOfMemoryException + Throw ex + Catch ex As System.Threading.ThreadAbortException + Throw ex + Catch e As Exception + Throw VbMakeException(vbErrors.CantCreateObject) + End Try + End Function + '============================================================================ ' Object/latebound functions. '============================================================================ diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs index b5b09f0..5f540bf 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/InteractionTests.cs @@ -73,5 +73,30 @@ namespace Microsoft.VisualBasic.Tests set { Value = (int)value + (int)index; } } } + + [Fact] + public void CreateObject() + { + Assert.Throws(() => Interaction.CreateObject(null)); + Assert.Throws(() => Interaction.CreateObject("")); + // Not tested: valid ProgID. + } + + [Theory] + [MemberData(nameof(IIf_TestData))] + public void IIf(bool expression, object truePart, object falsePart, object expected) + { + Assert.Equal(expected, Interaction.IIf(expression, truePart, falsePart)); + } + + private static IEnumerable IIf_TestData() + { + yield return new object[] { false, 1, null, null }; + yield return new object[] { true, 1, null, 1 }; + yield return new object[] { false, null, 2, 2 }; + yield return new object[] { true, null, 2, null }; + yield return new object[] { false, 3, "str", "str" }; + yield return new object[] { true, 3, "str", 3 }; + } } } diff --git a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft/VisualBasic/ApplicationServices/AssemblyInfoTests.cs b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft/VisualBasic/ApplicationServices/AssemblyInfoTests.cs index 26d0245..33daf4c 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft/VisualBasic/ApplicationServices/AssemblyInfoTests.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/tests/Microsoft/VisualBasic/ApplicationServices/AssemblyInfoTests.cs @@ -59,7 +59,7 @@ namespace Microsoft.VisualBasic.ApplicationServices.Tests } [Fact] - [SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] + [SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // Retrieving local process info is not supported on UWP. public void WorkingSet() { // Property is independent of the actual assembly.