Read and parse data from text file in DLL using in C++ way

  • 11 June 2020
  • 1 reply

Userlevel 6
Badge +2

In this forum post, a sample C++ code to read value from a text file in DLL is shared.


This is mainly a supplement to the follwoing knowledge base article. The only difference is we will do the similar thing with C++ code to how how to do this in C++ style.



How to read a static data file into a user-defined surface



In the attachments, a sample code is attached. This code does the same thing as shown in the following forum post. The difference is the diffraction efficiency data is read from a text file.



Simulate 2D diffraction grating using customized diffractive DLL



We will explain line by line in below.






The first thing to do is to define a global variable, which is declared outside of all the functions. As discussed here, during ray-tracing in OpticStudio, the DLL will be called several times. For the global variable, the inside value is unchanged and the DLL can always see the same data in different thread. Our plan is to read data from text file and save it in this global variable when OpticStudio call the DLL first time. Then later we don't need to read text file again and just need to read the saved data in the global variable.



It's suggested not to use pointers in C++ except you think the vector<> cannot handle the data you need. Here we defined two vectors for double: Rdata and Tdata, as well as two integers to save number diffraction order in x and y directions.






Note we need to include to use the STL container vector.






Inside of the DllMain() function, we first define some variable for reading and parsing data from text file.






We first check the value of ul_reason_for_all. There are 4 values but we will only use the case DLL_PROCESS_ATTACH. This will be true only when OpticStudio first time call this DLL. Note the DLL_PROCESS_DETACH will then be called when we remove the DLL in OpticStudio or just close OpticStudio.






Now let's check what is done to read text file in the case of DLL_PROCESS_ATTACH. The line 39-48 as shown below is to 'open' the text file. Here we assume the text file name is 2D_grating_efficiency_data.txt but you can change it if needed.






Note if open of the text file failed, an error is reported and the DLL returns FALSE to OpticStudio. Below shows how the error looks like. The left side message returned by the DLL. The right side is message returned by OpticStudio when the DLL returns FALSE.



200611-150401-image.png 200611-150409-image.png



There are many ways to parse data. Here we introduces the way with stringstream. Just consider line 51-53 below as a triplet to read a new line in the text file. Make sure the value at this line is separated by spaces in the text file. Then the data can be simply parse as shown in lin 55. Line 57-62 does some data validity check. Line 63-64 makes sure the value is odd integer. This is just an example and users need to decide how the format should be for the DLL.






It's important to define the size of the vector before you save data into it, or error will happen.






Then in line 72-96, we have a for loop to read each data. It's supposed to be num_nx*num_ny lines from the line 2 and we will read them one line by one.



Line 75 and 77 change the loop index to corresponded order index for x and y. This DLL assumes the data is also defined in this way in the text.



Line 78-79 check whether we reached the end of the file before data of all orders are read. If so, return an error.



Line 82 to 84 is the 'triplet' as discussed above.



Line 86-95 parse the efficiency data for reflection and transmission, check the parse is successful, and save the value into vector.



Line 97 closes the opened text file.






Then in the main code, we can simply use the data from the global variable Rdata and Tdata as shown below.









Note we didn't talk too much about how to use the 'vector', but you should be able to find many resources from search engine. For example, the following one a good introduction for people who first time use the type.






Also attached is a sample system as well as a sample text file. The sample text file should be put in the \Zemax\DLL\Diffractive\ as we defined in the code.








1 reply

Userlevel 6
Badge +2

Note: If an error shows like 'argument of type 'xxxxxx' is imcompatible with parameter of type 'LPCSTR'' for the function MessageBox, change the function name to MessageBoxA.

Similarly, if an error shows like 'argument of type 'xxxxxx' is imcompatible with parameter of type 'LPCWSTR'' for the function MessageBox, change the function name to MessageBoxW.