如何在C++中将一个.exe链接到另一个.exe

How to link one .exe to another .exe in C++

本文关键字:exe 一个 链接 另一个 C++      更新时间:2023-10-16

我有两个.exe。它们需要在运行时使用彼此的函数。一个是基于目录的应用程序.exe,另一个是主要应用程序.exe。现在主应用程序需要使用基于对话框的应用程序功能。但是在编译时,它可以机器人能够定位called function的存在,因为dialog based applications .lib不可用。因为如果我将基于对话框的应用程序设为.lib,那么程序将在运行时失败。所以,我需要拨打一个.exe到另一个.exe。当我尝试打电话时,

  TestClass tc;/* Call dialog based application exe functions*/
   tc.MyTest();

它抛出错误,例如

1>CallExe.obj : error LNK2001: unresolved external symbol "public: __thiscall TestClass::~TestClass(void)" (??1TestClass@@QAE@XZ)
1>CallExe.obj : error LNK2001: unresolved external symbol "public: void __thiscall TestClass::MyTest(void)" (?MyTest@TestClass@@QAEXXZ)
1>CallExe.obj : error LNK2001: unresolved external symbol "public: __thiscall TestClass::TestClass(void)" (??0TestClass@@QAE@XZ)

Main application.cpp

int _tmain(int argc, _TCHAR* argv[])
{
   STARTUPINFO si;     
   PROCESS_INFORMATION pi;
  TestClass tc;/* Call dialog based application exe functions*/
   tc.MyTest();
   // set the size of the structures
   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );
   LPCTSTR szCmdline = (LPCTSTR)(TEXT("Dialog.exe"));
  // start the program up
  if(!CreateProcess(TEXT("D:\Rasmi's\Personal\Visual Studio\Dialog\Debug\Dialog.exe"),   // the path
    argv[1],        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
        )
      {cout << "Unable to createn";}
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
        return 0;
    }

Dialog.exe

TestClass::TestClass(void)
{
}

TestClass::~TestClass(void)
{
}
void TestClass::MyTest()
{
    cout << "This is my Test Class n";
}

您可以使用以下代码来启动 exe 。

char exePath[200];
STARTUPINFO         si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
si.dwFlags     = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; //SW_SHOWDEFAULT;        //Hide the DOS or Console Window
si.hStdInput;
cstring sPrjPath="D:\Rasmi's\Personal\Visual Studio\Dialog\Debug";
    sprintf(exePath,"Dialog.exe %s", sPrjPath);

::SetCurrentDirectory(sPrjPath);
if( !CreateProcess( NULL, // No module name (use command line).
    exePath,      // Command line.
    NULL,                 // Process handle not inheritable.
    NULL,                 // Thread handle not inheritable.
    FALSE,                // Set handle inheritance to FALSE.
    NORMAL_PRIORITY_CLASS,// No creation flags.
    NULL,                 // Use parent's environment block.
    NULL,                 // Use parent's starting directory.
    &si,                  // Pointer to STARTUPINFO structure.
    &pi )                 // Pointer to PROCESS_INFORMATION structure.
    )
{
    WaitForSingleObject( pi.hProcess, INFINITE );
    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return;
}
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

正如其他人提到的,你有一个基本的设计问题。 你还没有说为什么需要两个可执行文件,但是如果你确实需要两个单独的应用程序相互交互,那么你就需要使用某种形式的进程间通信。

如果两个应用程序只需要共享公共代码来显示对话框窗体,但不需要相互交互,则可以创建一个 DLL 项目来包含共享代码。 然后,您的两个应用程序都将与此 DLL 链接。 有关示例,请参阅 MFC 扩展 DLL。

嗯,你不能那样做。如果你想在调用过程和函数等应用程序之间进行交互,你需要使用XML-RPC之类的东西,如果我理解正确的话

最简单的方法是使用 SendMessage() 将WM_USER消息发送到基于对话框的应用程序,并在消息循环中捕获它(使用 FindWindow() 获取句柄)。如果您有任何参数要传递,请考虑使用 WM_COPYDATA。