简体   繁体   English

WriteProcessMemory std::vector<any> 定制任何 class</any>

[英]WriteProcessMemory std::vector<any> custom any class

How would i use Write Process Memory with a std::vector我将如何使用带有 std::vector 的写入过程 Memory

This works if i use std::vector如果我使用 std::vector 这工作

Im not sure if this any class is returning right info when using.Data()我不确定这是否有任何 class 在 using.Data() 时返回正确的信息

class any
{
private:
    struct base {
        virtual ~base() {}
        virtual base* clone() const = 0;
    };
    template <typename T>
    struct data : base {
        data(T const& value) : value_(value) {}
        base* clone() const { return new data<T>(*this); }
        T value_;
    };
    base* ptr_;
public:
    template <typename T> any(T const& value) : ptr_(new data<T>(value)) {}
    any(any const& other) : ptr_(other.ptr_->clone()) {}
    any& operator= (any const& other) {
        any(other).swap(*this);
        return *this;
    }
    ~any() { delete this->ptr_; }
    void swap(any& other) { std::swap(this->ptr_, other.ptr_); }

    template <typename T>
    T& get() {
        return dynamic_cast<data<T>&>(*this->ptr_).value_;
    }
};

template<typename T>
size_t vectorsizeof(const typename std::vector<T>& vec)
{
    return sizeof(T) * vec.size();
}

std::vector<any> args{100, 1.1f};
WriteProcessMemory(hProc, pMemory, args.data(), vectorsizeof(args), nullptr)

There has to be a easy way to use std::any in a vector with Write Process Memory.必须有一种简单的方法在带有写入过程 Memory 的向量中使用 std::any。

No, there does not.不,没有。

Ignoring what std::any does with the value it stores for the moment, std::any stores its value (as if) indirectly.忽略std::any对它存储的值所做的事情, std::any间接存储它的值(好像)。 This means that it is rather like a vector<T> ;这意味着它更像一个vector<T> it stores a pointer to the object which it allocated on the heap (except the T is hidden from the type and it only stores one of them).它存储指向它在堆上分配的 object 的指针(除了T对类型隐藏并且它只存储其中一个)。 So copying the bits of the any itself will not (necessarily) make the T it stores visible;因此,复制any本身的位不会(必然)使其存储的T可见; you are (may be) copying a pointer, not what it points to.您正在(可能)复制指针,而不是它指向的内容。

Furthermore, any can't be delivered across processes like this, for many reasons.此外,由于许多原因, any东西都无法通过这样的流程交付。 Even if you could get access to the byte-range of the object being stored by an any , you wouldn't be able to use that to reconstruct the any on the other side.即使您可以访问由any存储的 object 的字节范围,您也无法使用它来重建另一侧的any And even if you somehow could, you'd somehow have to transmit the type_index that represents the type stored in the any .即使你能做到,你也必须以某种方式传输表示存储在any中的类型的type_index And that isn't cross-process compatible;不是跨进程兼容的; a type index value for a particular type in one process can be different from the index for the same type in the other process.一个进程中特定类型的类型索引值可以不同于另一个进程中相同类型的索引。

What you want simply isn't going to work.你想要的根本行不通。 You'll have to use something else, and that something else is going to have to have some knowledge of the type of data it's transmitting.您将不得不使用其他东西,并且其他东西必须对它正在传输的数据类型有所了解。

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

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