简体   繁体   中英

C/C++ - Pointer passed to function in DLL

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:

void TestFunction(int type, void* data) {
    SampleStruct dllstrcc;
    // fill dllstrcc here...
    SampleStruct *p_ret = data;
    *p_ret = dllstrcc;
}

And call it via

SampleStruct strcc;
TestFunction(type, &strcc);

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.

void* TestFunction(int type) {
    SampleStruct* pdllstrcc = new SampleStruct();
    return pdllstrcc;
}

(I'll return the new structure because it's much easier, but you could pass it out through a parameter if you need to by using void** data .)

You can call the function like:

SampleStruct *strcc = TestFunction(type);
// do something with the struct
delete strcc;

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.

you could change the function to

void *func(int,void *) 

and return the new struct - but note that it has to be allocated on the heap with new or malloc, and later freed with free or delete by the caller

btw, doesnt the default assignment operator do what you need?

sampleStruct newStruct;
sampleStruct *tmp=(sampleStruct *)data;
*tmp=newStruct;

Are you coding in c or in c++?

First: if you want to call the function like this:

SampleStruct strc;
TestFunction(DT_SS, &strc);

You can't. what would it mean to replace &strc ? You are trying to replace the address of a structure? That has no meaning. Also in c++ you don't use void * , you use SampleStruct *

if you want to replace something you have to call it like this:

SampleStruct strc;
SampleStruct * pstrc = & strc;
TestFunction(DT_SS, pstrc);

now you can replace pstrc with your result, if you write your function like this:

void TestFunction(int type, SampleStruct * & data)

Notice the & , this means you're passing the pointer data as a reference. Now you can write data = & dllstrcc; , and it will modify pstrc , because data is not a variable, but a reference to pstrc . But you may want to learn about memory handling and memory leaks before you try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM