【问题标题】:How to link one .exe to another .exe in C++如何在 C++ 中将一个 .exe 链接到另一个 .exe
【发布时间】:2013-10-15 09:58:21
【问题描述】:

我有两个.exes。他们需要在运行时使用彼此的功能。一种是基于对话的应用程序.exe,另一种是主应用程序.exe。现在主应用程序需要使用基于对话框的应用程序功能。但是在编译时,它可以 bot 定位 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 create\n";}
        // 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 引用的单独库中?
  • 这不起作用,需要 DLL。如果您不解决核心问题,这也可能会“在运行时失败”。

标签: c++ visual-studio-2010 mfc dialog linker


【解决方案1】:

您可以使用以下代码来启动您的 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 );

【讨论】:

    【解决方案2】:

    正如其他人所提到的,您有一个基本的设计问题。您还没有说明为什么需要两个可执行文件,但如果您确实需要两个独立的应用程序相互交互,那么您将需要使用某种形式的interprocess communication

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-30
          • 2010-09-12
          • 1970-01-01
          相关资源
          最近更新 更多