dll from win32 console.

sujan.dasmahapatra's Avatar
Go4Expert Member
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)
{
typedefvoid (*EntryPointfuncPtr)(int argc, constchar * argv );
HINSTANCE LoadMe;
LoadMe = LoadLibrary(L
"C:\\Users\\DasmahapatraS\\Projects\\Bhagavan_Cade m\\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
sujan.dasmahapatra's Avatar
Go4Expert Member
My application is launching but its going off. Please tell me whats going wrong. Thanks Sujan
sujan.dasmahapatra's Avatar
Go4Expert Member
check my code snippet

int main(int argc, char **argv)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary(TEXT("C:\\Users\\DasmahapatraS\\Projec ts\\Bhagavan_Cadem\\RevolutionProj\\debug\\Revolut ionProj.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
sujan.dasmahapatra's Avatar
Go4Expert Member
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\\Projec ts\\Bhagavan_Cadem\\RevolutionProj\\debug\\Revolut ionProj.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());
}
////////////////////////////////////////////////////////////////////////////////////
sujan.dasmahapatra's Avatar
Go4Expert Member
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\\Projec ts\\Bhagavan_Cadem\\RevolutionProj\\debug\\Revolut ionProj.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;
}
sujan.dasmahapatra's Avatar
Go4Expert Member
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\\Projec ts\\Bhagavan_Cadem\\RevolutionProj\\debug\\Revolut ionProj.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;
}