如何在 C# 中使用 UrlMon.dll 中的 CreateUri 方法

本文关键字:dll UrlMon 中的 CreateUri 方法 | 更新日期: 2023-09-27 18:30:23

我需要在C#中使用UrlMon.dll(...''Windows''System32)的CreateUri方法,但我不能,因为最后一个参数是IUri,它也在UriMon中.dll我不知道如何使用它。

我无法将此 dll 添加为参考或使用 Assembly.GetType() 获取 IUri 类型。

有什么想法吗?

如何在 C# 中使用 UrlMon.dll 中的 CreateUri 方法

我已经找到了解决方案:

public enum UriProperty
{
    ABSOLUTE_URI = 0,
    STRING_START = ABSOLUTE_URI,
    AUTHORITY = 1,
    DISPLAY_URI = 2,
    DOMAIN = 3,
    EXTENSION = 4,
    FRAGMENT = 5,
    HOST = 6,
    PASSWORD = 7,
    PATH = 8,
    PATH_AND_QUERY = 9,
    QUERY = 10,
    RAW_URI = 11,
    SCHEME_NAME = 12,
    USER_INFO = 13,
    USER_NAME = 14,
    STRING_LAST = USER_NAME,
    HOST_TYPE = 15,
    DWORD_START = HOST_TYPE,
    PORT = 16,
    SCHEME = 17,
    ZONE = 18,
    DWORD_LAST = ZONE
}
[ComImport]
[Guid("A39EE748-6A27-4817-A6F2-13914BEF5890"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IUri
{
    UInt32 GetPropertyBSTR([In]UriProperty uriProp, [Out]out string strProperty, [In]UInt32 dwFlags);
    UInt32 GetPropertyLength([In]UriProperty uriProp, [Out] out UInt32 pcPropLen, [In]UInt32 dwFlags);
    UInt32 GetPropertyDWORD([In]UriProperty uriProp, [Out] out UInt32 pcPropValue, [In]UInt32 dwFlags);
    UInt32 HasProperty([In]UriProperty uriProp, [Out] out bool fHasProperty);
    UInt32 GetAbsoluteUri([MarshalAs(UnmanagedType.BStr)][Out] out string sAbsoluteUri);
    UInt32 GetAuthority([MarshalAs(UnmanagedType.BStr)][Out] out string sAuthority);
    UInt32 GetDisplayUri([MarshalAs(UnmanagedType.BStr)][Out] out string sDisplayString);
    UInt32 GetDomain([MarshalAs(UnmanagedType.BStr)][Out] out string sDomain);
    UInt32 GetExtension([MarshalAs(UnmanagedType.BStr)][Out] out string sExtension);
    UInt32 GetFragment([MarshalAs(UnmanagedType.BStr)][Out] out string sFragment);
    UInt32 GetHost([MarshalAs(UnmanagedType.BStr)][Out] out string sHost);
    UInt32 GetPassword([MarshalAs(UnmanagedType.BStr)][Out] out string sPassword);
    UInt32 GetPath([MarshalAs(UnmanagedType.BStr)][Out] out string sPath);
    UInt32 GetPathAndQuery([MarshalAs(UnmanagedType.BStr)][Out] out string sPathAndQuery);
    UInt32 GetQuery([MarshalAs(UnmanagedType.BStr)][Out] out string sQuery);
    UInt32 GetRawUri([MarshalAs(UnmanagedType.BStr)][Out] out string sRawUri);
    UInt32 GetSchemeName([MarshalAs(UnmanagedType.BStr)][Out] out string sSchemeName);
    UInt32 GetUserInfo([MarshalAs(UnmanagedType.BStr)][Out] out string sUserInfo);
    UInt32 GetUserName([MarshalAs(UnmanagedType.BStr)][Out] out string sUserName);
    UInt32 GetHostType([Out] uint dwHostType);
    UInt32 GetPort([Out] uint dwPort);
    UInt32 GetScheme([Out] uint dwScheme);
    UInt32  GetZone([Out] uint dwZone);
    UInt32 GetProperties([Out] uint dwFlags);
    UInt32 IsEqual([In]IUri pUri,[Out] bool fEqual);
}

这为我们提供了 IUri 接口,现在我们可以使用 CreateUri 方法:

[DllImport("UrlMon.dll", SetLastError = true)]
public static extern bool CreateUri(string uri, uint flag, IntPtr dwReserved, out IUri a);