简体   繁体   English

C ++如何使dll从主线程修改变量?

[英]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. 我目前有一个可以帮助我进行一些操作的dll,我希望我的dll可以修改主线程的变量。 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). 我是否应该以某种方式将类(变量具有类的类型名称)发送到dll,我是否还应使dll包含定义了类的文件(.h文件)。

So basically, how can i make a dll modify a variable from another thread(In this case, the main thread)? 所以基本上,我怎样才能让dll修改另一个线程(在这种情况下,是主线程)的变量?

Edit: 编辑:

The variable i want to change is like this: 我要更改的变量是这样的:

Group< UMesh > InstancedMesh 组<UMesh> InstancedMesh

Group is a template class and UMesh is another class. 组是模板类,而UMesh是另一个类。

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). 我是否应该以某种方式将类(变量具有类的类型名称)发送到dll,我是否还应使dll包含定义了类的文件(.h文件)。

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: 第一个解决方案 ,将其命名为ObjectOriented解决方案,在您的库中创建一个接口,并接受实现该接口的对象的函数:

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: 然后在您的应用程序中使用doSomething时,您的类将从该接口派生:

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: 如果您的Group<T>没有此方法所需的接口,则像在第一个解决方案中一样制作一个适配器-但这一次没有显式的接口类并且没有虚拟性:

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) 顺便说一句,通过对象接口,我的意思是所有在此对象上操作的公共函数和所有全局函数(例如, operator << (ostream,Object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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