0
BOOL CTestBMPDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
    if (! CDocument::OnOpenDocument(lpszPathName))
        return FALSE;

    m_pvtkBMPReader = vtkBMPReader::New();
    m_pvtkBMPReader->SetFileName(lpszPathName);

    return TRUE;
 }

The above code doesn't compile and produces the C2664 error. Why?

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
Jerin
  • 41
  • 8
  • as the error says, you are passing the wrong argument type to the function `setFileName`. it expects a `char*` and you provide a `LPCTSTR` – ᴄʀᴏᴢᴇᴛ Feb 15 '17 at 11:08

1 Answers1

0

Your VC++ project is setup as a UNICODE so all the references to TCHAR are resolved into a wide character type w_char. However your vtk's SetFileName() function doesn't support UNICODE thus the compilation error.

To fix this you should either change the properties of your project to use ASCII or MBCS (depending on your needs) or do the manual UNICODE->MBCS (or into ASCII) conversion.

Below is an example on how to convert UNICODE into MBCS using system codepage:

const size_t fnameLen = 1024;
char * fname[fnameLen];
int converted = WideCharToMultibyte(
                      CP_ACP
                    , WC_COMPOSITECHECK | WC_ERR_INVALID_CHARS
                    , lpszPathName
                    , -1 // null-terminated string
                    , fname
                    , fnameLen
                    , NULL // or whatever you'd like it to be
                    , NULL);
 m_pvtkBMPReader->SetFileName(fname); // <-- this should work now as it is char*

Please note that I have just written down that code without compiling, so expect that you may need some tweaking before this builds and runs properly.

ATL and MFC provide a bunch of convenient macroses to simplify the string conversions.

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • http://stackoverflow.com/questions/4786292/converting-unicode-strings-and-vice-versa (Google is your friend ;-)) – YePhIcK Feb 15 '17 at 11:20