CS Outlook DataSet class

#region Outlook DataSet
private class OutlookDataSet
{
	#region DataSet Table Names
	private const string TableAppointment = "Appointment";
	private const string TableContact = "Contact";
	private const string TableMail = "Inbox";
	private const string TableTasks = "Tasks";
	private const string TableNotes = "Notes";
	#endregion

	#region Private Variables
	private Outlook.Application outLook = null;
	private Outlook.NameSpace nameSpace = null;
	private Outlook.MAPIFolder mapiFolder = null;
	private DataSet dataSet;
    #endregion

    public OutlookDataSet(Outlook.MAPIFolder currentFolder)
    {
        if (currentFolder == null)
        {
            outLook = new Outlook.Application();
            nameSpace = outLook.GetNamespace("MAPI");
        }
        else
        {
            mapiFolder = currentFolder;
        }

		initialize();
	}

	#region Properties
	protected internal DataTable this[string tableName] { get { return dataSet.Tables[tableName]; } }
	protected internal DataTable Appointment { get { return this[TableAppointment]; } }
	protected internal DataTable Contact { get { return this[TableContact]; } }
	protected internal DataTable Mails { get { return this[TableMail]; } }
	protected internal DataTable Tasks { get { return this[TableTasks]; } }
	protected internal DataTable Notes { get { return this[TableNotes]; } }
	#endregion

	private void initialize()
	{
		dataSet = new DataSet();

		#region Appointment
		dataSet.Tables.Add(TableAppointment);
		Appointment.Columns.Add("Subject");
		Appointment.Columns.Add("Location");
		Appointment.Columns.Add("Start");
		Appointment.Columns.Add("End");
		Appointment.Columns.Add("AllDayEvent");
		Appointment.Columns.Add("Duration");
		Appointment.Columns.Add("Organizer");
		Appointment.Columns.Add("Importance");
		Appointment.Columns.Add("Sensitivity");
		Appointment.Columns.Add("Body");
		#endregion

		#region Contact
		dataSet.Tables.Add(TableContact);
		Contact.Columns.Add("FirstName");
		Contact.Columns.Add("LastName");
		Contact.Columns.Add("CompanyName");
		Contact.Columns.Add("Email");
		Contact.Columns.Add("HomePhone");
		Contact.Columns.Add("WorkPhone");
		#endregion

		#region Mail
		dataSet.Tables.Add(TableMail);
		Mails.Columns.Add("From");
		Mails.Columns.Add("To");
		Mails.Columns.Add("Cc");
		Mails.Columns.Add("Subject");
		Mails.Columns.Add("Received");
		Mails.Columns.Add("Message");
		#endregion

		#region Tasks
		dataSet.Tables.Add(TableTasks);
		Tasks.Columns.Add("Subject");
		Tasks.Columns.Add("Categories");
		Tasks.Columns.Add("CreationTime");
		Tasks.Columns.Add("LastModificationTime");
		Tasks.Columns.Add("Contents");
		#endregion

		#region Notes
		dataSet.Tables.Add(TableNotes);
		Notes.Columns.Add("Subject");
		Notes.Columns.Add("Categories");
		Notes.Columns.Add("CreationTime");
		Notes.Columns.Add("LastModificationTime");
		Notes.Columns.Add("Contents");
		#endregion
	}

	public int GetFolderCount(Outlook.OlDefaultFolders folder)
	{
        if (mapiFolder == null)
		    mapiFolder = nameSpace.GetDefaultFolder(folder);
		return mapiFolder.Items.Count;
	}

