If you are using Visual C++ to create your application, you should use Visual C++ Windows Form uses .Net Framework 2.0 to create your graphics user interface (GUI). Exchanging data between a Visual C++ .Net Framework 2.0 application and WiT Engine DLL requires the transfer of data between managed and unmanaged memory space. WiT Engine DLL only uses unmanaged memory space. But GUI controls such as text message windows use managed memory space. Transferring memory between managed and unmanaged spce can be accomplished by using the Marshal class static methods.
To use Marshal class, you need to add this line into Form1.h:
using namespace System::Runtime::InteropServices;
If there is unmanaged code in your application, you need to set the project's General property: Common Language Runtime Support as /clr, not /clr:pure as default.
The following sample code demonstrates how you can transfer a managed string to an unmanaged string, then pass it into WiT Engine DLL. Notice that the memory allocated by Marshal::AllocHGlobal must be explicitly released by Marshal::FreeHGlobal.
String^ pStr = "demoPrograms\\engine\\wic\\simple.wic"; char* pChars = (char*)Marshal::StringToHGlobalAnsi(pStr).ToPointer(); witLoad(pChars); Marshal::FreeHGlobal((IntPtr)pChars);
The following sample code demonstrates how you can transfer an unmanaged string to a managed string.
#pragma managed
void myStatCallbackM(const int level, const char *bufRaw)
{
String ^buf = gcnew String(bufRaw);
String ^addStr = buf->Replace("\n", "\r\n");
Globals::myForm->statsWin->Text += addStr;
Globals::myForm->statsWin->SelectionStart =
Globals::myForm->statsWin->Text->Length;
Globals::myForm->statsWin->ScrollToCaret();
}
#pragma unmanaged
const void myStatusCallback(const int level, const char *buf)
{
myStatCallbackM(level, buf);
}
|