Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Chilkat Functions Returning a StringThis 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.
#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.