I think I am getting the hang of memory in c++/cli. So this native function:
can be wrapped like this:Code:int WriteToBuffer(HDIB hdib, int nFormat, BYTE* pBuffer, int nbMax);
and memcpy can be used to convert a String^ to a char* like this:Code:int Toolkit::Dib::WriteToBuffer(System::IntPtr hdib, int nFormat, array<Byte>^% pBuffer, int nbMax) { pin_ptr<Byte> p = &pBuffer[0]; return ::WriteToBuffer((HDIB)hdib.ToPointer(), nFormat, (BYTE*)p, nbMax); }
(*Note*)Code:const char * SystemStringToCSTR(System::String^ sz) { char ret [255]; IntPtr hglobal; hglobal = Marshal::StringToHGlobalAnsi(sz); memcpy_s(ret, 255, (char*)hglobal.ToPointer(), strlen((char*)hglobal.ToPointer())); Marshal::FreeHGlobal(hglobal); return ret; }
the above function does not work, because ret is an adress of memory that does not last beyond this function. To surcumvent this for the time being I made "char ret" into "static char ret", however this has an issue. If you were to call this function twice in the same callee, it will overright itself:
one now is overwritten. For my case it (sort of) works because I am only calling it once per function so far. however, this is not very scalable.Code:char * one = SystemStringToCSTR(first); char * two = SystemStringToCSTR(last);




Reply With Quote