Solved

Dynamically Linking DLL's from VC Application

Posted on 2013-01-14
6
613 Views
Last Modified: 2013-01-14
I am trying to work around a problem with DLL's.  I work in a database environment where the SDK calls have changed over the years.  I would like one of my applications to work regardless of which software version I am talking to.  The problem is that the DLL filenames have changed over the years as new functions have been added.  What I need is to make my application "dynamically" call the dynamically-loaded libraries.

The application is in Visual C/C++.  I currently have #define's to enable the use of functions only available in newer code, so I have to change the source file to compile for each specific version.  Additionally, I *also* have to change the Linker source file of the LIB with which I am linking.  

For example, older "v7.5" functions are available in the DLL W3DBAV75.DLL, and I must link with W3DBAV75.LIB.  If I want to use some of the newer functions that were added to a more recent version, then I have to change some #defines and change my linker options to use W3DBAV78.LIB, W3DBAV80.LIB, or W3DBAV90.LIB, which changes my DLL to the corresponding DLL, too.  I then recompile and I have a new release.

Of course, since I have to change #defines and Links and recompile, I have to post multiple versions of my application, too, so that users who have a newer version of the environment can use the newer features, while users who have an older environment still find the tool functional.  This gets confusing for the end users, who are not always experts in renaming EXE files (or reading documentation, for that matter).

What I would LIKE to do is to figure out how to get this to work automatically.  Essentially, the application can make a call to query the database environment version FIRST, then intelligently determine the exact DLL that needs to be called at runtime.  Obviously, the code would know which version of the DLL was loaded so that it would only call "valid" functions for the given version of the DLL to avoid any crashes or bad memory references.  

Is this level of intelligence even possible in Visual C/C++?  If so, how?
0
Comment
Question by:Bill Bach
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
ID: 38775346
Have you considered loading your DLLs and exporting the functions are runtime (explicit linkage) rather than compile time (implicit linkage)? As long as you know where the DLLs live you can just enumerate them all,  load each in turn, check its version and then import the functions of the newest DLL (or whichever one it is you need). This is, generally, how plugin type features are managed.

http://msdn.microsoft.com/en-us/library/784bt7z7.aspx
0
 
LVL 28

Author Comment

by:Bill Bach
ID: 38775382
This is exactly the kind of kickstart I needed.  Compiling some test code now...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 38775554
Great stuff. Just so you know, I am not a Windows programming expert as such so I hope this at least point you in the right direction; however, if you need more specific help I'm sure one of the Windows experts (I'm thinking of jkr in particular) will be in a position to assist.

Good luck.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Author Comment

by:Bill Bach
ID: 38775561
Thanks.  Retrofitting this into an application with approximately 50 DLL calls, it looks like its going to take a bit of time.  I'm trying to limit my testcase now.
0
 
LVL 28

Author Comment

by:Bill Bach
ID: 38776304
This indeed was exactly what I needed.  It isn't easy to retrofit into vendor-supplied .h files, especially when there are approximately 40 functions, but the first 5 that I did have worked perfectly.  It's allowed me to create a list of possible DLL's like this:
int LoadDTILibraryFunctions()
{
	hDLL = LoadLibrary("W3DBAV90.DLL");
	if (hDLL!=NULL)
	{
	   CallPvStart=(PVSTART)GetProcAddress(hDLL,"PvStart");
	   CallPvStop=(PVSTOP)GetProcAddress(hDLL,"PvStop");
	   CallPvConnectServer=(PVCONNECTSERVER)GetProcAddress(hDLL,"PvConnectServer");
	   CallPvDisconnect=(PVDISCONNECT)GetProcAddress(hDLL,"PvDisconnect");
	   CallPvGetServerName=(PVGETSERVERNAME)GetProcAddress(hDLL,"PvGetServerName");
	   return(90);
	}
	else
	{
		hDLL = LoadLibrary("W3DBAV80.DLL");
		if (hDLL != NULL)
		{
		   CallPvStart=(PVSTART)GetProcAddress(hDLL,"PvStart");
		   CallPvStop=(PVSTOP)GetProcAddress(hDLL,"PvStop");
		   CallPvConnectServer=(PVCONNECTSERVER)GetProcAddress(hDLL,"PvConnectServer");
		   CallPvDisconnect=(PVDISCONNECT)GetProcAddress(hDLL,"PvDisconnect");
		   CallPvGetServerName=(PVGETSERVERNAME)GetProcAddress(hDLL,"PvGetServerName");
		   return(80);
		}
	}

	return(0);
}

Open in new window


This lets me return the right version and even grab only the functions that I KNOW exist in each DLL.  Thanks again for your help!
0
 
LVL 40

Expert Comment

by:evilrix
ID: 38776331
You're very welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now