My first approach was...,
C#: string -> char* => C++: char* -> tchar*
Since the methods in SDK takes tchar*, I have to find the way to convert into txhar*....
Following is the way I did:
---------------------------------------------------------------------------------------
C++:
bool Init(System::IntPtr fpathIntPtr, int fpathLength){
char * fpathCharPtr = (char *)fpathIntPtr.ToPointer();
//convert unsigned char * to tchar *
//Get the size of the string by setting 4th parameter to -1
int len = MultiByteToWideChar(CP_ACP, 0, fpathCharPtr, -1, NULL, 0);
//allocate space fpr wide char string
wchar_t* fpath = new wchar_t[len];
if(!fpath)
{
delete []fpath;
}
//convert
MultiByteToWideChar (CP_ACP, 0, fpathCharPtr, -1, fpath, len );
//load init motion and play the motion
if(VSRC003_LoadMotion(fpath)){ System::Console::WriteLine("Failed to load the init motion."); return false;}
if(VSRC003_PlayMotion(0)){ System::Console::WriteLine("Failed to play the init motion.\n\n"); return false;}
else System::Console::WriteLine("Starting init motion.\n\n");
//release memory for fpath
delete []fpath;
-----------------------------------------------------------------------------------
The conversion part worked fine....
Now I was trying to find the way to convert string to char* in C#.
....
....
......
I tried everything I could find online and nothing worked...
by the way..., I spent three days...
Then, I decide to do it in different way!
C#: string -> IntPtr => C++: IntPtr -> char* -> tchar*
Wow!
Do not try converting IntPtr -> char*...
conversion from IntPtr returns unicode, not ascii!!!
Therefore..., try IntPtr -> tchar* !!!
---------------------------------------------------------------------------------
C#:
static void Main(string[] args)
{
Vsrc003Wrapper vsrc003Wrapper = new Vsrc003Wrapper();
string fpath = "init.txt";
IntPtr fpathIntPtr = Marshal.StringToHGlobalAuto(fpath);
vsrc003Wrapper.Init(fpathIntPtr, fpath.Length);
Marshal.FreeHGlobal(fpathIntPtr);
C++:
bool Init(System::IntPtr fpathIntPtr, int fpathLength){
wchar_t * fpath = reinterpret_cast<wchar_t*>(fpathIntPtr.ToPointer());
//load init motion and play the motion
if(VSRC003_LoadMotion(fpath)){ System::Console::WriteLine("Failed to load the init motion."); return false;}
No comments:
Post a Comment