Lets say i have the following function in my DLL:
void TestFunction(int type, void* data)
Now that function is called from an application which loads that DLL. The application initializes a structure and sends a pointer to that structure to that function:
SampleStruct strc;
TestFunction(DT_SS, &ss);
So far so good. Now what troubles me is how to replace the strcc variable in memory with another structure. If i do the following in my dll:
SampleStruct dllstrcc;
data = &dllstrcc;
data will now point to the new dllstrcc structure however when it exists the function and the control returns to the application the strc will still point to the first structure. How can i replace the structure of the application with my structure from the dll without assigning each field:
data.vara = dllstrcc.vara;
data.varb = dllstrcc.varb;
data.varc = dllstrcc.varc;
1. The simplest option is to copy the whole struct:
And call it via
The benefit is that you don’t have to worry about freeing memory, etc.
2. If you really want to replace the structure of the caller (have a new structure), you can allocate a new structure in the DLL.
(I’ll
returnthe new structure because it’s much easier, but you could pass it out through a parameter if you need to by usingvoid** data.)You can call the function like:
Don’t forget to delete the pointer, otherwise you’ll leak memory. You should explicitly decide who’s responsibility it is to free the memory, the caller’s or the DLL’s.