Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Chilkat Functions Returning a String

This example explains Chilkat functions in MFC that directly return a string. These functions begin with a lowercase letter.

If a string is returned directly by a Chilkat method (in MFC), it is a pointer into internal memory managed by Chilkat. It is not guaranteed to stay in memory forever. In fact, there are 10 possible internal buffers to hold the strings returned by a lowercase function. After the 10th lowercase function is called, the string returned by the least recent function call is no longer valid.

The intent of returning the string directly is for convenience. It is for cases where the application will immediately use the string, or for cases where the application immediately copies the string to an object or buffer under its own management.

This example demonstrates the 10 internal buffer limit.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // This example demonstrates the 10 internal buffer limit for functions returning strings.
    // We'll demonstrate using the JSON object.

    CkJsonObject json;

    // First, build a JSON document.
    json.UpdateString("x1","1");
    json.UpdateString("x2","2");
    json.UpdateString("x3","3");
    json.UpdateString("x4","4");
    json.UpdateString("x5","5");
    json.UpdateString("x6","6");
    json.UpdateString("x7","7");
    json.UpdateString("x8","8");
    json.UpdateString("x9","9");
    json.UpdateString("x10","10");
    json.UpdateString("x11","11");
    json.UpdateString("x12","12");

    // Call 12 functions in a row that directly return strings.
    // This is an error because there are a max of 10 internal buffers per Chilkat object
    // to hold the returned strings.
    // The calls to get "x11" and "x12" overwrite the internal buffers used by x1 and x2.
    const char *x1 = json.stringOf("x1");
    strOut.append("x1: ");
    strOut.append(x1);
    strOut.append("\r\n");
    const char *x2 = json.stringOf("x2");
    strOut.append("x2: ");
    strOut.append(x2);
    strOut.append("\r\n");
    const char *x3 = json.stringOf("x3");
    strOut.append("x3: ");
    strOut.append(x3);
    strOut.append("\r\n");
    const char *x4 = json.stringOf("x4");
    strOut.append("x4: ");
    strOut.append(x4);
    strOut.append("\r\n");
    const char *x5 = json.stringOf("x5");
    strOut.append("x5: ");
    strOut.append(x5);
    strOut.append("\r\n");
    const char *x6 = json.stringOf("x6");
    strOut.append("x6: ");
    strOut.append(x6);
    strOut.append("\r\n");
    const char *x7 = json.stringOf("x7");
    strOut.append("x7: ");
    strOut.append(x7);
    strOut.append("\r\n");
    const char *x8 = json.stringOf("x8");
    strOut.append("x8: ");
    strOut.append(x8);
    strOut.append("\r\n");
    const char *x9 = json.stringOf("x9");
    strOut.append("x9: ");
    strOut.append(x9);
    strOut.append("\r\n");
    const char *x10 = json.stringOf("x10");
    strOut.append("x10: ");
    strOut.append(x10);
    strOut.append("\r\n");
    // This call invalidates the memory pointed to by x1.
    const char *x11 = json.stringOf("x11");
    strOut.append("x11: ");
    strOut.append(x11);
    strOut.append("\r\n");
    // This call invalidates the memory pointed to by x2.
    const char *x12 = json.stringOf("x12");
    strOut.append("x12: ");
    strOut.append(x12);
    strOut.append("\r\n");
    strOut.append("---");
    strOut.append("\r\n");

    // If we try to get x1 and x2 again, 
    // we can see how the buffers were overwritten.
    // In fact, x1 and x2 may point to already-deleted memory,
    // and accessing the memory at these pointers may cause a crash.
    strOut.append("x1: ");
    strOut.append(x1);
    strOut.append("\r\n");
    strOut.append("x2: ");
    strOut.append(x2);
    strOut.append("\r\n");
    strOut.append("x3: ");
    strOut.append(x3);
    strOut.append("\r\n");
    strOut.append("x4: ");
    strOut.append(x4);
    strOut.append("\r\n");
    strOut.append("x5: ");
    strOut.append(x5);
    strOut.append("\r\n");
    strOut.append("x6: ");
    strOut.append(x6);
    strOut.append("\r\n");
    strOut.append("x7: ");
    strOut.append(x7);
    strOut.append("\r\n");
    strOut.append("x8: ");
    strOut.append(x8);
    strOut.append("\r\n");
    strOut.append("x9: ");
    strOut.append(x9);
    strOut.append("\r\n");
    strOut.append("x10: ");
    strOut.append(x10);
    strOut.append("\r\n");
    strOut.append("x11: ");
    strOut.append(x11);
    strOut.append("\r\n");
    strOut.append("x12: ");
    strOut.append(x12);
    strOut.append("\r\n");


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.