2014-12-27 44 views
-1

我会尽我所能解释我的问题。所以,我用C/C++编写的应用程序(客户端应用程序中的语言无关紧要),它从DLL导入一个函数,例如uint32_t * GetMemoryPointer()。然后它在序列写入该存储器指针这样的:观看从应用程序接收到的指定内存指针的写入

uint32_t* ptr = (uint32_t*)GetMemoryPointer(); 
*ptr = 3; 
*ptr = 4; 
*ptr = 1050; 

它这样做是序列中,而不将该值改变为DLL的任何信息。是否有可能在DLL中观看这个值?我试图做一个线程和循环查找更改,但它不可靠。有更好的解决方案吗?我对这样做感兴趣:应用程序写入,DLL发现该值已更改,HOLDS应用程序执行然后解释此值,然后允许应用程序继续执行。另一种不需要申请的方式可能会推动堆叠的新价值,但我需要了解每一个变化。我感兴趣的平台是Windows。语言无所谓可能是C或C++。是否有可能实现这一目标?这对我来说非常重要,而且我没有想法。我不想要代码,但我希望被告知是否有可能以及需要采用哪种方式。提前致谢。

+1

imho,这是一个XY问题。 http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem你应该解释你想达到什么。 – manuell 2014-12-27 15:17:14

+0

我解释了我想实现的目标,我想实现对DLL中映射的内存小区域(例如4B变量)的监视,并在发生应用程序端更改时得到通知。就这样。 – 2014-12-27 16:08:19

+0

你没有解释为什么你想监视一个小区域的内存。您无法控制客户端应用程序? – manuell 2014-12-27 16:17:43

回答

0

嗯,在我的头顶,如果你可以将内存标记为只读,并且当有人试图写入内存时,操作系统将抛出一个异常/错误,你必须抓住它。我不知道是否有任何图书馆存在,所以请尝试使用Google搜索。

1

一个选项是实现一个Value类型,该类型保存要监视的实际数据,并在值更改时使用observer pattern来分派通知。从一个简单的实现开始,该实现包含所需类型的值(在本例中为uint32_t)以及赋值运算符,该运算符在操作员更改该值时调用回调。

下面的例子就是这样做的,它包含一个转换运算符以允许用其他uint32_t值执行相当数量的操作。您可以对此进行扩展以满足您的要求,包括提供一整套运营商(operator+,operator/等),以使其更健壮。

#include <iostream> 
#include <vector> 
#include <cstdint> 


class Value 
{ 
    uint32_t value; 
    std::vector<void(*)()> observers; 

public: 

    Value() : value(0) {} 

    // Allows you to register a observer that gets called when 
    // the value changes 
    void RegisterListener(void (*f)()) 
    { 
     observers.push_back(f); 
    } 

    // Conversion operator that allows implicit conversions 
    // from Value to uint32_t. 
    operator uint32_t() const 
    { 
     return value; 
    } 

    Value& operator=(uint32_t newValue) 
    { 
     // Only alert observers if the value is actually changing. 
     if (value != newValue) 
     { 
      value = newValue; 
      for (std::vector<void(*)()>::const_iterator it = observers.begin(); 
       it != observers.end(); 
       ++it) 
      { 
       // Call the observer 
       (*it)(); 
      } 
     } 
     return *this; 
    } 
}; 

void Callback() 
{ 
    std::cout << "value changed\n"; 
} 

int main() 
{ 
    Value value; 
    value.RegisterListener(Callback); 

    // Value held in object can be assigned to a uint32_t due to the 
    // conversion operator. 
    uint32_t original = value; 

    // Change the value see the callback get invoked 
    value = value + 1; 

    // Restore the value to it's original and see the callback get invoked. 
    value = original; 
} 
+0

可悲的是,这不会在我的情况。当从DLL接收到指向(uint32_t *)的指针时,应用程序端的回调函数和运算符不会执行。 – 2014-12-27 16:06:52

+0

@cafebabe_t然后你做错了。无论您是从共享库(DLL),应用程序(EXE)还是两者的组合,此解决方案都能正常工作。 – 2014-12-27 18:21:08