|
| 1 | +As mentioned in [its page][wv2], WebView2 is available on Windows platforms and can be used from C# |
| 2 | +on a WPF or WinForms application, or in a Win32 with C++. |
| 3 | + |
| 4 | +:::caution |
| 5 | +While the macOS version of WebView2 is in development, there is no preview available. That said, |
| 6 | +registering the application to handle a custom protocol will require calling to OS APIs the same |
| 7 | +way it is done on Windows. |
| 8 | +::: |
| 9 | + |
| 10 | +To handle the |
| 11 | +On a WPF application the URL is passed as part of the parameters when invoking it so you have to |
| 12 | +look for it: |
| 13 | + |
| 14 | +```csharp |
| 15 | +class Program |
| 16 | +{ |
| 17 | + [STAThread] |
| 18 | + static void Main(string[] args) |
| 19 | + { |
| 20 | + if (args != null && args.Length > 0) |
| 21 | + { |
| 22 | + if (args[0].StartsWith("mycustomprotocol://")) |
| 23 | + { |
| 24 | + //handle URI activation |
| 25 | + } |
| 26 | + |
| 27 | + App application = new App(); |
| 28 | + application.InitializeComponent(); |
| 29 | + application.Run(); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +If you are using it on a Win32 application with C++, then your could will look similar to: |
| 35 | + |
| 36 | +```cpp |
| 37 | +// Parameters are assumed to be valid |
| 38 | + |
| 39 | +HRESULT GetPropVariantForUrlAndTime |
| 40 | + (PCWSTR pszUrl, const FILETIME &ftLastModified, PROPVARIANT **ppPropValue) |
| 41 | +{ |
| 42 | + *ppPropValue = NULL; |
| 43 | + |
| 44 | + // Allocate the propvariant pointer. |
| 45 | + size_t const cbAlloc = sizeof(**ppPropValue); |
| 46 | + *ppPropValue = (PROPVARIANT *)CoTaskMemAlloc(cbAlloc)); |
| 47 | + |
| 48 | + HRESULT hr = *ppPropValue ? S_OK : E_OUTOFMEMORY; |
| 49 | + |
| 50 | + if (SUCCEEDED(hr)) |
| 51 | + { |
| 52 | + PropVariantInit(*ppPropValue); // Zero init the value |
| 53 | + |
| 54 | + // Now allocate enough memory for 2 nested PropVariants. |
| 55 | + // PKEY_Search_UrlToIndexWithModificationTime is an array of two PROPVARIANTs. |
| 56 | + PROPVARIANT *pVector = (PROPVARIANT *)CoTaskMemAlloc(sizeof(*pVector) * 2); |
| 57 | + hr = pVector ? S_OK : E_OUTOFMEMORY; |
| 58 | + |
| 59 | + if (SUCCEEDED(hr)) |
| 60 | + { |
| 61 | + // Set the container PROPVARIANT to be a vector of two PROPVARIANTS. |
| 62 | + (*ppPropValue)->vt = VT_VARIANT | VT_VECTOR; |
| 63 | + (*ppPropValue)->capropvar.cElems = 2; |
| 64 | + (*ppPropValue)->capropvar.pElems = pVector; |
| 65 | + PWSTR pszUrlAlloc; |
| 66 | + hr = SHStrDup(pszUrl, &pszUrlAlloc); |
| 67 | + |
| 68 | + if (SUCCEEDED(hr)) |
| 69 | + { |
| 70 | + // Now fill the array of PROPVARIANTS. |
| 71 | + // Put the pointer to the URL into the vector. |
| 72 | + (*ppPropValue)->capropvar.pElems[0].vt = VT_LPWSTR; |
| 73 | + (*ppPropValue)->capropvar.pElems[0].pwszVal = pszUrlAlloc; |
| 74 | + |
| 75 | + // Put the FILETIME into vector. |
| 76 | + (*ppPropValue)->capropvar.pElems[1].vt = VT_FILETIME; |
| 77 | + (*ppPropValue)->capropvar.pElems[1].filetime = ftLastModified; |
| 78 | + } |
| 79 | + |
| 80 | + else |
| 81 | + { |
| 82 | + CoTaskMemFree(pVector); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (FAILED(hr)) |
| 87 | + { |
| 88 | + CoTaskMemFree(*ppPropValue); |
| 89 | + *ppPropValue = NULL; |
| 90 | + } |
| 91 | + } |
| 92 | + return S_OK; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +:::info |
| 97 | +Depending on how your application is packaged and distributed (i.e.: MSIX, squirrel, etc.) you might |
| 98 | +have to do some extra steps like modifying the registry or a manifest. |
| 99 | +::: |
| 100 | +You can read more in [Installing and Registering Protocol Handlers]. |
| 101 | + |
| 102 | +[Installing and Registering Protocol Handlers]: https://docusaurus.io/docs/markdown-features/admonitions |
| 103 | +[wv2]: /docs/webview2 |
0 commit comments