Re: Exception handling for loading of dependency library?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: Sorry - basic Q about using char[] instead of CString
    ... I've got a call to an external DLL I'm using LoadLibrary to get. ... lpFUNCTION MyFunc(CString p1, CString p2, short* retVal, CString* retText) ... It is not up to you how to declare the function; you need to know how it is actually declared in the DLL. ...
    (microsoft.public.vc.mfc)
  • Re: How to declare a pointer to a struct (typedef) in VB6
    ... but how do I declare a pointer to that variable so that I ... It can depend on how the 'struct' is declared in the signature. ... are using a regular Dll or an ActiveX Dll. ...
    (microsoft.public.vb.general.discussion)
  • Re: How to declare a pointer to a struct (typedef) in VB6
    ... but how do I declare a pointer to that variable so that I ... Just write your declare statement with the type as a byref parameter, ... BTW, on a somewhat related note, the VB app Im creating uses this dll ... but I also have it calling functions from a dll ...
    (microsoft.public.vb.general.discussion)
  • Re: pointer to pointer
    ... I have to call a function which is in dll in VB6. ... The function returns a pointer to pointer to an integer. ... private Declare Function hadmat lib "xxx.dll" as long ... CopyMemory x, byval x, 4 ...
    (microsoft.public.vb.general.discussion)
  • Re: pointer to pointer
    ... the function in the dll is something like this: ... int **hadmat1 ... The function returns a pointer to pointer to an integer. ... How to declare ...
    (microsoft.public.vb.general.discussion)