Search code examples
c#managedunmanagedexportsuser-defined-data-types

Assigning values to the address user defined structure in c# called from unmanaged cpp


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


Solution

  • 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

     [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
        public struct NSCASR_RCG_PHRASE_ST
        {
            [FieldOffset(0)]
            public ushort usConfidence;
            [FieldOffset(2)]
            public ushort usNumItems;
            [FieldOffset(4)]
            public short sGrammarIndex;
            [FieldOffset(6)]
            public short sGrammarType;
        }
    
    
        [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
        public unsafe struct NSCASR_RCG_RES_ST
        {
    
            [FieldOffset(0)]
            public uint ulSizeBytes;
            [FieldOffset(4)]
            public uint ulWarnings;
            [FieldOffset(8)]
            public ushort usNumPhrases;
            [FieldOffset(12)]
            public IntPtr pstrWaveformURI;
            [FieldOffset(16)]
            public uint ulWaveformSizeBytes;
            [FieldOffset(20)]
            public uint ulWaveformDuration;
        }