Solved

LoadLibrary fails

Posted on 2000-05-09
13
1,420 Views
Last Modified: 2013-12-03
I have a .EXE which depends on DLL A.  The .EXE uses LoadLibrary to load DLL B, which also depends on DLL A.  The LoadLibrary call on DLL B failes, reporting "invalid access to memory location" in its static initializer.  The static initializer for DLL B is using static data from DLL A, and fails whether trying to assign to that static data or invoke a method which likely also modifies the static data.

This application is *not* using MFC or MFC shared libs.

 How can I fix this?  What's causing the memory fault?
0
Comment
Question by:twall
  • 7
  • 6
13 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 2794492
So you export the data, not functions only, from the A?
Could you show the code related to the static data?
0
 
LVL 1

Author Comment

by:twall
ID: 2794550
Yes, both data and functions are exported from A.

Here's the static declaration, from DLL A:

namespace NS { vector<CORBA::Narrow_proto> * BasicType::_narrow_helpers; };

Here's the access, from DLL B:

struct _global_init_MYPLUGIN_DerivedType {
  _global_init_MYPLUGIN_DerivedType()
  {
    vector<CORBA::Narrow_proto>* vec = ::NS::BasicType::_narrow_helpers;
    if( vec == NULL )
        vec = ::NS::BasicType::_narrow_helpers = new vector<CORBA::Narrow_proto>;
    vec->push_back( MYPLUGIN::DerivedType::_narrow_helper2 );
  }
} __global_init_MYPLUGIN_DerivedType;
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2794593
Probably it would be easier to use the function thunks, but not to export the data directly.

Like this

A:
 
__declspec(dllexport) void SetNarrowHelpers(p)
{
   ::NS::BasicType::_narrow_helpers=p;
}

__declspec(dllexport) HelperType GetNarrowHelpers(p)
{
   return ::NS::BasicType::_narrow_helpers;
}
0
 
LVL 1

Author Comment

by:twall
ID: 2794609
Maybe, except that the code is auto-generated (by the CORBA IDL compiler).  Is there something inherently wrong with data access from DLL to DLL?  Obviously the data access works from .EXE to DLL.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2794705
Hard to say. Probably, the problem is as follows.

The .h file [may be] declares the static data differently for EXE (as imported) and DLL (as exported).

It is correct as long as you use this header in the EXE and in the DLL that exports the data (A).

But for B.DLL, the data must be declared as IMPORTED as well, but not as exported (the header file may detect that it is compiled in DLL mode and make the data exported).
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2794716
BTW, do you think that it's twice easier to answer your question in this area than in the C++ one?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:twall
ID: 2794733
Adjusted points from 100 to 200
0
 
LVL 1

Author Comment

by:twall
ID: 2794787
We are generating .DEF files that indicate all the exports. DLL B gets its class definitions from the original class header file, which has no declspecs in it.
0
 
LVL 1

Author Comment

by:twall
ID: 2794811
Wrapping the static data in static functions results in the same memory fault when trying to set the vector.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2795165
What is the .DEF file you use?
0
 
LVL 15

Accepted Solution

by:
NickRepin earned 200 total points
ID: 2795177
0
 
LVL 1

Author Comment

by:twall
ID: 2797181
And the answer is:

The .DEF file *does not* require "DATA" after all the data members being exported.

DLL B *does* require that the headers that it uses for DLL A use __declspec(dllimport) in order to access the data correctly.

Thanks, Nick.

0
 
LVL 1

Author Comment

by:twall
ID: 2797212
The second reference indicates part of what is necessary.

DATA is not required in the .DEF file.
__declspec(dllimport) *is* required in the headers from DLL A which are used by DLL B.

Thank you Nick.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

760 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

19 Experts available now in Live!

Get 1:1 Help Now