Skip to content

Commit

Permalink
Fixed a number of bugs with IUri and IUriBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
dahall committed Oct 23, 2020
1 parent 4aaf3e4 commit bf1e030
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
32 changes: 15 additions & 17 deletions PInvoke/UrlMon/UrlMon.Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -507,7 +508,7 @@ public interface IUri
/// </remarks>
// 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);

/// <summary>
/// Returns the string length of the specified Uniform Resource Identifier (URI) property. Call this function if you want the
Expand Down Expand Up @@ -548,7 +549,7 @@ public interface IUri
/// </remarks>
// 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);

/// <summary>Returns the specified numeric Uniform Resource Identifier (URI) property value.</summary>
/// <param name="uriProp">
Expand All @@ -566,7 +567,7 @@ public interface IUri
/// </remarks>
// 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);

/// <summary>Determines if the specified property exists in the Uniform Resource Identifier (URI).</summary>
/// <returns>Address of a BOOL value. Set to TRUE if the specified property exists in the URI.</returns>
Expand Down Expand Up @@ -962,13 +963,13 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Returns a new <c>IUri</c> object based on modifications to the original <c>IUri</c>.</summary>
/// <param name="dwCreateFlags">
/// [in] <c>DWORD</c> that combines flags, which control the creation of the <c>IUri</c> object. Refer to the <c>CreateUri</c>
/// function for a description of these flags. Pass the value of to use the same flags as were specified when the original
/// <c>IUri</c> object was created.
/// [in] <c>DWORD</c> that combines <see cref="Uri_CREATE"/> flags, which control the creation of the <c>IUri</c> object. Refer
/// to the <c>CreateUri</c> function for a description of these flags. Pass the value of <c>-1</c> to use the same flags as were
/// specified when the original <c>IUri</c> object was created.
/// </param>
/// <param name="dwAllowEncodingPropertyMask">[in]Reserved. Must be set to 0.</param>
/// <param name="dwReserved">[in]Reserved. Must be set to 0.</param>
Expand Down Expand Up @@ -1069,7 +1070,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the host component.</summary>
/// <param name="pcchHost">
Expand All @@ -1082,7 +1083,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the password component.</summary>
/// <param name="pcchPassword">
Expand All @@ -1095,7 +1096,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the path component.</summary>
/// <param name="pcchPath">
Expand All @@ -1108,7 +1109,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the port component.</summary>
/// <param name="pfHasPort">
Expand All @@ -1131,7 +1132,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the protocol scheme name.</summary>
/// <param name="pcchSchemeName">
Expand All @@ -1144,7 +1145,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Retrieves the value of the username component.</summary>
/// <param name="pcchUserName">
Expand All @@ -1157,7 +1158,7 @@ public interface IUriBuilder
/// </remarks>
// 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);

/// <summary>Sets the fragment component.</summary>
/// <param name="pwzNewValue">String variable that contains the new value, or NULL to remove the fragment component.</param>
Expand Down Expand Up @@ -1296,9 +1297,7 @@ public interface IUriBuilder
IAuthenticate
IAuthenticateEx
IBindHost
IBinding
IBindProtocol
IBindStatusCallback
IBindStatusCallbackEx
ICatalogFileInfo
ICodeInstall
Expand All @@ -1309,7 +1308,6 @@ public interface IUriBuilder
IMonikerProp
IPersistMoniker
ISoftDistExt
IUri
IUriBuilderFactory
IUriContainer
IWindowForBindingUI
Expand Down
12 changes: 6 additions & 6 deletions PInvoke/UrlMon/UrlMon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>Creates an asynchronous bind context for use with asynchronous monikers.</summary>
/// <param name="pbc">A pointer to the IBindCtx interface.</param>
Expand Down Expand Up @@ -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);

/// <summary>Creates a new <c>IUriBuilder</c> instance, and initializes it from an optional <c>IUri</c>.</summary>
/// <param name="pIUri">
Expand Down Expand Up @@ -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);

/// <summary>Creates a URL moniker from a full URL, or from a base context URL moniker and a partial URL.</summary>
/// <param name="pMkCtx">
Expand Down Expand Up @@ -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);

/// <summary>
/// Creates a new URL moniker from a full Uniform Resource Identifier (URI), or from a base context URL moniker and a relative URI.
Expand Down Expand Up @@ -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);

/// <summary>
/// This synchronous function is invoked by the client of a Windows Internet Explorer feature before the client accesses the feature.
Expand Down Expand Up @@ -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);

/// <summary>Gets the <c>CLSID</c> of the object to instantiate for the specified file.</summary>
Expand Down
43 changes: 43 additions & 0 deletions UnitTests/PInvoke/UrlMon/UrlMonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}

0 comments on commit bf1e030

Please sign in to comment.