none
how to serialize a dictionary class object and store its structure in INI file? RRS feed

  • Question

  • I'm looking for some c++/cli examples on how to serialize a dictionary class object and store its structure in an INI file.

     

    Tuesday, October 30, 2012 3:27 PM

Answers

  • ok. you're telling me to serialize the dictionary object into a String object?

    Sorry, I'm really novice to serialization. Could you please point me to some guidelines?

    Just loop through the dictionary (which is IEnumerable<KeyValuePair<TKey, TValue>>), and call ToString() on each Key and each Value, and write to the ini file however you chose.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Wednesday, October 31, 2012 1:12 PM
    • Marked as answer by dave_MA Friday, November 2, 2012 8:13 AM
    Tuesday, October 30, 2012 4:24 PM
    Moderator
  • Hello dave_MA,

    Try this out as startup code :

    #include "stdafx.h"
    #include <msclr\marshal.h>
    
    using namespace System;
    using namespace System::Collections::Specialized;
    using namespace System::Collections;
    using namespace msclr::interop;
    
    void DoTest()
    {
    	// Creates and initializes a new StringDictionary.
    	StringDictionary^ myCol = gcnew StringDictionary;
    	myCol->Add( "KEY1", "VALUE1" );
    	myCol->Add( "KEY2", "VALUE2" );
    	myCol->Add( "KEY3", "VALUE3" );
    	
    	IEnumerator^ enumerator = myCol -> GetEnumerator();
    	
    	enumerator -> Reset();
    	
    	while(enumerator -> MoveNext())
    	{
    		DictionaryEntry de = *dynamic_cast<DictionaryEntry^>(enumerator -> Current);
    		
    		String^ strKey = dynamic_cast<String^>(de.Key);
    		String^ strValue = dynamic_cast<String^>(de.Value);
    		
    		marshal_context context;
    		
    		LPCTSTR lpszKey = context.marshal_as<const TCHAR*>(strKey);
    		LPCTSTR lpszValue = context.marshal_as<const TCHAR*>(strValue);
    		
    		WritePrivateProfileString(L"TEST", lpszKey, lpszValue, L"c:\\TEST.INI");
    	}   
    }
    
    int main(array<System::String ^> ^args)
    {
        DoTest();
        
        return 0;
    }
    

    Best of luck,

    Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by dave_MA Friday, November 2, 2012 8:13 AM
    Friday, November 2, 2012 5:27 AM

All replies

  • There aren't any managed (.NET) classes in the framework for using .INI files - they're really kind of considered obsolete.

    If you want to do this, you could use the native WritePrivateProfileString to write out each value you wish, though (since you're in C++/CLI).  Just marshal the strings to an LPCTSTR and pass them into the function.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, October 30, 2012 4:03 PM
    Moderator
  • ok. you're telling me to serialize the dictionary object into a String object?

    Sorry, I'm really novice to serialization. Could you please point me to some guidelines?

    Tuesday, October 30, 2012 4:22 PM
  • ok. you're telling me to serialize the dictionary object into a String object?

    Sorry, I'm really novice to serialization. Could you please point me to some guidelines?

    Just loop through the dictionary (which is IEnumerable<KeyValuePair<TKey, TValue>>), and call ToString() on each Key and each Value, and write to the ini file however you chose.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Wednesday, October 31, 2012 1:12 PM
    • Marked as answer by dave_MA Friday, November 2, 2012 8:13 AM
    Tuesday, October 30, 2012 4:24 PM
    Moderator
  • Hello dave_MA,

    Try this out as startup code :

    #include "stdafx.h"
    #include <msclr\marshal.h>
    
    using namespace System;
    using namespace System::Collections::Specialized;
    using namespace System::Collections;
    using namespace msclr::interop;
    
    void DoTest()
    {
    	// Creates and initializes a new StringDictionary.
    	StringDictionary^ myCol = gcnew StringDictionary;
    	myCol->Add( "KEY1", "VALUE1" );
    	myCol->Add( "KEY2", "VALUE2" );
    	myCol->Add( "KEY3", "VALUE3" );
    	
    	IEnumerator^ enumerator = myCol -> GetEnumerator();
    	
    	enumerator -> Reset();
    	
    	while(enumerator -> MoveNext())
    	{
    		DictionaryEntry de = *dynamic_cast<DictionaryEntry^>(enumerator -> Current);
    		
    		String^ strKey = dynamic_cast<String^>(de.Key);
    		String^ strValue = dynamic_cast<String^>(de.Value);
    		
    		marshal_context context;
    		
    		LPCTSTR lpszKey = context.marshal_as<const TCHAR*>(strKey);
    		LPCTSTR lpszValue = context.marshal_as<const TCHAR*>(strValue);
    		
    		WritePrivateProfileString(L"TEST", lpszKey, lpszValue, L"c:\\TEST.INI");
    	}   
    }
    
    int main(array<System::String ^> ^args)
    {
        DoTest();
        
        return 0;
    }
    

    Best of luck,

    Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Marked as answer by dave_MA Friday, November 2, 2012 8:13 AM
    Friday, November 2, 2012 5:27 AM