Re: Exception handling for loading of dependency library?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sun, 23 Oct 2005 12:26:00 -0400
In order to do that, you will have to *not* use "implicit linking" but use "explicit
linking".
Suppose you have a function
int whatever(const CString & s, long w);
then right now, you call it as
int x = whatever(s, 451);
or something like that. This causes an external reference to be made to the function
'whatever', and when the linker goes to link, it finds the .lib file to contain not the
code for the function, but just a reference, e.g., <"mydll", "whatever">. At this point,
the DLL *must* be installed on the target machine, because loading the program will
attempt to also load "mydll.dll", and that will fail.
Instead, you will have to do something like this. Note that the typedef is not mandatory
but it makes life a lot easier.
typedef int (*whateverproc)(const CString & s, long w);
then somewhere, you will declare a pointer variable to the function (note the use of the
name of the function here! This makes life a lot easier...)
whateverproc whatever = NULL;
This variable should be declared at a level where it is available to whoever needs to call
the whatever function, and it must continue to exist as long as the function will be
needed. A common technique for doing this is to declare it as a global variable, usually
in a separate module, with a header file that says
extern whateverproc whatever;
along with the typedef.
Now, to load the library, you need to do
HMODULE module = ::LoadLibrary("mydll.dll");
if(module == NULL)
{ /* deal with it */
... it didn't load.
} /* deal with it */
else
{ /* loaded it */
whatever = (whateverproc)::GetProcAddress(module, "whatever");
ASSERT(whatever != NULL);
... other functions as needed
} /* loaded it */
When you are all done (usually as your program is exiting) you would typically call
FreeLibrary(module). Hence the variable should be declared with the same extent as the
pointers. Usually what I do is create a separate module which has all the pointers and
the handle for a given dll all in one place.
Note that your function call still looks like
int x = whatever(s, 45);
you do not need the obsolete syntax (*whatever)(...), which although it still works, is
pointless because ANSI C does not require dereferencing function pointer variables.
Now, all you have to do is make sure you don't call the function if its pointer, or that
module pointer, is NULL.
joe
On Sun, 23 Oct 2005 11:07:54 -0400, "MikeB" <mboonept@xxxxxxxxx> wrote:
>Hi,
>
>I have a program that has a few functions that require calls from a .lib
>file that is an Additional Dependency in the project setting. What I need
>to happen is, if the person does not have the needed .dll on their system, I
>would still like for the program to be able to run and the user to access
>all the functions except the ones that require the missing .dll. But what
>happens is, as soon as the program launches they get an error saying the
>program failed to start because xxx.dll was not found, and the program
>closes. Is there an easy way to handle this problem so that the program can
>go ahead and launch, and they won't see the message about the missing dll
>until they try to access one of the functions that requires it? Thanks.
>
>Mike B.
>
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Prev by Date: Re: Convert int to cstring?
- Next by Date: Re: Convert int to cstring?
- Previous by thread: Convert int to cstring?
- Next by thread: Re: Exception handling for loading of dependency library?
- Index(es):
Relevant Pages
|