// ImportRegularnejDLLDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ImportRegularnejDLL.h"
#include "ImportRegularnejDLLDlg.h"
#include "Import.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CImportRegularnejDLLDlg dialog




CImportRegularnejDLLDlg::CImportRegularnejDLLDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CImportRegularnejDLLDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CImportRegularnejDLLDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, editA);
	DDX_Control(pDX, IDC_EDIT2, editB);
	DDX_Control(pDX, IDC_EDIT3, editWynik1);
	DDX_Control(pDX, IDC_EDIT4, editWynik2);
}

BEGIN_MESSAGE_MAP(CImportRegularnejDLLDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CImportRegularnejDLLDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CImportRegularnejDLLDlg message handlers

BOOL CImportRegularnejDLLDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CImportRegularnejDLLDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImportRegularnejDLLDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CImportRegularnejDLLDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CString a,b,wynik;

	editA.GetWindowTextW(a);
	editB.GetWindowTextW(b);

	int _a = _wtoi(a);
    int _b = _wtoi(b);

	int _wynik = Odejmij(_a,_b);

	wynik.AppendFormat(L"%d",_wynik);
	editWynik1.SetWindowTextW(wynik);

	CString wynik2;

	typedef int (*pFun)(int,int);

	HINSTANCE dllHandle = LoadLibrary(L"FunkcjaWDLL.dll");

	if(dllHandle == NULL)
	{
		MessageBox(L"Nie udało się załadować biblioteki DLL");
		return ;
	}

	pFun pOdejmij  = (pFun)GetProcAddress(dllHandle,(LPCSTR)"Odejmij");

	if(pOdejmij != NULL)
	{
		int _wynik2 = pOdejmij(_a,_b);
		wynik2.AppendFormat(L"%d",_wynik2);
		editWynik2.SetWindowTextW(wynik2);
	}

	if(!FreeLibrary(dllHandle)) MessageBox(L"Nie udało się usunąć biblioteki DLL");
}
