CWebView::GetWebResult Method  
 
BOOL GetWebResult(
  LPTSTR lpszResult,  
  INT nMaxLength  
);
BOOL GetWebResult(
  CString& strResult  
);

The GetWebResult method returns the result from the most recent completed script execution.

Parameters

lpszResult
A null-terminated string buffer which will contain the result from the script that was previously executed. If this parameter is NULL, the result will not be copied and the method will return the length of the string, 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 lpszResult parameter is NULL, this value should be zero.

Return Value

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.

Remarks

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.

Example

// Execute a simple script that returns a string
INT nLength = pWebView->Execute(_T("document.title"), NULL, 0);

if (nLength > 0)
{
    // Allocate a buffer large enough to hold the result
    LPTSTR lpszResult = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
    if (lpszResult)
    {
        // Retrieve the result of the script execution
        if (pWebView->GetWebResult(lpszResult, nLength + 1))
        {
            _tprintf(_T("Page title is: %s\n"), lpszResult);
        }
        free(lpszResult);
    }
}
else
{
    _tprintf(_T("Script execution failed\n"));
}

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