1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

dll from win32 console.

Discussion in 'Win32' started by sujan.dasmahapatra, Oct 22, 2011.

  1. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Dear Friends
    I have a mfc application which I already converted to a dll. now I want to launch that application from another win32 client application. So how I can achieve this. Any h elp would be highly appreciated.
    check my code snippet.

    //in App.h header
    extern "C" __declspec(dllexport) void runAppli();
    //in App.cpp
    __declspec
    (dllexport) void runAppli(HWND hWnd)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    }
    //In the client console

    int main(int argc, char **argv)
    {
    typedef void (*EntryPointfuncPtr)(int argc, const char * argv );
    HINSTANCE LoadMe;
    LoadMe = LoadLibrary(L
    "C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll");
    if (LoadMe != 0)
    printf(
    "LoadMe library loaded!\n");
    else
    printf("LoadMe library failed to load!\n");
    EntryPointfuncPtr LibMainEntryPoint;
    LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,
    "runAppli");
    return 0;
    }

    I am not able to launch with this code. Please tell me whats going wrong... My application is appearing and disappearing from the screen.

    Thanks a lot for any help. Sujan
     
  2. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    My application is launching but its going off. Please tell me whats going wrong. Thanks Sujan
     
  3. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    check my code snippet

    int main(int argc, char **argv)
    {
    /* get handle to dll */
    HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));

    /* get pointer to the function in the dll*/
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"runAppli");

    /*
    Define the Function in the DLL for reuse. This is just prototyping the dll's function.
    A mock of it. Use "stdcall" for maximum compatibility.
    */
    typedef BOOL (__stdcall * pICFUNC)(HWND);

    pICFUNC MyFunction;
    MyFunction = pICFUNC(lpfnGetProcessID);


    HWND fWindow; // Window handle of the window you want to get

    fWindow = FindWindow(NULL, L"Window_title"); // Find the window

    ///* The actual call to the function contained in the dll */
    BOOL intMyReturnVal = MyFunction(fWindow);

    /* Release the Dll */
    FreeLibrary(hGetProcIDDLL);
    return 0;
    }

    //////////////////////////////////////////////////////////////////////////////
    //and in the App.cpp
    ///////////////////////////////////////////////////////////////
    __declspec(dllexport) void runAppli(HWND hwnd)

    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    thisDLL->m_pMainWnd->ShowWindow(SW_SHOW);
    }


    My application is launching but crashing immediately. Please tell me what should I do ? Please help me someone. Thanks Sujan
     
  4. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    I am able to reduce the code to this much but still its crashing...
    /////////////////////////////////////////////////////////////////////////////////////
    int main(int argc, char **argv)
    {
    /* get handle to dll */
    HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));

    /* get pointer to the function in the dll*/
    FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"runAppli");

    /*
    Define the Function in the DLL for reuse. This is just prototyping the dll's function.
    A mock of it. Use "stdcall" for maximum compatibility.
    */
    typedef BOOL (* pICFUNC)(HINSTANCE, LPCSTR);

    pICFUNC MyFunction=NULL;

    ///* The actual call to the function contained in the dll */
    BOOL intMyReturnVal = MyFunction(HMODULE (hGetProcIDDLL), "runAppli");

    /* Release the Dll */
    FreeLibrary(hGetProcIDDLL);
    return 0;
    }
    /////////////////////////////////////////////////////////////////////////////////////

    __declspec(dllexport) void runAppli()

    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    }
    ////////////////////////////////////////////////////////////////////////////////////
     
  5. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Now the code is looking like this ....But still application is not launching.

    int main(int argc, char **argv)
    {
    /* get handle to dll */
    HMODULE hDll = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
    typedef BOOL (* ICFUNC)(HINSTANCE, LPCSTR);
    ICFUNC lpfnGetProcessID = (ICFUNC)GetProcAddress(hDll, "runAppli");
    ///* The actual call to the function contained in the dll */
    BOOL intMyReturnVal = lpfnGetProcessID(hDll, "runAppli");
    /* Release the Dll */
    FreeLibrary(hDll);
    return 0;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    extern "C" BOOL __declspec(dllexport) runAppli(HWND hwnd, LPCSTR lpAppName)
    {
    // Only needed if resources are accessed
    //AFX_MANAGE_STATE(AfxGetStaticModuleState());
    ::ShowWindow(hwnd, SW_SHOW);
    UNREFERENCED_PARAMETER(lpAppName);
    return true;
    }
     
  6. sujan.dasmahapatra

    sujan.dasmahapatra New Member

    Joined:
    Jun 11, 2009
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    How can I get the handle from the client console and pass it to the lpfnGetProcessID .... to runAppli....With the code below I am getting run-time error. that hwnd being used without being intialized.. Please tell me how can I initialize hwnd. Thanks Sujan


    int main(int argc, char **argv)
    {
    HWND hwnd;
    /* get handle to dll */
    HMODULE hDll = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll"));
    typedef BOOL (* ICFUNC)(HWND, LPCSTR);
    ICFUNC lpfnGetProcessID = (ICFUNC)GetProcAddress(hDll, "runAppli");
    ///* The actual call to the function contained in the dll */
    BOOL intMyReturnVal = lpfnGetProcessID(hwnd, "runAppli");
    /* Release the Dll */
    FreeLibrary(hDll);
    return 0;
    }
     

Share This Page