将string转换为DWORD_PTR,将DWORD_PTR转换为string

convert string to DWORD_PTR and DWORD_PTR to string

本文关键字:PTR DWORD string 转换      更新时间:2023-10-16

我有一个函数,参数是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;