CWebView::GetWebMessageSource Method  
 
INT GetWebMessageSource(
  LPTSTR lpszSourceUri,  
  INT nMaxLength  
);
INT GetWebMessageSource(
  CString& strSourceUri  
);

The GetWebMessageSource method returns the source URI for the most recent message.

Parameters

lpszSourceUri
A null-terminated string buffer which will contain the source of the most recent message. If this parameter is NULL, the source URI will not be copied and the method will return the length of the URI, not including the terminating null character. An alternate version of this method accepts a reference to a CString object.
nMaxLength
An integer which specifies the maximum number of characters which can be copied into the string buffer. If the lpszSourceUri parameter is NULL, this value should be zero.

Return Value

If the method succeeds, the return value is the number of characters in the source URI, not including the terminating null character. If the buffer provided by the caller is not large enough to contain the entire URI, the method will fail. If the method fails, the return value is zero. To get extended error information, call the GetLastError method. If no source URI is available, the method returns zero and the last error code is set to NO_ERROR.

Remarks

The GetWebMessageSource method returns the URI of the document that sent the most recent message received by the browser. This method is typically called in response to the WEBVIEW_EVENT_MESSAGE_RECEIVED event, along with GetWebMessage, to retrieve both the message contents and its source. The source URI identifies the document at the time the message was received and may differ from the browser's current URL if navigation has occurred since then.

The source URI remains available until another message is received, at which point the previous source is discarded. The class does not maintain a history of received messages or their sources. Applications that need to preserve this information should store their own copy. It is not necessary to call this method from within an event handler.

If you don't want to use a fixed-length buffer, you can call the method with the lpszSourceUri parameter as NULL and nMaxLength as zero. The method will return the number of characters required to store the URI, not including the terminating null character. Unlike GetWebMessage, this method does not truncate the source URI if the buffer is too small. Instead, the method fails and sets the last error code to ST_ERROR_BUFFER_TOO_SMALL.

If your project is not configured to use Unicode, the source URI will always be returned as UTF-8 encoded text and the return value is the number of UTF-8 bytes in the string. It will not be localized to the current ANSI code page. If no source URI is available, the method returns zero without setting an error. Applications can call GetLastError to distinguish this condition from an actual error.

The source URI can be used to determine whether a message originated from a trusted document before processing its contents. This is particularly important when displaying content from external websites or allowing scripts to request privileged operations from the host application. Applications should validate the URI against a list of trusted origins, including the scheme, hostname and port, rather than relying on partial string comparisons. An unavailable or empty source URI should not be considered trusted. The source URI identifies the document that posted the message; it does not independently establish the origin of data that may have been forwarded to that document by another frame or script.

This method will return an empty string if no message has been received, if the WebView2 runtime did not return a source for the message or if messages have not been enabled by specifying the WEBVIEW_OPTION_ENABLE_MESSAGES option. If the Reset method is called, it will clear both the most recent message and message source.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also