dsfs21312 2015-06-17 09:13
浏览 165
已采纳

PHP执行程序失败

I want to execute a C++ Program with my PHP Back-end. The C++ Program is responsible for removing usb devices like usb sticks from my PC. When I open the program (which is on a separate local drive) with the CLI without administrative rights, the program starts and finishes the job correctly.

When I start the program with PHP using exec("/path/to/my/program.exe and-parameters") which is literally the same way as from the CLI, the program just starts and returns "failed" so something is different when using the CLI.

The C++ Code:

//
// RemoveDriveByLetter.cpp by Uwe Sieber - www.uwe-sieber.de
//
// Simple demonstration how to prepare a disk drive for save removal
//
// Works with removable and fixed drives under W2K, XP, W2K3, Vista
//
// Console application - expects the drive letter of the drive to remove as parameter
//
// you are free to use this code in your projects
//


#include "stdafx.h"
#include <stdio.h>

#include <windows.h>

#include <Setupapi.h>
#include <winioctl.h>
#include <winioctl.h>
#include <cfgmgr32.h>
#include <string>


//-------------------------------------------------
DEVINST GetDrivesDevInstByDeviceNumber(long DeviceNumber, UINT DriveType, char* szDosDeviceName);
//-------------------------------------------------



//-------------------------------------------------
int main(int argc, char* argv[])
{

    /*if ( argc != 2 ) {
        return 1;       
    }*/
    char DriveLetter = argv[1][0];
    DriveLetter &= ~0x20; // uppercase



    if ( DriveLetter < 'A' || DriveLetter > 'Z' ) {
        return 1;
    }

    std::string path = "";
    path += DriveLetter;
    path.append(":\\");
    printf(path.c_str());

    char szRootPath[sizeof(path)] ="";
    strncpy(szRootPath, path.c_str(), sizeof(path));

    std::string device = "";
    device += DriveLetter;
    device.append(":");
    printf(device.c_str());

    char szDevicePath[sizeof(device)] = "";
    strncpy(szDevicePath, device.c_str(), sizeof(device));

    std::string accesspath = "";
    accesspath += "\\\\.\\";
    accesspath += device;
    printf(accesspath.c_str());

    char szVolumeAccessPath[sizeof(accesspath)] = "";   // "\\.\X:"  -> to open the volume
    strncpy(szVolumeAccessPath, accesspath.c_str(), sizeof(accesspath));

    long DeviceNumber = -1;

    // open the storage volume
    HANDLE hVolume = CreateFile(szVolumeAccessPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
    if (hVolume == INVALID_HANDLE_VALUE) {
        return 1;
    }

    // get the volume's device number
    STORAGE_DEVICE_NUMBER sdn;
    DWORD dwBytesReturned = 0;
    long res = DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
    if ( res ) {
        DeviceNumber = sdn.DeviceNumber;
    }
    CloseHandle(hVolume);

    if ( DeviceNumber == -1 ) {
        return 1;
    }

    // get the drive type which is required to match the device numbers correctely
    UINT DriveType = GetDriveType(szRootPath);

    // get the dos device name (like \device\floppy0) to decide if it's a floppy or not - who knows a better way?
    char szDosDeviceName[MAX_PATH];
    res = QueryDosDevice(szDevicePath, szDosDeviceName, MAX_PATH);
    if ( !res ) {
        return 1;
    }

    // get the device instance handle of the storage volume by means of a SetupDi enum and matching the device number
    DEVINST DevInst = GetDrivesDevInstByDeviceNumber(DeviceNumber, DriveType, szDosDeviceName);

    if ( DevInst == 0 ) {
        return 1;
    }

    PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown; 
    WCHAR VetoNameW[MAX_PATH];
    VetoNameW[0] = 0;
    bool bSuccess = false;

    // get drives's parent, e.g. the USB bridge, the SATA port, an IDE channel with two drives!
    DEVINST DevInstParent = 0;
    res = CM_Get_Parent(&DevInstParent, DevInst, 0); 

    for ( long tries=1; tries<=3; tries++ ) { // sometimes we need some tries...

        VetoNameW[0] = 0;

        // CM_Query_And_Remove_SubTree doesn't work for restricted users
        //res = CM_Query_And_Remove_SubTreeW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, CM_REMOVE_NO_RESTART); // CM_Query_And_Remove_SubTreeA is not implemented under W2K!
        //res = CM_Query_And_Remove_SubTreeW(DevInstParent, NULL, NULL, 0, CM_REMOVE_NO_RESTART);  // with messagebox (W2K, Vista) or balloon (XP)

        res = CM_Request_Device_EjectW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, 0);
        //res = CM_Request_Device_EjectW(DevInstParent, NULL, NULL, 0, 0); // with messagebox (W2K, Vista) or balloon (XP)

        bSuccess = (res==CR_SUCCESS && VetoType==PNP_VetoTypeUnknown);
        if ( bSuccess )  { 
            break;
        }

        Sleep(500); // required to give the next tries a chance!
    }

    if ( bSuccess ) {
        printf("Success

");
        return 0;
    }

    printf("failed
");

    printf("Result=0x%2X
", res);

    if ( VetoNameW[0] ) {
        printf("VetoName=%ws)

", VetoNameW);
    }   
    return 1;
}
//-----------------------------------------------------------

char* appendCharToCharArray(char* array, char a)
{
    size_t len = strlen(array);

    char* ret = new char[len+2];

    strcpy(ret, array);    
    ret[len] = a;
    ret[len+1] = '\0';

    return ret;
}


//----------------------------------------------------------------------
// returns the device instance handle of a storage volume or 0 on error
//----------------------------------------------------------------------
DEVINST GetDrivesDevInstByDeviceNumber(long DeviceNumber, UINT DriveType, char* szDosDeviceName)
{
    bool IsFloppy = (strstr(szDosDeviceName, "\\Floppy") != NULL); // who knows a better way?

    GUID* guid;

    switch (DriveType) {
    case DRIVE_REMOVABLE:
        if ( IsFloppy ) {
            guid = (GUID*)&GUID_DEVINTERFACE_FLOPPY;
        } else {
            guid = (GUID*)&GUID_DEVINTERFACE_DISK;
        }
        break;
    case DRIVE_FIXED:
        guid = (GUID*)&GUID_DEVINTERFACE_DISK;
        break;
    case DRIVE_CDROM:
        guid = (GUID*)&GUID_DEVINTERFACE_CDROM;
        break;
    default:
        return 0;
    }

    // Get device interface info set handle for all devices attached to system
    HDEVINFO hDevInfo = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (hDevInfo == INVALID_HANDLE_VALUE)   {
        return 0;
    }

    // Retrieve a context structure for a device interface of a device information set
    DWORD dwIndex = 0;
    long res;

    BYTE Buf[1024];
    PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;
    SP_DEVICE_INTERFACE_DATA         spdid;
    SP_DEVINFO_DATA                  spdd;
    DWORD                            dwSize;

    spdid.cbSize = sizeof(spdid);

    while ( true )  {
        res = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, dwIndex, &spdid);
        if ( !res ) {
            break;
        }

        dwSize = 0;
        SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, NULL, 0, &dwSize, NULL); // check the buffer size

        if ( dwSize!=0 && dwSize<=sizeof(Buf) ) {

            pspdidd->cbSize = sizeof(*pspdidd); // 5 Bytes!

            ZeroMemory(&spdd, sizeof(spdd));
            spdd.cbSize = sizeof(spdd);

            long res = SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, dwSize, &dwSize, &spdd);
            if ( res ) {

                // in case you are interested in the USB serial number:
                // the device id string contains the serial number if the device has one,
                // otherwise a generated id that contains the '&' char...
                /*
                DEVINST DevInstParent = 0;
                CM_Get_Parent(&DevInstParent, spdd.DevInst, 0); 
                char szDeviceIdString[MAX_PATH];
                CM_Get_Device_ID(DevInstParent, szDeviceIdString, MAX_PATH, 0);
                printf("DeviceId=%s
", szDeviceIdString);
                */

                // open the disk or cdrom or floppy
                HANDLE hDrive = CreateFile(pspdidd->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                if ( hDrive != INVALID_HANDLE_VALUE ) {
                    // get its device number
                    STORAGE_DEVICE_NUMBER sdn;
                    DWORD dwBytesReturned = 0;
                    res = DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
                    if ( res ) {
                        if ( DeviceNumber == (long)sdn.DeviceNumber ) {  // match the given device number with the one of the current device
                            CloseHandle(hDrive);
                            SetupDiDestroyDeviceInfoList(hDevInfo);
                            return spdd.DevInst;
                        }
                    }
                    CloseHandle(hDrive);
                }
            }
        }
        dwIndex++;
    }

    SetupDiDestroyDeviceInfoList(hDevInfo);

    return 0;
}
//-----------------------------------------------------------

The program returns:

array(2) ( [0] => (string) D:\D:\.\D:failed [1] => (string) Result=0x33 )

Somebody a suggestion?

  • 写回答

3条回答 默认 最新

  • douciwang6819 2015-06-22 07:49
    关注

    If you are running PHP in safe mode then only files in the safe_mode_exec_dir will be allowed to run.

    It appears you are running in the Windows environment. You may want to consider executing this using the Windows shell which gives you more control over your external executing programs, and may return additional information if it fails and help diagnosing what the underlying issue is with the exec() function.

    Comments from the online PHP manual are:

    start Notepad.exe minimized in the background:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("notepad.exe", 7, false); 
    ?> 
    

    start a shell command invisible in the background:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("cmd /C dir /S %windir%", 0, false); 
    ?> 
    

    start MSPaint maximized and wait for you to close it before continuing the script:

    <?php 
    $WshShell = new COM("WScript.Shell"); 
    $oExec = $WshShell->Run("mspaint.exe", 3, true); 
    ?> 
    

    For more info on the Run() method go to: https://msdn.microsoft.com/en-us/subscriptions/d5fk67ky(v=vs.84).aspx

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。