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());
}