473,883 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1679
"Steve Terepin" <st***@terepin. freeserve.co.uk > wrote in message
news:#2******** *****@TK2MSFTNG P10.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******** ******@TK2MSFTN GP12.phx.gbl...
"Steve Terepin" <st***@terepin. freeserve.co.uk > wrote in message
news:#2******** *****@TK2MSFTNG P10.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
1740
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 Base_error("An object of incorrect type was given....");
11
1795
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
7839
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' to a particular VPN on the United Airlines network. The trouble is, I can't get my app to catch the popup (which is the netlet). The netlet opens outside in a separate IE window instead. I have a VB6 app that does what I want perfectly fine...
1
2764
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 "Permission". I have tried many possibilities without success. Thanks for any help, Varun Sud
7
2343
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 of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
12
6127
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 out of which unmanaged exception //get thrown
7
6398
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) == qaddrs+0x1000 and toks == "200": #producer write prod = int(toks, 16)
2
2371
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
18357
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
9796
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10859
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10420
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3239
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.