CWebView::GetWebMessage Method  
 
INT GetWebMessage(
  LPTSTR lpszMessage,  
  INT nMaxLength  
);

The GetWebMessage method returns the most recent message sent from the current document.

Parameters

lpszMessage
A null-terminated string buffer which will contain the message sent from the current document. If this parameter is NULL, the message will not be copied and the method will return the length of the message, not including the terminating null character.
nMaxLength
An integer which specifies the maximum number of characters which can be copied into the string buffer. If the lpszMessage parameter is NULL, this value should be zero.

Return Value

If the method succeeds, the return value is the number of characters in the message, not including the terminating null character. If the buffer provided by the caller is not large enough to contain the entire message, the contents will be truncated, but the method still returns the full length of the message. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The GetWebMessage method retrieves the most recent message sent from a script running in the browser. This is typically used to enable interaction between the web content displayed in the browser and the host application. For example, a page might notify the application when a user completes a form, clicks a button, or when some script logic produces a result that the native application needs to act upon. This provides a simple and secure channel for exchanging data between the web page and the host, without requiring the page to reload or navigate to a new URL.

If you don't want to use a fixed-length buffer for the message, you can call the method with the lpszMessage parameter as NULL and nMaxLegnth as zero. The method will return the number of characters in the message, not including the terminating null character. When dynamically allocating memory for the message, make sure you allocate additional space for the terminator.

If your project is not configured to use Unicode, the message will always be returned as UTF-8 encoded text. It will not be localized to the current ANSI code page. If the buffer you provide is not large enough to contain the entire message, the truncated contents may result in invalid UTF-8 text.

Messages are sent from JavaScript using the following method:

window.chrome.webview.postMessage(value);

When a script in the browser sends a message, the WEBVIEW_EVENT_MESSAGE_RECEIVED event will fire and your event handler should call this method to retrieve the value posted by the script. The value parameter can be any JSON-serializable object or a simple string. The format of the message depends on the type of value that was posted:

  • If the message was a structured JavaScript object or array, it will be returned as a JSON object string using a format like {"key":"value"}.
  • If the message was a string, it will be returned as a JSON-formatted string with surrounding quotes and any special characters will be escaped, such as "Hello\nWorld".

Applications that need to process these messages should parse the result as JSON to properly distinguish between plain strings and structured data.

It is not required for you to call this method inside a handler for the WEBVIEW_EVENT_MESSAGE_RECEIVED event. The message text returned by this method remains available until the next message arrives. If you need to preserve the contents of the message outside of the context of the current browser session, your application should always store its own copy. The library does not maintain a history of messages received.

Applications that receive messages from web content should consider validating the source of each message before processing it, particularly if the message can trigger privileged operations or access sensitive application data. The GetWebMessageSource method can be used to retrieve the URI of the document that sent the message, allowing the application to verify that it originated from a trusted source. Applications should not assume that messages are trustworthy simply because they were received by the browser. When validating the source, compare the complete origin, including the scheme, hostname and port, against a list of trusted origins rather than relying on partial string comparisons. Messages from unknown or untrusted sources should be rejected.

There is an internal limit of 1,048,576 characters as the maximum size of a message. If the message is too large, the WEBVIEW_EVENT_MESSAGE_RECEIVED event will fire with wParam as 0 (FALSE) and lParam as E_BOUNDS. If the browser is unable to retrieve the message for some reason, the event will fire with wParam as 0 and lParam will be an HRESULT error value. On success, the event will fire with wParam as 1 (TRUE) and lParam as 0 (S_OK).

Important: Message passing is disabled by default. To enable this functionality, the browser must be created or attached with the WEBVIEW_OPTION_ENABLE_MESSAGES flag. If this option is not specified, no messages will be received and this method will not return any content.

The internal message handler will only receive messages posted directly by the top-level document, not by child frames. Note that there are security considerations with allowing messages from cross-origin iframes. The browser's same-origin policy doesn't prevent an iframe from sending messages through WebView2's native messaging interface and this can have unintended consequences.

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