简体   繁体   中英

C++ How to make a dll modify a variable from main thread?

I currently have a dll which helps me with some operations, and i would like that my dll can modify a variable of the main thread. Do I somehow send the class(the variable has a typename of a class) to the dll, should i also make the dll include the file where the class is defined(.h file).

So basically, how can i make a dll modify a variable from another thread(In this case, the main thread)?

Edit:

The variable i want to change is like this:

Group< UMesh > InstancedMesh

Group is a template class and UMesh is another class.

Do I somehow send the class(the variable has a typename of a class) to the dll, should i also make the dll include the file where the class is defined(.h file).

No, you shall not make your library depends on some application type. How this library would be then usable for other applications?


Your library shall only accept objects of types known to this library.

So, you have the type in your application:

template <class T>
class Group;

And you want some library function accepting objects of this type.

There are two typical solutions:

First solution , let name it ObjectOriented solution, Make an interface in your library and function accepting objects implementing this interface:

class SomInterface {
public:
  virtual void foo1() = 0;
  virtual void foo2() = 0;
};

void doSomething(SomeInterface& object);

Then to use doSomething in your application, either your class shall derive from this interface:

template <class T>
class Group : public SomeInteface {
public:
  virtual void foo1() {...}
  virtual void foo2() {...}
};
Group<UMesh> object;
doSomething(object);

Or (I prefer this) make an adapter from your class to this interface:

template <class T>
class GroupSomeInterface : public SomeInteface {
public:
  GroupSomeInterface(Group<UMesh& object) : object(object) {}
  virtual void foo1() {...}
  virtual void foo2() {...}
private:
  Group<UMesh& object;
};
Group<UMesh> object;
GroupSomeInterface<UMesh> adapter(object);
doSomething(adapter);

Second solution , let name it generic solution, Since you have template class, it should be better for this particular case. Make a function template in your library accepting any type of some interface:

template <class T>
void doSomething(T& object);
Group<UMesh> object;
doSomething(object);

If your Group<T> does not have the interface this method requires, then make an adapter like in first solution - but this time without explicit interface class and with no virtualism:

template <class T>
class GroupSomeInterface  {
public:
  GroupSomeInterface(Group<UMesh& object) : object(object) {}
  void foo1() {...}
  void foo2() {...}
private:
  Group<UMesh& object;
};
Group<UMesh> object;
GroupSomeInterface<UMesh> adapter(object);
doSomething(adapter);

BTW, by object interface I mean all its public functions and all global functions operating on this object (like operator << (ostream,Object) for example)

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