Skip to content

Instantly share code, notes, and snippets.

@he426100
Last active January 17, 2024 10:13
Show Gist options
  • Select an option

  • Save he426100/5ee27f34d4bc8d8eb4c1955ff2790eda to your computer and use it in GitHub Desktop.

Select an option

Save he426100/5ee27f34d4bc8d8eb4c1955ff2790eda to your computer and use it in GitHub Desktop.
php-embed-windows
# 设置 CMake 最小版本要求
cmake_minimum_required(VERSION 3.0)
# 设置项目名称
project(PhpyStand)
# 设置包含目录
include_directories(
"D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/include/main"
"D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/include"
"D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/include/sapi"
"D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/include/Zend"
"D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/include/TSRM"
)
# 查找所需库
find_library(PHP_LIB php8 REQUIRED PATHS "D:/phpy/php-8.1.27-src/x64/Release/php-8.1.27-devel-vs16-x64/lib")
# 添加主源文件并设置项目名称
add_executable(${PROJECT_NAME} main.cpp)
# 链接库
target_link_libraries(${PROJECT_NAME} ${PHP_LIB})
#include "stdafx.h"
#include <sapi/embed/php_embed.h>
#include <string>
#include <string.h>
#include <codecvt>
#include <locale>
#include <vector>
#include <shellapi.h>
#pragma comment(lib, "php8embed.lib")
int main(int argc, char *argv[])
{
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
// std::wstring _cwd;
std::wstring _home;
std::wstring _phpystand;
std::wstring _args;
std::vector<std::wstring> _argv;
LPWSTR *argvw;
int _argc;
_args = GetCommandLineW();
argvw = CommandLineToArgvW(_args.c_str(), &_argc);
if (argvw == NULL) {
MessageBoxA(NULL, "Error in CommandLineToArgvW()", "ERROR", MB_OK);
return false;
}
_argv.resize(_argc);
for (int i = 0; i < _argc; i++) {
_argv[i] = argvw[i];
}
LocalFree(argvw);
wchar_t path[MAX_PATH + 10];
GetCurrentDirectoryW(MAX_PATH + 1, path);
// _cwd = path;
GetModuleFileNameW(NULL, path, MAX_PATH + 1);
_phpystand = path;
SetEnvironmentVariableW(L"PHPYSTAND", _phpystand.c_str());
int size = (int)_phpystand.size() - 1;
for (; size >= 0; size--) {
if (_phpystand[size] == L'.') break;
}
if (size < 0) size = (int)_phpystand.size();
std::wstring main = _phpystand.substr(0, size);
std::wstring _script = L"require '";
_script += main + L".int';";
SetEnvironmentVariableW(L"PHPYSTAND_SCRIPT", _script.c_str());
// Convert the wide string to UTF-8 encoded std::string
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
std::string utf8_script = myconv.to_bytes(_script);
// Execute PHP code
PHP_EMBED_START_BLOCK(0, NULL);
zend_eval_string(utf8_script.c_str(), NULL, "phpy app");
PHP_EMBED_END_BLOCK();
return 0;
}
<?php
echo 'hello, world!', PHP_EOL;
#pragma once
#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#pragma comment(lib, "user32.lib")
#define NTS
#define PHP_WIN32 1
#define ZEND_WIN32 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment