• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Convert VC++ (7.x) to dll

I have some existing VC++ code that I need to convert to a dll file.  This system is composed of several files.  I added the "extern c" and export definition file.  The dll created fine.   Every time I called a routine, it always executed the main (int main) function.  I commented out this function and I get "error LNK2019: unresolved external symbol  _main referenced in function _mainCRTStartup.
0
jayson_prichard
Asked:
jayson_prichard
1 Solution
 
Subrat (C++ windows/Linux)Software EngineerCommented:
code?
0
 
mjoachiamiakCommented:
The dll works a bit differently than exe. It is not possible to blindly copy paste all the functions to the dll.
dll needs to have DllMain function when exe has main. if you commented out the main function you should have DllMain (the starting point of your library).
I attached some sample code from one of my dlls.

" Every time I called a routine, it always executed the main (int main) function"
what do you mean by routine?
I think your "routine" is trying to call  int main() and because you commented it out the compiler cannot find it. I think this is the culprit here.

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		//loaded++;
		//Using FreeLibrary inside winmain is nonpredictible
		//if (loaded>1) FreeLibrary(hModule); //Unload itself 
 
 
		hInstDLL = hModule;
		//loaded=1;		
	case DLL_THREAD_ATTACH:
		 break;
	case DLL_THREAD_DETACH:
		 break;
	case DLL_PROCESS_DETACH:
		//if (loaded>0)
		//	loaded--;
		//if (MatlabCaller) mexPrintf("Closing all Dialog Windows, stopping cameras, unloading the cmuc2.dll\n");
		MatlabPrint("Closing all Dialog Windows, stopping cameras, unloading the cmuc2.dll\n");
		DllTrace(DLL_TRACE_ALWAYS,"Detaching\n");
		//stop cameras
		//Destroy all preview windows 
		//CFreeMemory(); < -- never do it fuckin shit
		if (DisplayArray)
		{
		for(int i=0;i<n_cameras;i++)
		{
			if (DisplayArray[i])
			{
				if (DisplayArray[i]->hwnd)
				{
				DestroyWindow(DisplayArray[i]->hwnd);	
				DisplayArray[i]->hwnd = NULL;
				}
				if (DisplayArray[i]->pCameraParametersDialog)
				{
					DestroyWindow(DisplayArray[i]->pCameraParametersDialog);	
					DisplayArray[i]->pCameraParametersDialog=NULL;
				}
				
			}
			CStopImageCapture(i);
		}
		}
		CFreeMemory(); //here it works ;D
#ifdef _DEBUG		
		DllTrace(DLL_TRACE_ALWAYS,"Calling DumpMemoryLeaks\n");
		int leak=_CrtDumpMemoryLeaks();
		DllTrace(DLL_TRACE_ALWAYS,"Memory Leaks %s\n",(leak!=0)?"DETECTED":"NOT detected");
#endif
		break;
	}
    return TRUE;
}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now