0

I wrote a function in a DLL that accepts a structure of vectors as an argument which is passed by reference and modifies the argument's data.

I export that function using a .def file to be used by another EXE and processes the data in the Structure of vectors.

Below i the Sample Code for that implementation:

Struct data // Defined in both DLL and EXE
{
   vector<int> IsEnabled;
   vector<string> Path;
};

IN DLL:

void func(data& ret)
{
  data temp;
  temp.IsEnabled.push_back(1); temp.Path.puch_back("C:\\Program Files\\");
  ret = temp;
}

IN EXE:

int main()
{
   if(true)
   {
      data a;
      func(a);
      for(int i = 0; i<a.IsEnabled.size(); i++)
      {
        cout<<a.IsEnabled[i]<<"\t:"<<a.Path[i]<<endl;
      }
   } // End of 'if' condition
}

This program is crashing when it reaches the end of the 'if' condition. I found out the reason for this. But is there any modification that I can make to my code or any other way by which i can achieve what i am trying to do here?

Bharath Suresh
  • 483
  • 2
  • 18
  • 1
    Possible issues with conflicting "calling convention" for your exported function (depending on how you define it in the `.def` file). Maybe [this discussion](https://stackoverflow.com/q/6511096/10871073) will be helpful? – Adrian Mole Jul 22 '20 at 14:13
  • 5
    Passing C++ objects across module boundaries imposes severe restrictions on the tools you can use to produce either side. We don't know anything about how those modules are built. Even if we did, there's a strong consensus towards: Don't, just don't. – IInspectable Jul 22 '20 at 15:08
  • 1
    If you want to exchange data between modules, I suggest you use a [serialized](https://en.wikipedia.org/wiki/Serialization) data format, such as C-style arrays and/or C-style structs (no member functions, just data). When using structs, you should also make sure that there is no padding, because if the compiler of one module inserts padding due to alignment requirements, but the other module's compiler doesn't, then the structs won't be compatible. – Andreas Wenzel Jul 22 '20 at 23:27
  • The scope of 'temp' variable of 'func' is limited only insdie the 'func'. So the contents of 'temp' does not exist outside the 'func'. You should use 'ret' variable directly instead of 'temp' variable. ret.IsEnabled.push_back(1); ret.Path.puch_back("C:\\Program Files\\"); – yamashiro.akihito Jul 24 '20 at 18:34
  • @yamashiro.akihito: I agree that it would be more efficient to modify the variable `ret` directly. However, as far as I can tell, this is only a performance issue and is not the reason for the crash. – Andreas Wenzel Jul 24 '20 at 19:10
  • @AndreasWenzel Thank you. That was helpful. – Bharath Suresh Aug 17 '20 at 05:21

0 Answers0