Consider the Following structure
/*Structure defined in unmanaged dll written in cpp*/
struct NSCASR_RCG_RES_ST
{
unsigned int ulSizeBytes;
unsigned int ulWarnings;
unsigned short usNumPhrases;
wchar_t* pstrWaveformURI;
unsigned int ulWaveformSizeBytes;
unsigned int ulWaveformDuration;
};
/*Structure defined in c#*/
struct NSCASR_RCG_RES_ST
{
unsigned int ulSizeBytes;
unsigned int ulWarnings;
unsigned short usNumPhrases;
String pstrWaveformURI;
unsigned int ulWaveformSizeBytes;
unsigned int ulWaveformDuration;
};
In my unmagaed DLL(cpp) I am calling the fucntion by passng the address of the structure as follows :
NSCASR_RCG_RES_ST result_recognize;
ASR_Recognize_ResultsGet(&result_recognize);
In my managed DLL the definition is like
void ASR_Recognize_ResultsGet(NSCASR_RCG_RES_ST *recognize)
{
/*MRCP_MD_TO_ASR is namespace and Constants is class name
which consists of structure NSCASR_RCG_RES_ST */
MRCP_MD_TO_ASR::Constants::NSCASR_RCG_RES_ST *pRecognitionResults;
pRecognitionResults = (MRCP_MD_TO_ASR::Constants::NSCASR_RCG_RES_ST *)recognize;
MRCP_MD_TO_ASR::ASR_API::ASR_Recognize_ResultsGet(*pRecognitionResults);
}
In c# code I am assigning the following members
public static int ASR_Recognize_ResultsGet(ref Constants.NSCASR_RCG_RES_ST pRecognitionResults)
{
pRecognitionResults = speech_results;
pRecognitionResults.ulSizeBytes = 200;
return 0;
}
But when I see the content of result_recognize after execution of statement the value 200 is getting assigned to usNumPhrases variable instead of ulSizeBytes
I got the solution by adding structlayout as explicit and using charset as c# uses unicode 16 , we have to make it unicode 8 if we are using IntPtr concepts