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

You still have to call LibMainEntryPoint() if you want it to run...

I am calling this right ?

LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"runAppli");

whatelse i have to call ? please give me some code snippet. Thanks Sujan

My application is launching but its going off. Please tell me whats going wrong. Thanks Sujan

You called GetProcAddress() to retrieve the function pointer.
Now LibMainEntryPoint holds that pointer.
If you want to invoke the function you have to call it like any other function too.

myPtr = GetProcAddress(...); // get function address
myPtr(...); // call it

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

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());
}
////////////////////////////////////////////////////////////////////////////////////

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

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

Look Friends
Now I am not calling the dll from not winn32 but from another mfc exe application. Where I have a button when I press it I should load the dll and launch the mainwindow. See my two apis

void CClientRevProjDlg::OnBnClickedButton1()
{
    typedef VOID (*MYPROC)(HWND);
    HINSTANCE hinstLib;
    MYPROC ProcAdd;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
    HWND h = AfxGetMainWnd()->GetSafeHwnd();

    // Get a handle to the DLL module.
    hinstLib = LoadLibrary(L"C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cadem\\RevolutionProj\\debug\\RevolutionProj.dll");
    // If the handle is valid, try to get the function address.
    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "runAppli");
    // Do add operation
    // If the function address is valid, call the function.
        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
            (ProcAdd) (h);
        // Free the DLL module.
        fFreeResult = FreeLibrary(hinstLib);
    }
    // If unable to call the DLL function, use an alternative.
    if (! fRunTimeLinkSuccess)
        printf("message via alternative method\n");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//And in the dll App.cpp I have an extern "C" like this....

extern "C" BOOL __declspec(dllexport) runAppli(HWND h)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
//  HWND h = AfxGetMainWnd()->GetSafeHwnd();
    ShowWindow(h,SW_SHOW);
    return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

With this when I press the run button from client exe, my dll mainwindow is shown but crashing and hanging for sometime. Please help me get rid of this problem someone Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.