C#(.net8) 和 純C++ 在 WINDOWS 10/11 x64下 編譯x32/x64程式 正確呼叫系統內建應用程式(OSK.exe)

C#(.net8) 和 純C++ 在 WINDOWS 10/11 x64下 編譯x32/x64程式 正確呼叫系統內建應用程式(OSK.exe)

C#(.net8) 和 純C++ 在 WINDOWS 10/11 x64下 編譯x32/x64程式 正確呼叫系統內建應用程式(OSK.exe)


資料來源: AI(perplexity ~ C#) + (copilot ~C++)

GITHUB: https://github.com/jash-git/CS_CPP-call-osk-base-on-windows-11-x64-


perplexity ~ 如何使用C# 寫出一個可以在X64和X32模式下都可正常啟動OSK的範例

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace LaunchOSKExample
{
    class Program
    {
        static void Pause()
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
        }
        static void Main(string[] args)
        {
            //Directory.GetParent 是 C# 中用來取得指定路徑的父目錄的靜態方法
            //Environment.GetFolderPath 是 C# 中用來取得特殊文件夾路徑的方法。
            //Environment.SpecialFolder.System 是 C# 中的一个枚举值,属于 System.Environment 类。它指向操作系统的系统文件夹路径。在 Windows 上,这通常是包含 Windows 系统文件的目录
            bool is64BitOS = Environment.Is64BitOperatingSystem;
            bool is64BitProcess = Environment.Is64BitProcess;
            if (is64BitProcess)
            {
                Process.Start(
                    Path.Combine(
                        Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName,
                        "System32",
                        "cmd.exe"),
                    "/c osk.exe");
            }
            else
            {
                Process.Start(
                    Path.Combine(
                        Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName,
                        "Sysnative",
                        "cmd.exe"),
                    "/c osk.exe");
            }


            Pause();
        }
    }
}


copilot ~ 將上述C#轉換成C++

#include <iostream>
#include <string>
#include <windows.h>
#include <shlobj.h> // For SHGetFolderPath
#include <thread>
#include <chrono>

void Pause()
{
    std::cout << "Press any key to continue...";
    std::cin.get();
}

std::string GetParentDirectory(const std::string& path)
{
    size_t lastSlash = path.find_last_of("\\/");
    if (lastSlash != std::string::npos)
    {
        return path.substr(0, lastSlash);
    }
    return path;
}

int main()
{
    BOOL is64BitProcess = FALSE;
    if (sizeof(void*) == 4) {
        std::cout << "編譯結果為 32 位" << std::endl;
        is64BitProcess = FALSE;
    }
    else if (sizeof(void*) == 8) {
        std::cout << "編譯結果為 64 位" << std::endl;
        is64BitProcess = TRUE;
    }

    CHAR systemPath[MAX_PATH];
    SHGetFolderPathA(NULL, CSIDL_SYSTEM, NULL, SHGFP_TYPE_CURRENT, systemPath);

    std::string systemParentPath = GetParentDirectory(systemPath);
    std::string command;

    if (is64BitProcess)
    {
        command = systemParentPath + "\\System32\\cmd.exe /c osk.exe";
    }
    else
    {
        command = systemParentPath + "\\Sysnative\\cmd.exe /c osk.exe";
    }

    STARTUPINFOA si = { sizeof(si) };
    PROCESS_INFORMATION pi = {};
    if (!CreateProcessA(NULL, const_cast<char*>(command.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        std::cerr << "Failed to launch process. Error: " << GetLastError() << std::endl;
    }
    else
    {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }

    Pause();
    return 0;
}

One thought on “C#(.net8) 和 純C++ 在 WINDOWS 10/11 x64下 編譯x32/x64程式 正確呼叫系統內建應用程式(OSK.exe)

  1. C#(.net8) 和 純C++ 在 WINDOWS 10/11 x64下 編譯x32/x64程式 正確 呼叫/執行 系統內建應用程式(OSK.exe)

    判斷 x64/x32 編譯本身/作業系統

    PS C++判斷 OS code

    BOOL is64BitOS = FALSE;
    #if defined(_WIN64)
    is64BitOS = TRUE;
    #else
    BOOL isWow64 = FALSE;
    if (IsWow64Process(GetCurrentProcess(), &isWow64))
    {
    is64BitOS = isWow64;
    }
    #endif

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *