hello all.
i have problem in loading irr file to my mfc app.
for loading irr file i have to use COpendialog .
but it's works with CString and loadscene() works with char .
how can i solve this problem ( converting Cstring to char ).
coverting from CString to char
They are MFC classes. You should really be looking at the MFC documentation to find the answer. That said, the MFC CString class provides a conversion operator to LPCTSTR which is actually a const TCHAR*. Unless you are compiling your code with _UNICODE defined a TCHAR is actually a char.
So anywhere that you need a const char* you can just pass a CString. The necessary conversion will happen automatically.
Travis
So anywhere that you need a const char* you can just pass a CString. The necessary conversion will happen automatically.
Travis
thankyou vitek.
forget MFC . i am using single win32 app
i just got these resualts
resualt is many unknown characters and also engine log : unable to load scene file + unknown characters.
resualt : first character of the filename . (example : file name = scene1.irr -> resualt = s)
and also engine log : unable to open scene file :s
i can not load my scene yet .
if it is possible please help me in this case .
forget MFC . i am using single win32 app
i just got these resualts
Code: Select all
//first code
CFileDialog open(TRUE);
const TCHAR* filename;
if (open.DoModal() == IDOK)
{
filename = open.GetFileName();
}
Smgr->loadScene((irr::c8*)filename);
Code: Select all
// second code
CFileDialog open(TRUE);
CString cstr;
if (open.DoModal() == IDOK)
{
cstr = open.GetFileName();
}
Smgr->loadScene((irr::c8*)(const TCHAR*)cstr);
and also engine log : unable to open scene file :s
i can not load my scene yet .
if it is possible please help me in this case .
The first method you posted won't ever work. The string that is being returned is a temporary, and you are getting a pointer to something that is owned by that temporary. When the temporary goes out of scope, it is destroyed, and your pointer points to garbage.
Theoretically, you should only have to do this...
Given your last post, I'm assuming that you will get an error message about an illegal conversion from TCHAR* or wchar_t* to c8*. If this is the case, then I'm guessing that you are compiling with _UNICODE defined. If the symbol _UNICODE is defined for a build of your program, TCHAR is defined as type wchar_t, otherwise it is just a char.
The easy solution is to remove the definition of _UNICODE. It usually comes from your project settings, but it can come from other places. If you don't want to remove _UNICODE, you need to convert the wchar_t string to a char string. You can do this with core::stringc or with wcstomb.
Theoretically, you should only have to do this...
Code: Select all
CFileDialog open(TRUE);
if (open.DoModal() == IDOK)
{
Smgr->loadScene(open.GetFileName());
}
The easy solution is to remove the definition of _UNICODE. It usually comes from your project settings, but it can come from other places. If you don't want to remove _UNICODE, you need to convert the wchar_t string to a char string. You can do this with core::stringc or with wcstomb.
Code: Select all
CFileDialog open(TRUE);
if (open.DoModal() == IDOK)
{
#ifdef _UNICODE
// use stringc conversion ctor to do cheap conversion
core::stringc filename(open.GetFileName());
Smgr->loadScene(filename.c_str());
#else
Smgr->loadScene(open.GetFileName());
#endif
}
i think UNICODE has defined before , becuase when i defined it , i got warning for redefinition UNICODE.
this is error that compiler gave me after i replace them with your code:
unfortunately i don't know why you can compile it but i can't.
and worst thing is that when i ignore other code and use just this line of code : Smgr->loadScene(open.GetFileName()); ,
i get this error :
thankyou for your attention vitek.
this is error that compiler gave me after i replace them with your code:
Code: Select all
error C2664: 'irr::core::string<T>::string(const irr::core::string<T> &)' : cannot convert parameter 1 from 'CString' to 'const irr::core::string<T> &'
with
[
T=wchar_t
]
Reason: cannot convert from 'CString' to 'const irr::core::string<T>'
with
[
T=wchar_t
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
//error is for this line :core::stringw filename(open.GetFileName());Code: Select all
error C2664: 'bool irr::scene::ISceneManager::loadScene(const irr::c8 *,irr::scene::ISceneUserDataSerializer *)' : cannot convert parameter 1 from 'const wchar_t *' to 'const irr::c8 *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
//error is for this line :Smgr->loadScene(filename.c_str());and worst thing is that when i ignore other code and use just this line of code : Smgr->loadScene(open.GetFileName()); ,
i get this error :
Code: Select all
error C2664: 'bool irr::scene::ISceneManager::loadScene(const irr::c8 *,irr::scene::ISceneUserDataSerializer *)' : cannot convert parameter 1 from 'CString' to 'const irr::c8 *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Like I said, if it is defined, the easiest solution is to remove it.i think UNICODE has defined before , becuase when i defined it , i got warning for redefinition UNICODE.
From the first error message you gave, it looks like you used core::stringw, but you need to use core::stringc. Try this...
Code: Select all
// note that this is a stringc, not a stringw!!!
core::stringc filename(open.GetFileName().GetBuffer());
Smgr->loadScene(filename.c_str());