DLL's and Maps

This is a discussion on DLL's and Maps within the C++ Programming forums, part of the General Programming Boards category; I have a DLL function that looks like this: Code: bool GetSupportedHeaders(map <string, string> &Me) { map <string, string> HeaderMap; ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    DLL's and Maps

    I have a DLL function that looks like this:

    Code:
    bool GetSupportedHeaders(map <string, string> &Me)
    {
    	map <string, string> HeaderMap;
    	HeaderMap["Host:"] = "Hello";
    	Me = HeaderMap;
    	return true;
    }
    Its job is to take a <string,string> map, populate it, and give it back to the function that called it.

    Now, in my main code I load and call the DLL like this:
    Code:
    typedef bool (*GETSUPPORTEDHEADERS) (map<string, string> &Me);
    GETSUPPORTEDHEADERS GetSupportedHeaders;
    HINSTANCE gSWEBS_headermapDLL;
    
    int main()
    {
    	map <string, string> SupportedHeaders;
    	
    	gSWEBS_headermapDLL = LoadLibrary(".\\SWEBS_headermap.dll");						// Load the library
    	if (gSWEBS_headermapDLL == NULL)
    	{
    		cout << "Could not load DLL";
    		return true;
    	}
    	cout << "Library loaded succesfully" << endl;
    	GetSupportedHeaders = (GETSUPPORTEDHEADERS) GetProcAddress(gSWEBS_headermapDLL, "GetSupportedHeaders");
    	if (GetSupportedHeaders == NULL)
    	{
    		cout << "Could not find GetSupportedHeaders() :(" << endl;
    		return true;
    	}
    	cout << "Set GetSupportHeaders()" << endl;
    	
    	GetSupportedHeaders( SupportedHeaders );
    
    	cout << "GetSupportHeaders() has returned." << endl;
    	
    	cout << SupportedHeaders["Host:"];
    	GetSupportedHeaders = NULL;
    	return false;
    }
    Which loads the library and is supposed to print what the DLL put into the map.

    When it runs I get this:
    Library loaded successfully
    Set GetSupportedHeaders()
    GetSupportedHeaders() has returned

    And then there are 2 access violations I am not too sure why, because my understanding of pointers isn't very good. Can somone please tell me whats going on and how I must fix it? Thanks

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I did, and it came up with something pointing to some code that looked to be in some sort of a template, probably from the STL. I doubt theres any problems there, does it look like theres something wrong with my code?

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    It runs perfectly when I have it all in the same code set. I wrote both programs at the same time so I know it was a clean build, and the only function in there was GetSupportedHeaders (and DLLEntryPoint or whatever they call it).

    I think it has something to do with accessing part of the map by its address as if it was a pointer (since I dont think you can make pointers to maps). I might try not using maps and simpy return a few structures and stuff, do it a different way. Stupid STL, stupid C++, stupid me *curses and mumbles*.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Isn't there a problem with allocating memory in a separate dll that is used in the main exe or another dll? I seem to remember reading about problems people have had trying to use stl containers in a dll because of the way the memory is allocated. That may be what is happening here.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Darn, thats probably the problem then. One thing I didn't really mention though is I get the error codes AFTER GetSupportedHeaders() returns, just before the program finishes. So it sounds like I haven't let some memory go or something.

    Ok, say I have a class in my main exe though, and I want some functions inside the DLL to access properties of that class, what should I do? Can anyone think of a quick and easy way around it?

    Edit: If I keep away from the STL though, that should be fine shouldn't it? Only problem is some of the properties of the class contain STL stuff, but the class itself isn't STL

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I looked a little harder and found this link:
    http://support.microsoft.com/default...b;en-us;172396 which I got from here:
    http://www.codeguru.com/forum/showth...hreadid=248922

    If that's your problem, those links might help you get around it.

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks for the link jlou!! Thats exactly my problem. Alright, can you help me out on this then? I think in my EXE file I am supossed to give the DLL the address of a function IN the executable so that it can be used to access the map? Is that it?

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    To be honest, I don't know the answer. I have never come across this problem specifically, I only remembered reading about it. All STL container data I use is contained completely within each DLL, which deal with all the object creation and destruction on their own.

    Maybe somebody else here is familiar with this problem and can help you better. I'd also suggest continuing to search for this topic, since it seems that it is somewhat common. Maybe you will stumble across more and/or better info.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21