	public void fillAppointments()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.AppointmentItem _item = item as Outlook.AppointmentItem;
				Appointment.Rows.Add(new object[]
				{
					_item.Subject,
					_item.Location,
					_item.Start,
					_item.End,
					_item.AllDayEvent,
					_item.Duration,
					_item.Organizer,
					_item.Importance,
					_item.Sensitivity,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillContacts()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.ContactItem _item = item as Outlook.ContactItem;
				Contact.Rows.Add(new object[]
				{
					_item.FirstName,
					_item.LastName,
					_item.CompanyName,
					_item.Email1Address,
					_item.HomeTelephoneNumber,
					_item.BusinessTelephoneNumber
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillInbox(string filter = null)
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Mails.Clear();
            if (filter != null)
            {
                object folderItem = mapiFolder.Items.Find(filter);
                while (folderItem != null)
                {
                    Outlook.MailItem mailItem = folderItem as Outlook.MailItem;
                    if (mailItem != null)
                    {
                        MessageBox.Show(mailItem.Subject);
                    }
                    folderItem = mapiFolder.Items.FindNext();
                }
            }
            else
            {
                foreach (var item in mapiFolder.Items)
                {
                    if (item is Outlook.MailItem)
                    {
                        Outlook.MailItem _item = item as Outlook.MailItem;
                        Mails.Rows.Add(new object[]
                        {
                            (String.IsNullOrEmpty(_item.SenderName) ? _item.SenderEmailAddress : _item.SenderName),
                            _item.To,
                            _item.CC,
                            _item.Subject,
                            _item.ReceivedTime,
                            _item.Body
                        });
                    }
                }
            }
		}
		catch (System.Exception ex)
		{
            MessageBox.Show(ex.Message);
			throw ex;
		}
	}

	public void fillTasks()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.TaskItem _item = item as Outlook.TaskItem;
				Tasks.Rows.Add(new object[]
				{
					_item.Subject,
					_item.StartDate,
					_item.DueDate,
					_item.Status,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillNotes()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderNotes);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.NoteItem _item = item as Outlook.NoteItem;
				Notes.Rows.Add(new object[]
				{
					_item.Subject,
					_item.Categories,
					_item.CreationTime,
					_item.LastModificationTime,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}
}
#endregion

C# with C++ DLL 포인터 처리

C# dll import 구문

[DllImport("darkmailcore.dll", CharSet = CharSet.Unicode)]
public static extern int readSQLScript(string fileName);
 
[DllImport("darkmailcore.dll")]
public static extern IntPtr databaseSchema(int index);

사용

string connectionString = string.Format("Data Source={0};Version=3", databaseFile);
if (System.IO.File.Exists(databaseFile) == false)
{
	SQLiteConnection.CreateFile(databaseFile);
	Console.WriteLine("{0} File created", databaseFile);
}
 
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
	connection.Open();
 
	for (int index = 0; index < count; index++)
	{
		string sql = Marshal.PtrToStringUni(databaseSchema(index));
		Console.Write(sql);
		SQLiteCommand command = new SQLiteCommand(sql, connection);
		command.ExecuteNonQuery();
		Console.WriteLine(" Created..");
	}
}

C++ 함수들

DARKMAILCORE_API int readSQLScript(BSTR fileName)
{
	USES_CONVERSION;
	DD(true, "asdfadfasdf");
	CComBSTR _fileName = fileName;
	TCHAR buffer[1024];
	GetCurrentDirectory(sizeof(buffer), buffer);
	std::string scriptFile = darkmatter::core::utility::string::format("%s\\%s", T2CA(buffer), OLE2CA(_fileName));
	DD(true, "scriptFile: %s", scriptFile.c_str());
 
	std::string statement;
	std::vector<int> controlOpStack;
 
	FILE* file = fopen(scriptFile.c_str(), "r");
	if (file != nullptr)
	{
		while (!feof(file))
		{
			std::string line = readLine(file);
			if (line.length() <= 0 || darkmatter::core::utility::string::startWith(line, "--"))
				continue;
 
			if (darkmatter::core::utility::string::startWith(line, "BEGIN"))
			{
				controlOpStack.push_back(1);
			}
			else if (darkmatter::core::utility::string::endWith(line, "END;"))
			{
				int num = controlOpStack[0];
				controlOpStack.erase(controlOpStack.begin());
				if (num != 1)
				{
					DD(true, "Malformed schema: END without BEGIN");
					return -1;
				}
			}
 
 
			if (controlOpStack.size() <= 0 && darkmatter::core::utility::string::endWith(line, ";"))
			{
				line = line.substr(0, line.length() - 1);
				statement += line;
 
 
				sqlStatement.push_back(statement);
 
				statement.clear();
			}
			else
			{
				statement += line;
				statement += " ";
			}
		}
 
		fclose(file);
	}
	else
	{
		DD(true, "%s file open failed: %d", scriptFile.c_str(), GetLastError());
	}
 
	return sqlStatement.size();
}
 
DARKMAILCORE_API BSTR* databaseSchema(int index)
{
	USES_CONVERSION;
	return (BSTR*)A2BSTR(sqlStatement[index].c_str());
}