File indexing completed on 2024-05-12 05:45:31

0001 /*
0002     Copyright Hannah von Reth <vonreth@kde.org>
0003 
0004     Redistribution and use in source and binary forms, with or without
0005     modification, are permitted provided that the following conditions
0006     are met:
0007     1. Redistributions of source code must retain the above copyright
0008        notice, this list of conditions and the following disclaimer.
0009     2. Redistributions in binary form must reproduce the above copyright
0010        notice, this list of conditions and the following disclaimer in the
0011        documentation and/or other materials provided with the distribution.
0012 
0013     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0014     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0017     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0018     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0019     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0020     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0021     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0022     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0023     SUCH DAMAGE.
0024 */
0025 
0026 #include "kshim.h"
0027 #include "kshimmain.h"
0028 #include "kshimdata.h"
0029 
0030 #include <windows.h>
0031 #include <shellapi.h>
0032 
0033 int _main()
0034 {
0035     const auto commandLine = GetCommandLineW();
0036     int argc;
0037     wchar_t **argv = CommandLineToArgvW(commandLine, &argc);
0038 
0039     std::vector<KShimLib::string_view> args;
0040     args.resize(argc);
0041     for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
0042         args[i] = argv[i];
0043     }
0044 
0045     // global handle, don't free
0046     const auto handle = FindResourceW(nullptr, KShimLib::PayLoadKey.data(), KShimLib::PayloadCategory.data());
0047     const auto payload = static_cast<uint8_t *>(LockResource(LoadResource(nullptr, handle)));
0048 
0049     return KShim::main({ payload, payload + SizeofResource(nullptr, handle) }, args);
0050 }
0051 
0052 int main()
0053 {
0054     return _main();
0055 }
0056 
0057 int WINAPI WinMain(HINSTANCE, HINSTANCE, char *, int)
0058 {
0059     if (AttachConsole(ATTACH_PARENT_PROCESS)) {
0060         FILE *dummy;
0061         _wfreopen_s(&dummy, L"CONOUT$", L"w", stdout);
0062         setvbuf(stdout, nullptr, _IONBF, 0);
0063 
0064         _wfreopen_s(&dummy, L"CONOUT$", L"w", stderr);
0065         setvbuf(stderr, nullptr, _IONBF, 0);
0066         std::ios::sync_with_stdio();
0067     }
0068     return _main();
0069 }