Marshalling a Linked List
Apologies for duplicate posting.

Hi

I am having trouble marshalling a linked list from a DLL.


------C++ Structure and Function-------- 

    struct localeInfo { 
       WCHAR countryName[BUFFER_SIZE];
       WCHAR localeName[BUFFER_SIZE]; 
       localeInfo *next; 
    }
    
    int GetSystemLocales(localeInfo **ppList);

-----------C# Declarations----------- 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
    private struct LocaleInfo { 
       public string countryName; 
       public string localeName; 
       public IntPtr next; 
    };
    
    [DllImport("systemLocales.dll")]
    private static extern int GetSystemLocales(ref IntPtr ppList);
    
    
    int main() 
    { 
       var pListHead = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); 
       try 
       { 
           GetSystemLocales(ref pListHead); 
           var deref1(IntPtr)Marshal.PtrToStructure(pListHead, typeof(IntPtr)); 
           var deref2 = (LocaleInfo)Marshal.PtrToStructure(deref1, typeof(LocaleInfo)); 
       } 
       finally 
       { 
          Marshal.FreeHGlobal(pListHead); 
       }
    
    }


I get an FatalExecutionEngine Exception at deref2 declaration. I can't figure out how to get the linked list back and access its contents.

Here is the C++ code that I wrote to get the linked list. I want something similar to work in C#. 


    localeInfo *pHead = NULL; 
    localeInfo *pTemp; 
    GetSystemLocales(&pHead);
    
    for(pTemp = pHead; pTemp!=NULL; pTemp = pTemp->next)
    {
            wprintf(L"Display Name : %s (%s) \n", pTemp->countryName, pTemp->localeName);
    }
Marshalling a Linked List
===

Apologies for duplicate posting.

Hi

I am having trouble marshalling a linked list from a DLL.


------C++ Structure and Function-------- 

    struct localeInfo { 
       WCHAR countryName[BUFFER_SIZE];
       WCHAR localeName[BUFFER_SIZE]; 
       localeInfo *next; 
    }
    
    int GetSystemLocales(localeInfo **ppList);

-----------C# Declarations----------- 

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
    private struct LocaleInfo { 
       public string countryName; 
       public string localeName; 
       public IntPtr next; 
    };
    
    [DllImport("systemLocales.dll")]
    private static extern int GetSystemLocales(ref IntPtr ppList);
    
    
    int main() 
    { 
       var pListHead = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); 
       try 
       { 
           GetSystemLocales(ref pListHead); 
           var deref1(IntPtr)Marshal.PtrToStructure(pListHead, typeof(IntPtr)); 
           var deref2 = (LocaleInfo)Marshal.PtrToStructure(deref1, typeof(LocaleInfo)); 
       } 
       finally 
       { 
          Marshal.FreeHGlobal(pListHead); 
       }
    
    }


I get an FatalExecutionEngine Exception at deref2 declaration. I can't figure out how to get the linked list back and access its contents.

Here is the C++ code that I wrote to get the linked list. I want something similar to work in C#. 


    localeInfo *pHead = NULL; 
    localeInfo *pTemp; 
    GetSystemLocales(&pHead);
    
    for(pTemp = pHead; pTemp!=NULL; pTemp = pTemp->next)
    {
            wprintf(L"Display Name : %s (%s) \n", pTemp->countryName, pTemp->localeName);
    }