var hMainWnd=AkelPad.GetMainWnd();
if (!hMainWnd) WScript.Quit();

if (AkelPad.SendMessage(AkelPad.GetEditWnd(), 3125 /*AEM_GETSEL*/, 0, 0))
{
  var oSys=AkelPad.SystemFunction();
  
  //NSIS
  var NSIS_MAX_STRLEN=1024;
  //var nNsisUnicode=0 /*DT_ANSI*/;
  var nNsisUnicode=1 /*DT_UNICODE*/;
  var lpStack=0;

  var pMD5 =GetMD5String(AkelPad.GetSelText(4 - AkelPad.GetEditNewLine(0)));
  WScript.Echo(pMD5);
}


//Functions
function GetMD5String(lpString)
{
  pushstring(lpString);
  nsiscall(".\\AkelFiles\\Tools\\Md5dll\\md5dll.dll::GetMD5String");
  return popstring();
}

function nsiscall(pFunction)
{
  var lppStack;

  if (lppStack=AkelPad.MemAlloc(4 /*DWORD*/))
  {
    AkelPad.MemCopy(lppStack, lpStack, 3 /*DT_DWORD*/);
    oSys.Call(pFunction, hMainWnd, NSIS_MAX_STRLEN, 0, lppStack);
    lpStack=AkelPad.MemRead(lppStack, 3 /*DT_DWORD*/);
    AkelPad.MemFree(lppStack);
  }
}

function pushstring(pString)
{
  var lpTop;

  if (lpTop=oSys.Call("kernel32::GlobalAlloc", 0x40 /*GPTR*/, (nNsisUnicode?NSIS_MAX_STRLEN * 2:NSIS_MAX_STRLEN) + 8 /*sizeof(stack_t)*/))
  {
    AkelPad.MemCopy(lpTop + 4 /*stack_t.text*/, pString.substr(0, NSIS_MAX_STRLEN), nNsisUnicode);
    AkelPad.MemCopy(lpTop + 0 /*stack_t.next*/, lpStack, 3 /*DT_DWORD*/);
    lpStack=lpTop;
  }
}

function popstring(pString)
{
  var lpTop;
  var pString="";

  if (lpStack)
  {
    lpTop=lpStack;
    pString=AkelPad.MemRead(lpTop + 4 /*stack_t.text*/, nNsisUnicode);
    lpStack=AkelPad.MemRead(lpTop + 0 /*stack_t.next*/, 3 /*DT_DWORD*/);
    oSys.Call("kernel32::GlobalFree", lpTop);
  }
  return pString;
}