If the method succeeds, the return value is the number of
characters in the result string, not including the terminating null
character. A positive return value indicates that a result string is
available, but does not necessarily indicate that the JavaScript
expression completed without an exception. A return value of zero may
indicate an error or an empty result. To get extended error information,
call the GetLastError method.
The GetWebResult method retrieves the result of the most recent
JavaScript expression executed in the browser using either the
AsyncExecute or Execute methods. The result is
provided as a JSON-formatted string, which may be either a quoted string
with escaped characters or a JSON object, depending on the value returned
by the script. Applications that expect structured data should parse the
returned string using a JSON parser.
Based on how the project is configured, the GetWebResult
method will either call WebViewGetResultA, which uses 8-bit
strings, or WebViewGetResultW, which uses Unicode strings.
The ANSI version returns the result as a UTF-8 encoded string, while
the Unicode version returns a UTF-16 string. For the ANSI version,
the returned length represents the number of bytes in the UTF-8 result,
excluding the terminating null character. Because UTF-8 uses a
variable number of bytes to represent Unicode characters, the returned
length may be greater than the number of characters in the string.
If the result is truncated, the method ensures that a multibyte
UTF-8 sequence is not split.
This method returns the total length of the script result,
excluding the terminating null character, regardless of the size of
the result buffer. If the buffer is too small, the result is truncated
and null-terminated. If the lpszResult parameter is NULL, the
method will return the required length of the result string in
characters, not including the terminating null, which can be used to
allocate a buffer of the appropriate size.
Only the result from the most recently executed script is retained.
Executing another script will overwrite the previous result, even if the
value has not yet been retrieved. If your application needs to preserve
earlier results, it should copy the returned string into its own buffer
for later use.
When using AsyncExecute, the result is available
after the WEBVIEW_EVENT_SCRIPT_COMPLETED event has been raised.
Applications should wait for this event before retrieving the result
and attempting to call GetWebResult before the event occurs
will return an empty string. When using the Execute method, the
script is executed synchronously and the result is available immediately
after the method returns successfully.
INT nLength = pWebView->Execute(_T("document.title"), NULL, 0);
if (nLength > 0)
{
LPTSTR lpszResult = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
if (lpszResult)
{
if (pWebView->GetWebResult(lpszResult, nLength + 1))
{
_tprintf(_T("Page title is: %s\n"), lpszResult);
}
free(lpszResult);
}
}
else
{
_tprintf(_T("Script execution failed\n"));
}