Solved

std::map and DLL functions

Posted on 2002-03-20
8
492 Views
Last Modified: 2013-12-14
When using std::map as an argument passed to a dll, I always get an access violation. I've now resorted to the simplest of programs, but still can't work it out... Here's a snippet:

/* ========= .dll file =============== */
#include <map>
#include <string>

using namespace std;

typedef map< int, int > Map;

extern "C" __declspec( dllexport ) void Test( Map & m )
{
//keep getting access violations as soon as I try to use m
     int i = m[1];
     int j = m[2];
}
/* ======== .exe file =================== */
The .exe file is a very simple main() which calls Test() with a map it creates. I get the access violation whether I link dynamically using "LoadLibrary()" or statically by linking with the .lib.

Any comments will be greatly appreciated.
Tom

0
Comment
Question by:Tom_P
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:lidorc
ID: 6882306
You pass a reference to a C function..
Pass a pointer instead
0
 
LVL 1

Expert Comment

by:lidorc
ID: 6882309
In other words:
not

extern "C" __declspec( dllexport ) void Test( Map & m )

but

extern "C" __declspec( dllexport ) void Test( Map * m )
0
 

Author Comment

by:Tom_P
ID: 6882350
lidorc,

thanks for the interest but I think you will find that 'extern "C" ' is just a linkage convention. It tells the compiler to use the "C" name mangling, as oposed to the "C++" style.

Tom
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 6882403
Try to set the CRT to be used as "multithreaded DLL" for both projects ( DLL&EXE) under "Project Settings | C++ | Code Generation | Use Runtime Library..."
0
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 50 total points
ID: 6883560
PRB: Access Violation When Accessing STL Object in DLL (Q172396)
http://support.microsoft.com/support/kb/articles/Q172/3/96.ASP 

HOWTO: Exporting STL Components Inside & Outside of a Class (Q168958)
http://support.microsoft.com/support/kb/articles/Q168/9/58.ASP 


0
 
LVL 2

Expert Comment

by:antoinebf
ID: 6884090
If it can help:
I had the same problem... Simply put (very simply put), with no specific settings or changes as proposed in the previous comment, I think it's because map have entity (dll/exe) specific "end" iterator. So if you pass a map (or list, multimap, any such container but a vector) from a dll to an exe (or another dll), the latter won't be able to recognize the end of the sequence, therefor causing the access violation.

The vector is ok because it's a sequential memory container (not node based). It can be used as pointer params (type*).

A simple way to bypass this is to convert the map to a vector of pair, pass the vector from a dll to another, and reconvert the vector to a map. Of course there is a lot of overhead, but it's better than a crash! :) Or only use vector, if possible!
Another solution is the exports proposed in the previous comment (I briefly tried, but got confused and bailed out...).

In MS visual studio 7 (.net) they should have a new implemation of STL that will not have this anoying limitation... but I have not had the chance to use it yet...

Hope this will help.

A>
0
 

Author Comment

by:Tom_P
ID: 6888370
Thanks very much everyone for your interest. The problem is now resolved.
Tom
0
 

Expert Comment

by:wyzegg
ID: 8207955
Tom:
I have encountered  a same problem, my function define is:


extern "C" __declspec( dllexport ) void Test( vector<int> & m )
{
    m.push_back(10);
}

I try to get the vector in my executable program.
in
http://beta.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20253705.html


-- Dan said

"
The only STL container that can currently be exported is vector.
"

-- Dan

Thanks very much everyone for your interest. The problem is now resolved.
Tom
Tom please tell me how you resolved your problem.
thank you!



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

726 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