这可能是一个非常愚蠢的问题,但这是我一直在努力的事情.在方法中更改LPWSTR后,它似乎只是针对该特定方法进行更改并在之后立即恢复.我知道全局变量是邪恶的,但这不是我的选择,因为它需要更改相当多的代码.这是我正在做的一个例子:
Test.h
static LPWSTR globalStr = L"This shouldn't be here.";
// The ...s are irrelevant code.
class Test {
public:
...
void changeGlobalStr();
void testMethod();
...
...
};
Run Code Online (Sandbox Code Playgroud)
TEST.CPP
#include "Test.h"
Test::changeGlobalStr() {
string testString("This should be here.");
// I manipulate testString over the next few lines so a variable is necessary.
...
BSTR bConversion = _com_util::ConvertStringToBSTR(testString.c_str());
globalStr = bConversion
// This prints out the correct output.
wcout << "globalStr in changeGlobalStr(): " << globalStr;
}
Run Code Online (Sandbox Code Playgroud)
SecondTest.cpp
#include "Test.h"
Test::testMethod() …Run Code Online (Sandbox Code Playgroud)