pin_ptr and interior_ptr in vc++
-
16-04-2021 - |
Question
I am working on a project that was written(a HID inteface for STM32) by a person who worked before in visual c++ 2008. So to imitate the line that is causing problem, I created a sample winform application in VC++ 2008. Here is the click event with this one line giving build error only when built for x64, but a win32 build doesn't give any building error and works fine.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ devPath = this->textBox1->Text;
MessageBox::Show(devPath);
pin_ptr<const TCHAR> pPath = PtrToStringChars(devPath); *error line
}
};
and the the build error that appears only for x64 build is:
Error 1 error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'cli::pin_ptr<Type>'
Thanks.
Solution
- Right Click on the project
- Properties
- Configuration Properties
- General
- Character-Set "Use Unicode Character Set"
This fixed the problem.
OTHER TIPS
The "better" solution is probably:
pin_ptr<const WCHAR> pPath = PtrToStringChars(devPath);
and then use CreateFileW
, because you have a Unicode string.
That way your code will work regardless of project-file Unicode configuration.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow