RAD Studio -点击按钮打开exe文件



我用的是FMX。有人知道我如何运行.exe文件在一个按钮点击?在这个问题上没有足够的资源。

I am trying

extern DELPHI_PACKAGE NativeUInt __fastcall FileOpen('Consoleapp.exe');

但老实说,我没能看到一个工作的例子。

更新:完整代码如下:

#include <fmx.h>
#pragma hdrstop
#include <shellapi.h>
#include "MainStartWindow.h"
#include "LandingWindow.h"
#include "Login.h"
#include "Register.h"
#include "TempDashboard.h"
#pragma package(smart_init)
#pragma resource "*.fmx"
TLandingWin *LandingWin;
__fastcall TLandingWin::TLandingWin(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TLandingWin::FormClose(TObject *Sender, TCloseAction &Action)
{
// if LandingWin gets closed by X, startWin gets closed as well
startWin->Close();
}
void __fastcall TLandingWin::landingLoginButtonClick(TObject *Sender)
{
LoginWin->Show();
RegisterWin->Close();
//LandingWin->Hide();
}
void __fastcall TLandingWin::landingRegisterButtonClick(TObject *Sender)
{
RegisterWin->Show();
LoginWin->Close();
}
void __fastcall TLandingWin::guestLoginButton2Click(TObject *Sender)
{
HINSTANCE result = ShellExecuteW(Handle, NULL, L"\Console\app.exe", NULL, NULL, SW_SHOW);
if (int(result) > 32)
{
// success...
}
else
{
// error...
}
LandingWin->Hide();
}

知道为什么我得到这个错误吗?

shellapi.h(88):候选函数不可用:不知道从'Fmx::Types::TWindowHandle *'到'HWND'(又名'HWND__ *')的第一个参数的转换

在c++中,'Consoleapp.exe'是一个多字节字符字面值,而不是字符串字面值。与Delphi不同的是,Delphi对字符和字符串都使用'...',而c++则需要对字符和字符串分别使用'...'"..."。而且,在这两种情况下,您都需要转义c++文字中使用的字符(在Delphi中不需要),例如:"Console\app.exe".

现在,也就是说,你说你想要">运行"另一个程序。FileOpen()不是实现这一目标的方式。它只是打开文件,以便您可以访问其原始数据。

在Windows上,使用Win32CreateProcess()函数代替,例如:

#ifdef _Windows
#include <windows.h>
#else
// include other platform headers as needed...
#endif
void __fastcall TForm1::Button1Click(TObject *Sender)
{
#ifdef _Windows
STARTUPINFOW si = {};
si.cb = sizeof(si);
// populate other fields as needed...
PROCESS_INFORMATION pi = {};
WCHAR szCmdLine[] = L"Console\app.exe";
if (CreateProcessW(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
// success...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
// error...
}
#else
// handle other platforms as needed...
#endif
}

或者,您可以使用ShellExecute()/ShellExecuteEx()函数代替,例如:

#ifdef _Windows
#include <windows.h>
#include <shellapi.h>
#include <FMX.Platform.Win.hpp>
#else
// include other platform headers as needed...
#endif
void __fastcall TForm1::Button1Click(TObject *Sender)
{
#ifdef _Windows
HINSTANCE result = ShellExecuteW(
Fmx::Platform::Win::FormToHWND(this),
NULL, L"Console\app.exe", NULL, NULL, SW_SHOW);
if (reinterpret_cast<INT_PTR>(result) > 32)
{
// success...
}
else
{
// error...
}
#else
// handle other platforms as needed...
#endif
}
#ifdef _Windows
#include <windows.h>
#include <shellapi.h>
#include <FMX.Platform.Win.hpp>
#else
// include other platform headers as needed...
#endif
void __fastcall TForm1::Button1Click(TObject *Sender)
{
#ifdef _Windows
SHELLEXECUTEINFOW sei = {};
sei.cbSize = sizeof(sei);
sei.hwnd = Fmx::Platform::Win::FormToHWND(this);
sei.lpFile = L"Console\app.exe";
sei.nShow = SW_SHOW;
// populate other fields as needed...
if (ShellExecuteExW(&sei))
{
// success ...
}
else
{
// error...
}
#else
// handle other platforms as needed...
#endif
}

ShellExecute API(假设您确实想执行该文件)。

#include <shellapi.h>
ShellExecute(0,L"open",L"app.exe",0,0,SW_SHOWNORMAL);

最新更新