We help IT Professionals succeed at work.

Convert VC++ (7.x) to dll

jayson_prichard
on
Medium Priority
323 Views
Last Modified: 2013-12-14
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.
Comment
Watch Question

Subrat (C++ windows/Linux)Software Engineer
CERTIFIED EXPERT

Commented:
code?
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.