summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2022-08-24 22:44:05 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2022-08-24 22:44:05 +0200
commita2b83596438eac9353acf254ca91842697f6001a (patch)
tree6ac075ac6f6d30b5e30a62673b7d5968df91a5e9
parentc8e7d8922310108f2d46189a8f48abcb68e72534 (diff)
WIP: Movewin32
-rw-r--r--bootstrap.bat13
-rwxr-xr-xclbootstrap.sh25
-rw-r--r--src/execute.cc99
-rw-r--r--src/libctor.h8
4 files changed, 106 insertions, 39 deletions
diff --git a/bootstrap.bat b/bootstrap.bat
index 2a8820a..cb56de1 100644
--- a/bootstrap.bat
+++ b/bootstrap.bat
@@ -28,17 +28,6 @@ set CC=cl.exe
set AR=lib.exe
set LD=link.exe
cl /nologo /std:c++20 /D_X86_ /EHsc /Isrc src/bootstrap.cc /link /SUBSYSTEM:CONSOLE /out:ctor.exe
-
-set /p DUMMY=Hit ENTER to continue...
-
ctor.exe
-
-set /p DUMMY=Hit ENTER to continue...
-
cl /nologo /std:c++20 /D_X86_ /EHsc /Isrc ctor.cc build/libctor.lib /link /SUBSYSTEM:CONSOLE /out:ctor.exe
-
-set /p DUMMY=Hit ENTER to continue...
-
-ctor.exe configure --name ctor.exe --ctor-includedir=src --ctor-libdir=build --cxx=cl.exe --cc=cl.exe --ar=lib.exe --ld=link.exe
-
-set /p DUMMY=!!!!!!!!!!!!!!!!!Hit ENTER to continue...
+ctor.exe configure --ctor-includedir=src --ctor-libdir=build --cxx=cl.exe --cc=cl.exe --ar=lib.exe --ld=link.exe
diff --git a/clbootstrap.sh b/clbootstrap.sh
index e3a420f..75414ab 100755
--- a/clbootstrap.sh
+++ b/clbootstrap.sh
@@ -1,6 +1,6 @@
#!/bin/bash
set -e
-set -x
+#set -x
export WINEDEBUG=-all
rm -f ~/.wine/drive_c/number
@@ -32,19 +32,16 @@ export CXX="cl.exe"
export CC="cl.exe"
export AR="lib.exe"
export LD="link.exe"
-wine cl /nologo /std:c++20 /D_X86_ /EHsc /Isrc src/bootstrap.cc /link /SUBSYSTEM:CONSOLE /out:ctor.exe
-
-#echo -e "\n\n\nHit ENTER to continue...\n\n\n"; read
+wine cl /nologo /std:c++20 /D_X86_ /EHsc /Isrc src/bootstrap.cc /link /SUBSYSTEM:CONSOLE /out:ctor.exe
+#sleep 1
wine ctor.exe
-
-#echo -e "\n\n\nHit ENTER to continue...\n\n\n"; read
-
+#sleep 1
wine cl /nologo /std:c++20 /D_X86_ /EHsc /Isrc ctor.cc build/libctor.lib /link /SUBSYSTEM:CONSOLE /out:ctor.exe
-
-#echo -e "\n\n\nHit ENTER to continue...\n\n\n"; read
-
-wine ctor.exe configure --ctor-includedir=src --ctor-libdir=build --cxx=cl.exe --cc=cl.exe --ar=lib.exe --ld=link.exe
-
-#echo -e "\n\n\nHit ENTER to continue...\n\n\n"; read
-
+#sleep 1
+wine ctor.exe configure --ctor-includedir=src --ctor-libdir=build
+#sleep 1
+wine ctor.exe -a test/ctor.cc
+#sleep 1
+wine ctor.exe check
+#sleep 1
diff --git a/src/execute.cc b/src/execute.cc
index 85c1da1..2111f49 100644
--- a/src/execute.cc
+++ b/src/execute.cc
@@ -13,6 +13,15 @@
#include <processthreadsapi.h>
#include <synchapi.h>
#include <processenv.h>
+#include <handleapi.h>
+#include <libloaderapi.h>
+
+//#include <winbase.h>
+#define INHERIT_PARENT_AFFINITY 0x00010000
+#define DETACHED_PROCESS 0x00000008
+
+#define WINDOWS_LEAN_AND_MEAN
+#include <windows.h>
#endif
#include <iostream>
@@ -163,23 +172,95 @@ int execute(const std::string& command,
env_str += '\0';
}
env_str += '\0';
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si,sizeof(si));
- si.cb=sizeof(si);
- ZeroMemory(&pi,sizeof(pi));
+
+ STARTUPINFO si{};
+// si.hStdInput = GetStdHandle(((DWORD)-10)/*STD_INPUT_HANDLE*/);
+// si.hStdOutput = GetStdHandle(((DWORD)-11)/*STD_OUTPUT_HANDLE*/);
+// si.hStdError = GetStdHandle(((DWORD)-12)/*STD_ERROR_HANDLE*/);
+// si.dwFlags = /*STARTF_USESTDHANDLES*/0x00000100;
+
+ PROCESS_INFORMATION pi{};
+
+ si.cb = sizeof(si);
+
// TODO: Use SetDllDirectory(...) to set DLL search directory
- if(!CreateProcess(nullptr, //"C:/WINDOWS/notepad.exe",
- //"notepad.exe c:/readme.txt",0,0,0,0,0,0,&si,&pi))
- (char*)cmd.data(),0,0,0,0,env_str.data(),0,&si,&pi))
+
+ if(!CreateProcess(nullptr, // lpApplicationName
+ (char*)cmd.data(), // lpCommandLine
+ nullptr, // lpProcessAttributes
+ nullptr, // lpThreadAttributes
+ TRUE, // bInheritHandles
+ INHERIT_PARENT_AFFINITY |
+ //DETACHED_PROCESS |
+ ///*DEBUG_PROCESS*/ 0x00000001 |
+ ///*CREATE_NO_WINDOW*/ 0x08000000 |
+ ///*CREATE_BREAKAWAY_FROM_JOB*/ 0x01000000 |
+ /*CREATE_NEW_PROCESS_GROUP*/ 0x00000200 |
+ 0, // dwCreationFlags
+ env_str.data(), // lpEnvironment
+ nullptr, // lpCurrentDirectory
+ &si, // lpStartupInfo
+ &pi)) // lpProcessInformation
{
return 1; // Could not start process;
}
if(terminate)
{
- ExitProcess(0);
+ char target[MAX_PATH+1];
+ char tmpdir[MAX_PATH+1];
+ int cnt{0};
+
+ // The returned string ends with a backslash
+ if(GetTempPathA(sizeof(tmpdir), tmpdir) == 0)
+ {
+ std::cerr << "Could not read TMP folder\n";
+ return GetLastError();
+ }
+
+ char source[MAX_PATH];
+ HMODULE module = GetModuleHandle(0);
+ GetModuleFileNameA(module, source, MAX_PATH);
+
+ while(true)
+ {
+ if(cnt > 10) // If we need to try more than 10 times something is wrong
+ {
+ return 1;
+ }
+
+ sprintf(target, "%s.tmp-%d", source, cnt);
+ if(MoveFileA(source, target))
+ {
+ break; // success
+ }
+
+ auto err = GetLastError();
+ if(err == ERROR_ALREADY_EXISTS)
+ {
+ if(DeleteFileA(target))
+ {
+ continue; // Try again
+ }
+
+ auto err = GetLastError();
+ if(err != ERROR_ACCESS_DENIED)
+ {
+ std::cerr << "Could not delete file\n";
+ return err;
+ }
+
+ cnt++;
+ continue; // Increment and try again
+ }
+ else
+ {
+ std::cerr << "Could not move file\n";
+ return err;
+ }
+ }
}
+
// Now 'pi.hProcess' contains the process HANDLE, which you can use to
// wait for it like this:
const DWORD infinite = 0xFFFFFFFF;
diff --git a/src/libctor.h b/src/libctor.h
index e4ea7e7..c91777e 100644
--- a/src/libctor.h
+++ b/src/libctor.h
@@ -116,10 +116,10 @@ int reg(ExternalConfigurations (*cb)(const Settings&),
const std::source_location location = std::source_location::current());
// Convenience macro - ugly but keeps things simple(r)
-#define CONCAT(a, b) CONCAT_INNER(a, b)
-#define CONCAT_INNER(a, b) a ## b
-#define UNIQUE_NAME(base) CONCAT(base, __LINE__)
-#define REG(cb) namespace { int UNIQUE_NAME(unique) = reg(cb); }
+#define CTOR_CONCAT(a, b) CTOR_CONCAT_INNER(a, b)
+#define CTOR_CONCAT_INNER(a, b) a ## b
+#define CTOR_UNIQUE_NAME(base) CTOR_CONCAT(base, __LINE__)
+#define REG(cb) namespace { int CTOR_UNIQUE_NAME(unique) = reg(cb); }
// Predefined configuration keys
namespace cfg