0

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.

Vikyboss
  • 940
  • 2
  • 11
  • 23
  • Not actually, I need to pass that into the CreateFile as the first parameter. And for 32bit it worked, the problem only happens in 64bit. Thanks for the quick reply. – Vikyboss Jan 16 '12 at 20:30
  • 2
    PtrToStringChars always returns a const wchar_t; are your 64-bit builds not compiling to Unicode? – Joe Jan 16 '12 at 21:14
  • 1
    Thanks Sir. I have answered my question. You got it right, I set the character set to unicode. It was set to No Character Set. Also Multi-Byte Character set gave same error message. But Unicode fixed it. Thanks Again. – Vikyboss Jan 16 '12 at 22:04

2 Answers2

1
  1. Right Click on the project
  2. Properties
  3. Configuration Properties
  4. General
  5. Character-Set "Use Unicode Character Set"

This fixed the problem.

Vikyboss
  • 940
  • 2
  • 11
  • 23
0

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thanks, I will try this one out. But I was wondering what does these character set do? Like setting Multi-Byte will not work on all computers? I'm new to these and would appreciate if you point in a direction. Thanks. – Vikyboss Jan 16 '12 at 22:19
  • 1
    @Vikyboss: It isn't all that important anymore, since most people have upgraded, but Win9x doesn't have full Unicode support. Multi-byte mode (which is single-byte actually, except for Far-Eastern languages) will run on all versions of Windows, Unicode builds usually only on NT: Win2000, XP, Vista, Win7. On Win9x, `CreateFileW` isn't available, so you'd actually have to call the Unicode->MBCS conversion function `WideCharToMultiByte` in order to make a usable filename. Modern versions of Windows can use Unicode filenames directly. – Ben Voigt Jan 16 '12 at 22:43