// MessagesDLL.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include "MessagesDLL.h" #include #include #include #ifdef _DEBUG #define new DEBUG_NEW #endif using namespace std; CWinApp theApp; BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { //--- switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } //--- return(TRUE); } MT4_EXPFUNC int __stdcall addMessage(string contentStr, string dateStr, string seenStr) { int nRetCode = 0; HMODULE hModule = ::GetModuleHandle(nullptr); if (hModule != nullptr) { // initialize MFC and print and error on failure if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs wprintf(L"Fatal Error: MFC initialization failed\n"); nRetCode = 1; } else { // TODO: code your application's behavior here. } } else { // TODO: change error code to suit your needs wprintf(L"Fatal Error: GetModuleHandle failed\n"); nRetCode = 1; } // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { _tprintf(_T("Fatal Error: MFC initialization failed\n")); return 1; } CDatabase db; LPCTSTR connectString = _T("Driver={SQL Server};") _T("Server=SQL6001.SmarterASP.NET;Database=DB_A1F32C_MessageDB;Uid=DB_A1F32C_MessageDB_admin;Pwd=messageDB123;"); if (!db.OpenEx(connectString)) { AfxMessageBox(_T("Could not open database")); return 2; } /* AfxMessageBox(_T("Connected to database")); char* content = "kiskutya"; char* date = "2017-05-24 15:09:17.597"; char* seen = "false"; string contentStr(content); string seenStr(seen); string dateStr(date); */ string separator("','"); string query("INSERT INTO DBO.Messages(content, createddate, seen) VALUES('"); (((((query += contentStr) += separator) += dateStr) += separator) += seenStr) += "')"; const char* c = query.c_str(); wchar_t *wq = new wchar_t[strlen(c) + 1]; size_t size = (strlen(c) + 1); size_t convertedChars; mbstowcs_s(&convertedChars, wq, size, c, size - 1); TRY { db.ExecuteSQL(wq); } CATCH_ALL(e) { e->ReportError(); // if something goes wrong, we'll see what is. } END_CATCH_ALL return nRetCode; }