简体   繁体   中英

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

I have two .exe s. They need to use each other's functions at run times. One is dilogue based application .exe and other one is main application .exe . Now main application need to use dialog based applications function. But while compiling it could bot able to locate the existance of called function because unavailability of dialog based applications .lib . Because if i make dialog based application to .lib , then the program will fail at run time. So, I need to make call one .exe to other .exe . When I am trying call,

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

It is throwing errors like

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;
    }

In Dialog.exe

TestClass::TestClass(void)
{
}


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

you can use following code for launching your 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 );

As others mentioned, you have a fundamental design problem. You haven't said why you need two executables, but if you truly do have a need for two separate applications to interact with each other, then you will need to use some form of interprocess communication .

If your two applications need only to share common code to display dialog forms, but do not need interact with each other, then you can create a DLL project to contain the shared code. Both of your applications will then link with this DLL. See MFC Extension DLLs for examples.

hmm, you can't do like that. if you want to have a interaction between to apps like calling procedures and functions you need to use something like xml-rpc, if i understood you correctly

Easiest way would be to send a WM_USER message using SendMessage() to the dialog-based application and catch it in the message loop (use FindWindow() to get the handle). If you have any arguments to pass, consider using WM_COPYDATA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM