K
K
Kostya Bakay2015-04-10 19:51:01
C++ / C#
Kostya Bakay, 2015-04-10 19:51:01

How to port code from Windows to Ubuntu?

In general, this code runs on Windows, there was a task to make it work on Linux (never used it). I naively believe that you can just copy and paste it, installed Ubuntu and Eclipse, but naturally a lot of errors appeared, the code does not work. So, how to properly port the code to Linux and what should I pay attention to? I must say right away that I am a complete beginner in Linux, so I may not know some banal aspects related to it, and this put me in a stupor.

#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <ShellAPI.h>
#include <process.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <string.h>

unsigned _stdcall PrintInfoToFile(void *fileHandle) {

  HANDLE hdl = *(HANDLE*)fileHandle;

  for (int i = 0; i < 10; i++){
    Sleep(100);
    printf("NewThread : %d ms\n", i * 100);

    DWORD dwProcId;
    DWORD dwThreadId;
    DWORD dwNOBW;
    char buffer[100];

    dwProcId = GetCurrentProcessId();
    dwThreadId = GetCurrentThreadId();

    // Print ProcessID:
    _itoa(dwProcId, buffer, 10);
    WriteFile(hdl, "ProcessID: ", 11, &dwNOBW, NULL);
    WriteFile(hdl, &buffer, strlen(buffer), &dwNOBW, NULL);
    WriteFile(hdl, " ", 1, &dwNOBW, NULL);

    // Print ThreadID:
    _itoa(dwThreadId, buffer, 10);
    WriteFile(hdl, "ThreadID: ", 10, &dwNOBW, NULL);
    WriteFile(hdl, &buffer, strlen(buffer), &dwNOBW, NULL);
    WriteFile(hdl, " ", 1, &dwNOBW, NULL);

    // Print system time:
    struct tm *localtime(const time_t *time);
    time_t theTime;
    time(&theTime);
    tm *t = localtime(&theTime);
    char* timestamp = asctime(t);

    WriteFile(hdl, timestamp, strlen(timestamp), &dwNOBW, NULL);

    WriteFile(hdl, "\r\n", 2, &dwNOBW, NULL);
  }
  // _endthreadex
  _endthreadex(0);
  return 0;
}

void PrintThreadOrProcessPriority(DWORD ThreadPriorityClass)
{
  switch (ThreadPriorityClass)
  {
  case THREAD_PRIORITY_LOWEST:
    printf("Lowest priority\n");
    break;
  case THREAD_PRIORITY_NORMAL:
    printf("normal priority\n");
    break;
  case THREAD_PRIORITY_HIGHEST:
    printf("Highest priority\n");
    break;
  case THREAD_PRIORITY_ERROR_RETURN:
    printf("some error\n");
  default:
    break;
  }
}

int main()
{
  // Part 1: Process system calls

  // ShellExecute
  printf("----------Part 1: Process system calls----------\n");
  printf("Shell Execute: calc.exe\n");
  ShellExecuteA(NULL, NULL, "calc.exe", NULL, NULL, SW_SHOWNORMAL);
  printf("done!\n");

  // CreateProcess
  printf("Create Process: calc.exe\n");
  STARTUPINFOA si;
  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  PROCESS_INFORMATION pi;
  if (CreateProcessA(NULL, "calc.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    printf("Done!\n");

  // OpenProcess
  HANDLE hdl;
  printf("Open Process\n");
  hdl = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId);

  // GetExitCodeProcess
  printf("Get Exit Code Process\n");
  DWORD Exit;
  GetExitCodeProcess(hdl, &Exit);
  printf("Exit Code: %x\n", Exit);
  if (Exit == STILL_ACTIVE)
    printf("process is running\n");
  else
    printf("process isn't running\n");

  // SetPriorityClass
  printf("Set Priority Class: REALTIME\n");
  SetPriorityClass(hdl, REALTIME_PRIORITY_CLASS);

  Sleep(1000);
  // TerminateProcess
  printf("Terminate proccess\n");
  TerminateProcess(hdl, 0);

  // CloseHandle
  printf("Close proccess handle\n");
  CloseHandle(hdl);


  // Part 2: Thread system calls
  printf("----------Part 2: Thread system calls-----------\n");
  unsigned ThreadId;
  HANDLE hFile;
  DWORD dwExitCode;
  hFile = CreateFileA("D:\\spz_lab1.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  HANDLE hThread;

  // _beginthreadex
  Sleep(200);
  printf("Create new thread\n");
  hThread = (HANDLE)_beginthreadex(NULL, 0, &PrintInfoToFile, &hFile, 0, &ThreadId);

  // GetPriorityClass
  Sleep(200);
  printf("Get thread priority class\n");
  DWORD ThreadPriorityClass = GetThreadPriority(hThread);
  PrintThreadOrProcessPriority(ThreadPriorityClass);

  // SetPriorityClass
  Sleep(200);
  printf("Set Priority class\n");
  SetThreadPriority(hThread, THREAD_PRIORITY_LOWEST);
  ThreadPriorityClass = GetThreadPriority(hThread);
  PrintThreadOrProcessPriority(ThreadPriorityClass);

  DWORD Thread_exit_code;

  // Wait before thread finish his work 
  while (true) {
    GetExitCodeThread(hThread, &Thread_exit_code);
    if (Thread_exit_code == STILL_ACTIVE) {	// GetExitCodeThread
      continue;
    }
    else {
      printf("Close Thread handle\n");
      CloseHandle(hThread);	// CloseHandle
      break;
    }
  }
  printf("End\n");
  return EXIT_SUCCESS;
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sitev_ru, 2015-04-10
@kostyabakay

It is necessary to adapt each line under Linux
For example:
Waiting for 100 milliseconds.
We search on the Internet: c ++ linux sleep
We find: www.cyberforum.ru/cpp/thread75979.html The
code turns out:

#ifdef WINDOWS
Sleep(100);
#endif

#ifdef LINUX
usleep(100000);
#endif

Etc...

L
Leonid Fedotov, 2015-04-10
@iLeonidze

If it doesn't matter how, and most importantly that it works, then maybe Wine ?

E
Eddy_Em, 2015-04-10
@Eddy_Em

Forget this pornography and write again.

M
mamkaololosha, 2015-04-10
@mamkaololosha

Either rewrite to C++11/14 or rewrite from 0 to posix threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question