0

I have a process which calls a DLL several times. In the DLL, I have some variables which have to be initialized from a XML file and their values will remain same. Just as an example, let's say my process is following:

  1. Ask user to enter the Name
  2. Ask user to enter the Employee ID
  3. Call DLL and read the values of Company Name, Street, City and ZIP Code from a XML file
  4. Generate a report.
  5. Repeat from step-1 (loop)

QUESTION: As the values of the Company Name, Street, City and ZIP Code defined in XML file will remain same so, there is no point of reading the XML file for every call to this DLL in this looped process. How can I set the variable in DLL during first call and use them for rest of the calls?

UPDATE: As some answers has suggested to use global variables and initialize them once after reading from XML file but unfortunately, it is not possible in my process. It is because of the reason that only the DLL is in the C++ and rest of the process is using an another framework. Though, I can extract the values from DLL and assign to the variables of another framework but this DLL is a part of big project and I am not allowed to modify the variables and another stuff of the main project. That's why I have to find a way that this DLL maintains its own copy of variables.

skm
  • 5,015
  • 8
  • 43
  • 104

4 Answers4

1

Create a class which reads the XML file and initializes the member variables with Company Name, Street, City and ZIP Code from XML file in the Constructor.

Create a global instance of this class. Now use the global instance of this class in the function in your DLL.

  • thanks for the answer, but I can't do that. kindly have a look at UPDATE. Please suggest. – skm Dec 08 '16 at 08:22
0

Keyword is cache. :) I suggest you read values from the file and then store them (preferably in a map) for subsequent calls.

Although this problem can be solved using global variables, this practice is widely discouraged https://stackoverflow.com/a/19374253/1341515

Community
  • 1
  • 1
Anton Shmelev
  • 75
  • 3
  • 7
  • thanks for the answer, but I can't do that. kindly have a look at UPDATE. Please suggest. – skm Dec 08 '16 at 08:23
0

you can read the content of your XML file at first in the DLL Entry and keep it in a global variable. every time you call the function from your Dll (Step 3) the function should read the datas from that global variable.

Arash
  • 383
  • 4
  • 16
  • thanks for the answer, but I can't do that. kindly have a look at UPDATE. Please suggest. – skm Dec 08 '16 at 08:22
0

Use of Singleton class has helped me initialize the variable (read from XML) only once. I defined the variables which have to be read from the XML file as the member variables of the Singleton class. During the creation of the first object instance to this singleton class, I read the XML file and assigned the values to the corresponding member variables. Since, an object to a singleton class is created only one time so, all the calls to the DLL uses the same values of the variables (which were read from the XML).

skm
  • 5,015
  • 8
  • 43
  • 104