将string转换为DWORD_PTR,将DWORD_PTR转换为string
convert string to DWORD_PTR and DWORD_PTR to string
我有一个函数,参数是DWORD_PTR,我需要将字符串传递给这个函数(将字符串转换为DWORD_PTR),然后我需要在func(将DWORD_PTR转换为字符串)中读取它
std::string myStr = "some string";
myFunc(DWORD_PTR string)
我该怎么做?
注。让我们跳过诸如"为什么使用DWORD_PTR作为参数?"因为我需要它
试试这个:
myFunc(DWORD_PTR data) {
std::string local_copy{ reinterpret_cast<char*>(data) };
}
std::string data = "some string";
myFunc( reinterpret_cast<DWORD_PTR>(data.c_str()) );
的工作原理是将数据所在的地址作为指针传输(并转换为DWORD ptr)。你可以用不同的方式来实现它,但是你没有指定(例如你可以传输std::string的地址)。
注。让我们跳过诸如"为什么使用DWORD_PTR作为参数?"因为我需要它。
好吧,我会跳过这个问题-但我认为这个问题的答案是"因为你有x-y问题"…只是说"。
1.calling myFunc((DWORD_PTR)&myStr);
2.inside the function: std::string& str = *(std::string*)string;
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 以十六进制打印出 DWORD 会在 win32 c++ 中返回0x7FFFFFFF
- 非法,左操作数具有类型 'DWORD [29]'
- 编译时出错 - 从 DWORD 到 LPCVOID 的转换
- 如何在 c++ 中将 DWORD 转换为 PVOID?
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- C++添加 2 个 DWORD 的算术溢出
- 此指令中的错误 C2415 "MOV Incoming_Packet_Length,DWORD PTR SS:[EBP-14]"
- MOV EAX, DWORD PTR SS:[EBP+8h] 的含义是什么,我怎样才能把它翻译成 AT&T 格式?