473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Catching the absence of a DLL

I've got a Managed C++ wrapper class that calls some functions that talk to
a hardware data acquisition card, via a LIB supplied by the vendor. The
calls ultimately go through to a DLL, which is installed in the System
directory. Now, everything works fine on the machine that actually has the
hardware board present, and has the DLL installed. But I also want to run
this code on my laptop, and I'd prefer not to clutter up my system directory
with that hardware-specific DLL. When I run up my Test app, which creates an
instance of the Managed wrapper class (which is in its own assembly) it
collapses with a 'FileNotFound' exception - complaining that my Managed
wrapper assembly (or one of its dependencies) is not present. Presumably the
CLR is trying to load the assembly that contains my managed wrapper, and
falls over because the required DLL (used by the LIB routines I'm calling)
cannot be found. Fair enough - except that the exception is getting thrown
right at the beginning, before I event attempt to create an instance of my
managed wrapper class.

What I'd really like to do is catch this exception somehow, and substitute a
software simulation of the hardware card when the real hardware isn't
present. Any ideas ?
Nov 16 '05 #1
2 1659
"Steve Terepin" <st***@terepin.freeserve.co.uk> wrote in message
news:#2*************@TK2MSFTNGP10.phx.gbl...
I've got a Managed C++ wrapper class that calls some functions that talk to a hardware data acquisition card, via a LIB supplied by the vendor. The
calls ultimately go through to a DLL, which is installed in the System
directory. Now, everything works fine on the machine that actually has the
hardware board present, and has the DLL installed. But I also want to run
this code on my laptop, and I'd prefer not to clutter up my system directory with that hardware-specific DLL. When I run up my Test app, which creates an instance of the Managed wrapper class (which is in its own assembly) it
collapses with a 'FileNotFound' exception - complaining that my Managed
wrapper assembly (or one of its dependencies) is not present. Presumably the CLR is trying to load the assembly that contains my managed wrapper, and
falls over because the required DLL (used by the LIB routines I'm calling)
cannot be found. Fair enough - except that the exception is getting thrown
right at the beginning, before I event attempt to create an instance of my
managed wrapper class.

What I'd really like to do is catch this exception somehow, and substitute a software simulation of the hardware card when the real hardware isn't
present. Any ideas ?


Chances are that the library supplied by the vendor is an import library.
When you link an application against an import library, the addresses of the
functions that it exports are not "resolved" until your application is
loaded. That address resolution can't happen until the DLL is loaded and its
base address is determined which is what ultimately determines the addresses
of the exports.

There are, I think, two ways to go.

One would have you not link against the vendor's import library. Rather you
load the DLL at runtime with LoadLibrary(). For every export in the vendor's
library that you need to call you would call GetProcAddress(). Armed with an
address, you can call the vendor's functions through a pointer. You can even
go so far as to build a "shim" DLL. That is a DLL which has the same number
of exports, each with the same calling convention. Your code would call
these shimmed functions. It would be the job of the functions in the shim to
either call the vendor's functions where his DLL is available or your
emulation.

Another option is, I think, to use a technique called "delayed loading".
With it, the complier and linker can be told to inhibit the loading of a DLL
at process activation. At runtime you would need to determine whether the
vendor's DLL is available, and if so, just call it. At that point, the delay
load mechanism would be used to load the DLL and obviate the need for calls
through pointers. If not available, you would need to call your emulation.
If you want to pursue this route check the docs for the meaning of the
/DELAYLOAD option and search the MSDN-CD or at http://msdn.microsft.com for
more info, especially the articles written by Matt Pietrek and John Robbins.

Regards,
Will

Nov 16 '05 #2
Many thanks - /DELAYLOAD does the trick nicely. Perfect :)))

S.

"William DePalo [MVP VC++ ]" <wi***********@mvps.org> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
"Steve Terepin" <st***@terepin.freeserve.co.uk> wrote in message
news:#2*************@TK2MSFTNGP10.phx.gbl...
I've got a Managed C++ wrapper class that calls some functions that talk to
a hardware data acquisition card, via a LIB supplied by the vendor. The
calls ultimately go through to a DLL, which is installed in the System
directory. Now, everything works fine on the machine that actually has the hardware board present, and has the DLL installed. But I also want to run this code on my laptop, and I'd prefer not to clutter up my system

directory
with that hardware-specific DLL. When I run up my Test app, which creates an
instance of the Managed wrapper class (which is in its own assembly) it
collapses with a 'FileNotFound' exception - complaining that my Managed
wrapper assembly (or one of its dependencies) is not present. Presumably the
CLR is trying to load the assembly that contains my managed wrapper, and
falls over because the required DLL (used by the LIB routines I'm

calling) cannot be found. Fair enough - except that the exception is getting thrown right at the beginning, before I event attempt to create an instance of my managed wrapper class.

What I'd really like to do is catch this exception somehow, and

substitute a
software simulation of the hardware card when the real hardware isn't
present. Any ideas ?
Chances are that the library supplied by the vendor is an import library.
When you link an application against an import library, the addresses of

the functions that it exports are not "resolved" until your application is
loaded. That address resolution can't happen until the DLL is loaded and its base address is determined which is what ultimately determines the addresses of the exports.

There are, I think, two ways to go.

One would have you not link against the vendor's import library. Rather you load the DLL at runtime with LoadLibrary(). For every export in the vendor's library that you need to call you would call GetProcAddress(). Armed with an address, you can call the vendor's functions through a pointer. You can even go so far as to build a "shim" DLL. That is a DLL which has the same number of exports, each with the same calling convention. Your code would call
these shimmed functions. It would be the job of the functions in the shim to either call the vendor's functions where his DLL is available or your
emulation.

Another option is, I think, to use a technique called "delayed loading".
With it, the complier and linker can be told to inhibit the loading of a DLL at process activation. At runtime you would need to determine whether the
vendor's DLL is available, and if so, just call it. At that point, the delay load mechanism would be used to load the DLL and obviate the need for calls through pointers. If not available, you would need to call your emulation.
If you want to pursue this route check the docs for the meaning of the
/DELAYLOAD option and search the MSDN-CD or at http://msdn.microsft.com for more info, especially the articles written by Matt Pietrek and John Robbins.
Regards,
Will

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
11
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
1
by: Chris LaJoie | last post by:
Hi, I have a question regarding the catching of popups in a separate window. I just can't get it to work. My browser is extremely simple and is designed for a single purpose: to open a 'netlet'...
1
by: varunhome | last post by:
Hi, I want to check for the absence of a string in regular expression. For example, if the string is "Error opening file: Permission denied. Aborting.", I want to check for absence of the string...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.