    

 ## On this page

  

 

 # Example 26 - C++ 

 Last update: 16.07.2025 

# <a class="anchor" id="ex26_s2"></a>C++

// CppStandaloneApplication.cpp : Defines the entry point for the console application.

//

 

\#include "stdafx.h"

\#include &lt;stdlib.h&gt;

\#include &lt;stdio.h&gt;

\#include &lt;iostream&gt;

\#include &lt;string&gt;

\#include &lt;ctime&gt;

\#include &lt;functional&gt;

\#include &lt;assert.h&gt;

\#include &lt;map&gt;

\#include &lt;io.h&gt; 

 

 

// Note - .tlh files will be generated from the .tlb files (above) once the project is compiled.

// Visual Studio will incorrectly continue to report IntelliSense error messages however until it is restarted.

\#include "zosapi.h"

 

using namespace std;

using namespace [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using namespace ZOSAPI_Interfaces;

 

void handleError(std::string msg);

void logInfo(std::string msg);

void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication);

 

int RunApplication()

{

 CoInitialize(nullptr);

 

 // Create the initial connection class

 IZOSAPI_ConnectionPtr TheConnection(__uuidof([ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)));

 

 // Define a Preferences File.

 // Preferences file is defined on the IZOSAPIConnection interface (prior to connecting to the API)

 // If no PreferencesFile is defined it will use the default OpticStudio.CFG file however changes will not persist between sessions. 

 // If a PreferencesFile is defined, then any changes will save automatically. 

 cout &lt;&lt; "===PreferenceFile===\\n";

 _bstr_t cfgFile = R"(C:\\Users\\Documents\\Zemax\\Configs\\OpticStudio.CFG)";

 std::string strFile = \_bstr\_t(cfgFile, false);

 if (_access_s(strFile.c_str(), 0) == 0) {

 TheConnection-&gt;PreferencesFile = cfgFile;

 cout &lt;&lt; "PreferencesFile: " + TheConnection-&gt;PreferencesFile + "\\n";

 }

 else {

 cout &lt;&lt; "Default OpticStudio.CFG preferences used\\n";

 }

 

 // Attempt to create a Standalone connection

 IZOSAPI_ApplicationPtr TheApplication = TheConnection-&gt;CreateNewApplication();

 if (TheApplication == nullptr)

 {

 handleError("An unknown error occurred!");

 return -1;

 }

 

 // Check the connection status

 if (!TheApplication-&gt;IsValidLicenseForAPI)

 {

 handleError("License check failed!");

 return -1;

 }

 if (TheApplication-&gt;Mode != ZOSAPI_Mode::ZOSAPI_Mode_Server)

 {

 handleError("Standlone application was started in the incorrect mode!");

 return -1;

 }

 

 IOpticalSystemPtr TheSystem = TheApplication-&gt;PrimarySystem;

 

 // Add your custom code here...

 

 // Define variables for OpticStudio Preferences

 IPreferencesPtr Preference = TheApplication-&gt;Preferences;

 IPreferencesGeneralPtr PrefG = Preference-&gt;General;

 

 // Define the enums to a map/dictionary

 std::map&lt;int, const char*&gt; DateTimeFormatLookup;

 DateTimeFormatLookup[DateTimeType::DateTimeType_None] = "None";

 DateTimeFormatLookup[DateTimeType::DateTimeType_Date] = "Date";

 DateTimeFormatLookup[DateTimeType::DateTimeType_DateTime] = "DateTime";

 

 std::map&lt;int, const char*&gt; languageLookup;

 languageLookup[LanguageType::LanguageType_Chinese] = "Chinese";

 languageLookup[LanguageType::LanguageType_English] = "English";

 languageLookup[LanguageType::LanguageType_Japanese] = "Japanese";

 

 std::map&lt;int, const char*&gt; EncodingTypeLookup;

 EncodingTypeLookup[EncodingType::EncodingType_ANSI] = "ANSI";

 EncodingTypeLookup[EncodingType::EncodingType_Unicode] = "Unicode";

 

 // Read and print the initial settings 

 cout &lt;&lt; "\\n===Check Settings===";

 cout &lt;&lt; "\\nDateTimeFormat: " &lt;&lt; DateTimeFormatLookup[PrefG-&gt;DateTimeFormat];

 cout &lt;&lt; "\\nLanguage: " &lt;&lt; languageLookup[PrefG-&gt;Language];

 cout &lt;&lt; "\\nZMXFileEncoding: " &lt;&lt; EncodingTypeLookup[PrefG-&gt;ZMXFileEncoding];

 cout &lt;&lt; "\\nTXTFileEncoding: " &lt;&lt; EncodingTypeLookup[PrefG-&gt;TXTFileEncoding];

 cout &lt;&lt; "\\nUseSessionFiles: " &lt;&lt; std::boolalpha &lt;&lt; (bool)PrefG-&gt;UseSessionFiles;

 cout &lt;&lt; "\\nIncludeCalculatedDataInsession: " &lt;&lt; (bool)PrefG-&gt;IncludeCalculatedDataInSession;

 cout &lt;&lt; "\\nUpdateMostRecentlyUsedList: " &lt;&lt; (bool)PrefG-&gt;UpdateMostRecentlyUsedList;

 

 // Check if the user preferences string is empty before printing it out 

 if (PrefG-&gt;UserPreferences.length() == 0)

 cout &lt;&lt; "\\nUserPreferences: \[None\]";

 else

 cout &lt;&lt; "\\nUserPreferences: " &lt;&lt; PrefG-&gt;UserPreferences;

 

 cout &lt;&lt; "\\n";

 

 // Reset the settings to default

 Preference-&gt;ResetToDefaults();

 

 // Set the settings 

 PrefG-&gt;DateTimeFormat = DateTimeType_None;

 PrefG-&gt;ZMXFileEncoding = EncodingType_ANSI;

 PrefG-&gt;TXTFileEncoding = EncodingType_ANSI;

 PrefG-&gt;UseSessionFiles = false;

 PrefG-&gt;IncludeCalculatedDataInSession = false;

 PrefG-&gt;UpdateMostRecentlyUsedList = false;

 PrefG-&gt;UserPreferences = "Never gonna give you up, never gonna let you down";

 

 cout &lt;&lt; "\\n===Final Settings===";

 cout &lt;&lt; "\\nDateTimeFormat: " &lt;&lt; DateTimeFormatLookup[PrefG-&gt;DateTimeFormat];

 cout &lt;&lt; "\\nLanguage: " &lt;&lt; languageLookup[PrefG-&gt;Language];

 cout &lt;&lt; "\\nZMXFileEncoding: " &lt;&lt; EncodingTypeLookup[PrefG-&gt;ZMXFileEncoding];

 cout &lt;&lt; "\\nTXTFileEncoding: " &lt;&lt; EncodingTypeLookup[PrefG-&gt;TXTFileEncoding];

 cout &lt;&lt; "\\nUseSessionFiles: " &lt;&lt; std::boolalpha &lt;&lt; (bool)PrefG-&gt;UseSessionFiles;

 cout &lt;&lt; "\\nIncludeCalculatedDataInsession: " &lt;&lt; (bool)PrefG-&gt;IncludeCalculatedDataInSession;

 cout &lt;&lt; "\\nUpdateMostRecentlyUsedList: " &lt;&lt; (bool)PrefG-&gt;UpdateMostRecentlyUsedList;

 if (PrefG-&gt;UserPreferences.length() == 0)

 cout &lt;&lt; "\\nUserPreferences: \[None\]";

 else

 cout &lt;&lt; "\\nUserPreferences: " &lt;&lt; PrefG-&gt;UserPreferences;

 

 

 cout &lt;&lt; "\\n\\n";

\#if defined(\_DEBUG)

 // keeps console open when in debug mode

 system("pause");

\#endif

 

 // Clean up

 finishStandaloneApplication(TheApplication);

 

 

 return 0;

}

 

 

 

void handleError(std::string msg)

{

 throw new exception(msg.c_str());

}

 

void logInfo(std::string msg)

{

 printf("%s", msg.c_str());

}

 

void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication)

{

 // Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases

 if (TheApplication != nullptr)

 {

 TheApplication-&gt;CloseApplication();

 }

}

 

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)

{

 return RunApplication();

}

 

int _tmain(int argc, _TCHAR* argv[])

{

 return RunApplication();

}

[ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)

**Definition:** ZemaxService.cs:198



[ZOSAPI](namespace_z_o_s_a_p_i.xhtml)

The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...

**Definition:** IAS_FieldCurvatureAndDistortion.cs:5