//이파일은 외부프로그램에서 included usable. 
#include "stdafx.h"
#include <BatGoRang.h>
#include <stdio.h>
HANDLE m_hMapping = NULL;
LPVOID m_lpvMem = NULL; // pointer to shared memory
HWND hGorangWnd = NULL;
long GoRangCmd(LPCSTR szcmd, LPCSTR szkeyname, LPCSTR szparam)
{
	BATGORANGDATA tData = { 0, };
	if (hGorangWnd == NULL)
		hGorangWnd = ::FindWindow(NULL, BATGORANG_WINDOW);
	else if(!IsWindow(hGorangWnd)){
			hGorangWnd = ::FindWindow(NULL, BATGORANG_WINDOW);
	}
	if (hGorangWnd == NULL)
	{
		DWORD dwerr = GetLastError();
		return 0;
	}
	tData.id = BATGORANG_SYSTEMID;
	strncpy(tData.szCmd, szcmd, sizeof(tData.szCmd));
	strncpy(tData.szKeyName, szkeyname, sizeof(tData.szKeyName));
	strncpy(tData.szParam, szparam, sizeof(tData.szParam));
	SetSHMDataFromExternalProcess(&tData);
	::SendMessage(hGorangWnd, WM_BATGORANG, (WPARAM)BATGORANG_SETCMD, (LPARAM)0L);
	return 1;
}

void GoRangLog(LPCSTR szkeyname, LPCSTR szbuff)
{
	BATGORANGDATA tData = { 0, };
	if (hGorangWnd == NULL)
		hGorangWnd = ::FindWindow(NULL, BATGORANG_WINDOW);
	else if (!IsWindow(hGorangWnd)) {
		hGorangWnd = ::FindWindow(NULL, BATGORANG_WINDOW);
	}
	if (hGorangWnd == NULL)
	{
		DWORD dwerr = GetLastError();
		return;
	}
	strncpy(tData.szKeyName,szkeyname,sizeof(tData.szKeyName));
	strncpy(tData.szParam,szbuff,sizeof(tData.szParam));
    SetSHMDataFromExternalProcess(&tData);
	//Set Shared Memory of NETracer
	//Nofity to NETracer Window
    ::SendMessage(hGorangWnd, WM_BATGORANG, (WPARAM)BATGORANG_SETDATA, (LPARAM)0L);
    //::PostMessage(hWnd, WM_BATGORANG, (WPARAM)BATGORANG_SETDATA, (LPARAM)0L);
}

void SetSHMDataFromExternalProcess(LPBATGORANGDATA lpData)
{
	//HANDLE hMapping;
	//LPVOID lpvMem = NULL; // pointer to shared memory
	LPBATGORANGDATA lpSharedData;
    if (m_hMapping == NULL)
        m_hMapping = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, BATGORANG_MEMFILEMAP);
    if (m_hMapping == NULL)
		return;
    if (m_lpvMem == NULL)
        m_lpvMem = MapViewOfFile(m_hMapping, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
    if (m_lpvMem == NULL)
		return; 

    lpSharedData = (LPBATGORANGDATA)m_lpvMem;
	memcpy(lpSharedData,lpData,sizeof(BATGORANGDATA));
    //BOOL flag = UnmapViewOfFile(m_lpvMem);
    //CloseHandle(m_hMapping);
}