簡體   English   中英

如何在C ++中將一個.exe鏈接到另一個.exe

[英]How to link one .exe to another .exe in C++

我有兩個.exe 他們需要在運行時使用彼此的功能。 一種是基於dilogue的應用程序.exe ,另一種是主應用程序.exe 現在,主應用程序需要使用基於對話框的應用程序功能。 但是在編譯時,由於dialog based applications .lib不可用,它可能能夠定位被called function的存在。 因為如果我將基於對話框的應用程序制作為.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。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM