المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : C# / C++ Interop, Odd behaviour from marshalled strings



C++ Programming
04-14-2009, 06:01 PM
Hello,

I am trying some Interop in my application. This is the first time I have had to pass arguments with function calls. I am experiencing some odd results.



// C# Function Call
RenderEngine.LoadNewMesh("BasicMesh", "Pyramid3", 1);

// Managed C++ Wrapper Function
void RenderEngine::LoadNewMesh(String^ meshType, String^ meshName, int nSubsets)
{
char* cMeshType = (char*)(void*)Marshal::StringToHGlobalAnsi(meshType);
char* cMeshName = (char*)(void*)Marshal::StringToHGlobalAnsi(meshName);

m_engineRoot->LoadMesh.LoadNewMesh(cMeshType, cMeshName, nSubsets);
}

// Native C++ Function
void MeshLoader::LoadNewMesh(char* meshType, char* meshName, UINT nSubsets)
{
//char*
m_meshType = meshType;
//char*
m_meshName = meshName;
m_nSubsets = 0;
m_nTotalSubsets = nSubsets;

m_pMesh = new BasicMesh();

// This message box will display Pyramid3
MessageBox(0, meshName, 0, 0);

// This code will not trigger the message box
if (meshName == "Pyramid3")
MessageBox(0, (char*)"meshName is Pyramid3", 0, 0);

// This code will trigger and m_meshName is set directly as Pyramid3
// if it is not set directly here the call to GetMeshByName later on returns NULL
if (strcmp(meshName,"Pyramid3") == 0)
m_meshName = "Pyramid3";
}

// Object is referenced by name here
MeshManager::CreateMesh(m_pMesh, m_meshName);

// Will return NULL if char* is not set directly
MeshManager::GetMeshByName("Pyramid3");