diff --git a/PInvoke/UrlMon/UrlMon.Interfaces.cs b/PInvoke/UrlMon/UrlMon.Interfaces.cs index 5a37c9f01..3c16b0df8 100644 --- a/PInvoke/UrlMon/UrlMon.Interfaces.cs +++ b/PInvoke/UrlMon/UrlMon.Interfaces.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; +using Vanara.InteropServices; using static Vanara.PInvoke.Ole32; namespace Vanara.PInvoke @@ -507,7 +508,7 @@ public interface IUri /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775026(v=vs.85) // HRESULT GetPropertyBSTR( Uri_PROPERTY uriProp, [out] BSTR *pbstrProperty, DWORD dwFlags ); - void GetPropertyBSTR([In] Uri_PROPERTY uriProp, [MarshalAs(UnmanagedType.BStr)] out string pbstrProperty, [In] Uri_DISPLAY dwFlags); + void GetPropertyBSTR([In] Uri_PROPERTY uriProp, [MarshalAs(UnmanagedType.BStr)] out string pbstrProperty, [In, Optional] Uri_DISPLAY dwFlags); /// /// Returns the string length of the specified Uniform Resource Identifier (URI) property. Call this function if you want the @@ -548,7 +549,7 @@ public interface IUri /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775028(v=vs.85) // HRESULT GetPropertyLength( Uri_PROPERTY uriProp, [out] DWORD *pcchProperty, DWORD dwFlags ); - void GetPropertyLength([In] Uri_PROPERTY uriProp, out uint pcchProperty, [In] Uri_DISPLAY dwFlags); + void GetPropertyLength([In] Uri_PROPERTY uriProp, out uint pcchProperty, [In, Optional] Uri_DISPLAY dwFlags); /// Returns the specified numeric Uniform Resource Identifier (URI) property value. /// @@ -566,7 +567,7 @@ public interface IUri /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775027(v=vs.85) // HRESULT GetPropertyDWORD( Uri_PROPERTY uriProp, [out] DWORD *pdwProperty, DWORD dwFlags ); - void GetPropertyDWORD([In] Uri_PROPERTY uriProp, out uint pdwProperty, [In] uint dwFlags); + void GetPropertyDWORD([In] Uri_PROPERTY uriProp, out uint pdwProperty, [In] uint dwFlags = 0); /// Determines if the specified property exists in the Uniform Resource Identifier (URI). /// Address of a BOOL value. Set to TRUE if the specified property exists in the URI. @@ -962,13 +963,13 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774988(v=vs.85) // HRESULT CreateUriSimple( [in] DWORD dwAllowEncodingPropertyMask, [in] DWORD_PTR dwReserved, [out] IUri **ppIUri ); - IUri CreateUriSimple(Uri_HAS dwAllowEncodingPropertyMask, IntPtr dwReserved = default); + IUri CreateUriSimple([Optional] Uri_HAS dwAllowEncodingPropertyMask, IntPtr dwReserved = default); /// Returns a new IUri object based on modifications to the original IUri. /// - /// [in] DWORD that combines flags, which control the creation of the IUri object. Refer to the CreateUri - /// function for a description of these flags. Pass the value of to use the same flags as were specified when the original - /// IUri object was created. + /// [in] DWORD that combines flags, which control the creation of the IUri object. Refer + /// to the CreateUri function for a description of these flags. Pass the value of -1 to use the same flags as were + /// specified when the original IUri object was created. /// /// [in]Reserved. Must be set to 0. /// [in]Reserved. Must be set to 0. @@ -1069,7 +1070,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774990(v=vs.85) // HRESULT GetFragment( [out] DWORD *pcchFragment, [out] LPCWSTR *ppwzFragment ); - void GetFragment(out uint pcchFragment, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzFragment); + void GetFragment(out uint pcchFragment, out StrPtrUni ppwzFragment); /// Retrieves the value of the host component. /// @@ -1082,7 +1083,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774991(v=vs.85) // HRESULT GetHost( [out] DWORD *pcchHost, [out] LPCWSTR *ppwzHost ); - void GetHost(out uint pcchHost, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzHost); + void GetHost(out uint pcchHost, out StrPtrUni ppwzHost); /// Retrieves the value of the password component. /// @@ -1095,7 +1096,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774993(v=vs.85) // HRESULT GetPassword( [out] DWORD *pcchPassword, [out] LPCWSTR *ppwzPassword ); - void GetPassword(out uint pcchPassword, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzPassword); + void GetPassword(out uint pcchPassword, out StrPtrUni ppwzPassword); /// Retrieves the value of the path component. /// @@ -1108,7 +1109,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774994(v=vs.85) // HRESULT GetPath( [out] DWORD *pcchPath, [out] LPCWSTR *ppwzPath ); - void GetPath(out uint pcchPath, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzPath); + void GetPath(out uint pcchPath, out StrPtrUni ppwzPath); /// Retrieves the value of the port component. /// @@ -1131,7 +1132,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774996(v=vs.85) // HRESULT GetQuery( [out] DWORD *pcchQuery, [out] LPCWSTR *ppwzQuery ); - void GetQuery(out uint pcchQuery, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzQuery); + void GetQuery(out uint pcchQuery, out StrPtrUni ppwzQuery); /// Retrieves the value of the protocol scheme name. /// @@ -1144,7 +1145,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774997(v=vs.85) // HRESULT GetSchemeName( [out] DWORD *pcchSchemeName, [out] LPCWSTR *ppwzSchemeName ); - void GetSchemeName(out uint pcchSchemeName, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzSchemeName); + void GetSchemeName(out uint pcchSchemeName, out StrPtrUni ppwzSchemeName); /// Retrieves the value of the username component. /// @@ -1157,7 +1158,7 @@ public interface IUriBuilder /// // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms774998(v=vs.85) // HRESULT GetUserName( [out] DWORD *pcchUserName, [out] LPCWSTR *ppwzUserName ); - void GetUserName(out uint pcchUserName, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzUserName); + void GetUserName(out uint pcchUserName, out StrPtrUni ppwzUserName); /// Sets the fragment component. /// String variable that contains the new value, or NULL to remove the fragment component. @@ -1296,9 +1297,7 @@ public interface IUriBuilder IAuthenticate IAuthenticateEx IBindHost - IBinding IBindProtocol - IBindStatusCallback IBindStatusCallbackEx ICatalogFileInfo ICodeInstall @@ -1309,7 +1308,6 @@ public interface IUriBuilder IMonikerProp IPersistMoniker ISoftDistExt - IUri IUriBuilderFactory IUriContainer IWindowForBindingUI diff --git a/PInvoke/UrlMon/UrlMon.cs b/PInvoke/UrlMon/UrlMon.cs index 3e4c4172a..906e09a5d 100644 --- a/PInvoke/UrlMon/UrlMon.cs +++ b/PInvoke/UrlMon/UrlMon.cs @@ -2024,7 +2024,7 @@ public static extern HRESULT CoInternetParseUrl([MarshalAs(UnmanagedType.LPWStr) // reserved, IBindStatusCallback *pBSCb, IEnumFORMATETC *pEFetc, IBindCtx **ppBC ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("urlmon.h", MSDNShortId = "0c79b61b-d3d6-48fd-aaee-21cddad09208")] - public static extern HRESULT CreateAsyncBindCtx([Optional] uint reserved, IBindStatusCallback pBSCb, IEnumFORMATETC pEFetc, out IBindCtx ppBC); + public static extern HRESULT CreateAsyncBindCtx([Optional] uint reserved, IBindStatusCallback pBSCb, [In, Optional] IEnumFORMATETC pEFetc, out IBindCtx ppBC); /// Creates an asynchronous bind context for use with asynchronous monikers. /// A pointer to the IBindCtx interface. @@ -2071,7 +2071,7 @@ public static extern HRESULT CoInternetParseUrl([MarshalAs(UnmanagedType.LPWStr) // cfmtetc, FORMATETC *rgfmtetc, IEnumFORMATETC **ppenumfmtetc ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("urlmon.h", MSDNShortId = "302418e5-48b6-46ee-bb96-2a8170c4af5e")] - public static extern HRESULT CreateFormatEnumerator(uint cfmtetc, [In] FORMATETC[] rgfmtetc, out IEnumFORMATETC ppenumfmtetc); + public static extern HRESULT CreateFormatEnumerator(uint cfmtetc, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] FORMATETC[] rgfmtetc, out IEnumFORMATETC ppenumfmtetc); /// Creates a new IUriBuilder instance, and initializes it from an optional IUri. /// @@ -2482,7 +2482,7 @@ public static extern HRESULT CreateUriFromMultiByteString([MarshalAs(UnmanagedTy // CreateURLMoniker( _In_ IMoniker *pMkCtx, _In_ LPCWSTR szURL, _Out_ IMoniker **ppmk ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("Urlmon.h"), Obsolete("Use CreateURLMonikerEx.")] - public static extern HRESULT CreateURLMoniker([In] IMoniker pMkCtx, [MarshalAs(UnmanagedType.LPWStr)] string szURL, out IMoniker ppmk); + public static extern HRESULT CreateURLMoniker([In, Optional] IMoniker pMkCtx, [MarshalAs(UnmanagedType.LPWStr)] string szURL, out IMoniker ppmk); /// Creates a URL moniker from a full URL, or from a base context URL moniker and a partial URL. /// @@ -2514,7 +2514,7 @@ public static extern HRESULT CreateUriFromMultiByteString([MarshalAs(UnmanagedTy // CreateURLMonikerEx( IMoniker *pMkCtx, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("Urlmon.h")] - public static extern HRESULT CreateURLMonikerEx(IMoniker pMkCtx, [MarshalAs(UnmanagedType.LPWStr)] string szURL, out IMoniker ppmk, URL_MK dwFlags); + public static extern HRESULT CreateURLMonikerEx([In, Optional] IMoniker pMkCtx, [MarshalAs(UnmanagedType.LPWStr)] string szURL, out IMoniker ppmk, URL_MK dwFlags); /// /// Creates a new URL moniker from a full Uniform Resource Identifier (URI), or from a base context URL moniker and a relative URI. @@ -2542,7 +2542,7 @@ public static extern HRESULT CreateUriFromMultiByteString([MarshalAs(UnmanagedTy // CreateURLMonikerEx2( IMoniker *pMkCtx, IUri *pUri, IMoniker **ppmk, DWORD dwFlags ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("Urlmon.h")] - public static extern HRESULT CreateURLMonikerEx2(IMoniker pMkCtx, IUri pUri, out IMoniker ppmk, URL_MK dwFlags); + public static extern HRESULT CreateURLMonikerEx2([In, Optional] IMoniker pMkCtx, IUri pUri, out IMoniker ppmk, URL_MK dwFlags); /// /// This synchronous function is invoked by the client of a Windows Internet Explorer feature before the client accesses the feature. @@ -2735,7 +2735,7 @@ public static extern HRESULT CreateUriFromMultiByteString([MarshalAs(UnmanagedTy // *ppwzMimeOut, _Reserved_ DWORD dwReserved ); [DllImport(Lib.UrlMon, SetLastError = false, ExactSpelling = true)] [PInvokeData("Urlmon.h")] - public static extern HRESULT FindMimeFromData([In] IBindCtx pBC, [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl, IntPtr pBuffer, uint cbSize, + public static extern HRESULT FindMimeFromData([In, Optional] IBindCtx pBC, [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl, IntPtr pBuffer, uint cbSize, [MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed, FMFD dwMimeFlags, [MarshalAs(UnmanagedType.LPWStr)] out string ppwzMimeOut, uint dwReserved = 0); /// Gets the CLSID of the object to instantiate for the specified file. diff --git a/UnitTests/PInvoke/UrlMon/UrlMonTests.cs b/UnitTests/PInvoke/UrlMon/UrlMonTests.cs index a30a76419..f514a908c 100644 --- a/UnitTests/PInvoke/UrlMon/UrlMonTests.cs +++ b/UnitTests/PInvoke/UrlMon/UrlMonTests.cs @@ -13,5 +13,48 @@ namespace Vanara.PInvoke.Tests [TestFixture] public class UrlMonTests { + [Test] + public void CreateUriTest() + { + Assert.That(CreateUri("https://microsoft.com", 0, default, out var iUri), ResultIs.Successful); + using var pUri = ComReleaserFactory.Create(iUri); + var sb = new StringBuilder(); + foreach (var mi in typeof(IUri).GetMethods().Where(t => t.Name.StartsWith("Get") && t.GetParameters().Length == 0)) + { + try + { + var obj = mi.Invoke(iUri, null); + if (!(obj is null)) + sb.Append($"{mi.Name.Substring(3)}={obj}; "); + } + catch { } + } + TestContext.WriteLine(sb.ToString()); + Assert.IsTrue(iUri.HasProperty(Uri_PROPERTY.Uri_PROPERTY_DOMAIN)); + string str = null; + Assert.That(() => iUri.GetPropertyBSTR(Uri_PROPERTY.Uri_PROPERTY_DOMAIN, out str), Throws.Nothing); + Assert.That(str, Is.EqualTo("microsoft.com")); + uint i = 0; + Assert.That(() => iUri.GetPropertyDWORD(Uri_PROPERTY.Uri_PROPERTY_PORT, out i), Throws.Nothing); + Assert.That(i, Is.EqualTo(443U)); + } + + [Test] + public void CreateIUriBuilderTest() + { + Assert.That(CreateUri("https://microsoft.com", 0, default, out var iUri), ResultIs.Successful); + using var pUri = ComReleaserFactory.Create(iUri); + + Assert.That(CreateIUriBuilder(ppIUriBuilder: out var iBld), ResultIs.Successful); + using var pBld = ComReleaserFactory.Create(iBld); + iBld.SetHost("microsoft.com"); + iBld.SetSchemeName("https"); + using var pUri2 = ComReleaserFactory.Create(iBld.CreateUri(Uri_CREATE.Uri_CREATE_ALLOW_RELATIVE)); + Assert.IsTrue(iUri.IsEqual(pUri2.Item)); + + StrPtrUni sch = default; + Assert.That(() => iBld.GetSchemeName(out _, out sch), Throws.Nothing); + Assert.That((string)sch, Is.EqualTo("https")); + } } } \ No newline at end of file