dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Vector in a DLL

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Location
    Germany
    Posts
    24

    Vector in a DLL

    Hi there! I want to create a DLL for later use in an application.

    The DLL must contain a vector.

    (my ultimate goal is to create multiple instances of an object-class. Say: AllTeams>team1,team2, team3 etc. -- In Visual Basic (where I grew up with) this would be done by using a collection. I just mention this trying to explain my issue..)

    I use Visual C++ 2008 Express.

    My approach is:

    1. I create a File>New Project -> Win32 > Win32Project->TypeLL + Export Symbols ( I named
    all this StaticLD_2.sln

    2. In Project Settings under StaticLD_2 Properties > C/C++>Extended>Calling COnvention I place __stdcall (/Gz) (insted of /Gd) ... doing try and error to prevent a metadata issue..

    3. I add minor code to the automatically generated code by the wizard. Under TODO you will now find:
    int MyVar;
    vector<int> MyVect;
    Entire code, see below:

    4. StaticLD_2.h:

    #ifdef STATICLD_2_EXPORTS
    #define STATICLD_2_API __declspec(dllexport)
    #else
    #define STATICLD_2_API __declspec(dllimport)
    #endif

    #include <vector>
    using namespace std;

    // Diese Klasse wird aus StaticLD_2.dll exportiert.
    class STATICLD_2_API CStaticLD_2
    {
    public:
    CStaticLD_2(){
    }
    // TODO: Hier die Methoden hinzufügen.
    int MyVar;
    vector<int> MyVect;

    };

    extern STATICLD_2_API int nStaticLD_2;

    extern STATICLD_2_API int fnStaticLD_2(int Multiplyer);


    5. The above copiles well and you wned up with StaticLD_2.DLL and *.LIB.

    6. I now create a new console project to test the DLL. I also copy the dll and lib created in step 5 into the project folder of the new application project. The new project is called StaticLD_App_2.sln

    7. StaticLD_App_2.cpp :

    #include "stdafx.h"
    #include "D:\rc_Sept07\CPP_Express\tr\StaticLD_2\StaticLD_2\StaticLD_2.h"


    int _tmain(int argc, _TCHAR* argv[])
    {
    int fortytwo=nStaticLD_2;
    int EightyFour=fnStaticLD_2(2);

    CStaticLD_2 MyClass;
    MyClass.MyVar=5;

    int MyItem=111;
    MyClass.MyVect.push_back(MyItem);

    return 0;
    }


    8. Here comes the PROBLEM:
    When running the code using Debugger, it copiles will, I run into an error when approaching line marked with ***:
    int MyItem=111;
    *** MyClass.MyVect.push_back(MyItem);

    The error box is Titled with: "Debug Assertion failed"


    9. From what I vaguely learned : using vectors in a DLL is tricky. My know is too rudimentary to understand the reasons so I have no ide ahow to overcome this issue.

    10. Remember my real goal: I want to create a collection structure. I try to do this using a vector. Finally, the vectors will not be defined through vector<int> but rather throu vector<MyObectClass>. However, I must undertand doing it with integers first.

    11. Can someone help me get things right?

    12. I attach the code. You may need to adjust the path of the include statment in the application project.

    Ciao,
    codecheater

  2. #2
    Join Date
    Mar 2010
    Location
    Germany
    Posts
    24

    Re: Vector in a DLL

    here comes the project. It produces a "Debug Assertion failed" error
    Attached Files Attached Files

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vector in a DLL

    Quote Originally Posted by codecheater View Post
    The error box is Titled with: "Debug Assertion failed"

    9. From what I vaguely learned : using vectors in a DLL is tricky. My know is too rudimentary to understand the reasons so I have no ide ahow to overcome this issue.
    It isn't just vector -- any data structure where you are allocating memory in one module, and changing it in another module, is dangerous.

    The reason is that the heap manager in the DLL is not the same as the heap manager in the application. Not only that, you have to make sure that the DLL is compiled with exactly the same settings (optimizations, etc.) as the application, otherwise the internals of "vector" is different in the DLL and the application.

    So you have to make sure that you

    1) use the DLL version of the runtime library of the application and DLL.

    2) You compile the DLL with the same settings you're compiling your application.

    3) You use the same version of the compiler for both creation of the DLL and the application.
    10. Remember my real goal: I want to create a collection structure. I try to do this using a vector. Finally, the vectors will not be defined through vector<int> but rather throu vector<MyObectClass>. However, I must undertand doing it with integers first.
    Why expose the internals to the main app this way? What if you decide to use a deque later on, or a std::list? You have to change the DLL, and any application that uses the DLL.

    Instead, you expose or export an "API" that the app uses to add, remove, search, whatever, the data in your data structure. The app need not know that the data is in a vector, set, map, deque, list, or your own made up data structure.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2010
    Location
    Germany
    Posts
    24

    Re: Vector in a DLL

    Hello Paul,

    your concerns about using a vector sound convincing to me. I did not see those points before. Now, I realize that my original approach would mean trouble on the future road.

    So you suggest to take an entirely different approach.

    Quote:
    [ Instead, you expose or export an "API" that the app uses to add, remove, search, whatever, the data in your data structure. ]

    Is my understanding correct:
    - In my initial approach I did expose an API to the main application. But that API included a vector. Which means trouble.
    - Now, you suggest to ommit the vector from that API.
    - Do you suggest to introduce the vector only inside of the main application?

    Or am I walking into the wrong direction?

    Kind Regards,
    Ralph

  5. #5
    Join Date
    Mar 2010
    Location
    Germany
    Posts
    24

    Re: Vector in a DLL

    Paul, I really appreciate your comments yesterday. I also realize that due to my limited c++ skills I may not be capable of keeping up a constructive dialogue. Therefore, below, I am now rather asking for some "help for help". But before, let me illustrate my goal using some tree-view:

    Say, I want to calculate how many players in a football league wear red shirts.
    I would create a Class FootballLeague as follows:


    Class FootballLeage
    !
    + Teams
    ! !
    ! +Team A
    ! ! -Town
    ! ! - Colour_of_Shirt
    ! ! - Nr_Players
    ! !
    ! +Team B
    ! ! - Town
    ! ! - Colour_of_Shirt
    ! ! - Nr_Players
    ! !
    ! +Team C
    ! ! - Town
    ! ! - Colour_of_Shirt
    ! ! - Nr_Players
    !
    + Function_Count_Players ("Red")


    Note that each team has an attribute "colour of shirt". Finally, the function can loop through all teams, identify the shirt colours and if a match is given, it can multiply by the number of players and doing the summation. (my real project deals with thermodynamics but I try to bring it up into an intuitive arena)

    I can easily do all this in one single C++ project. My skills are sufficient for that and it does work.

    However, I rather prefer to create a DLL for the class Football League.
    And then make use of the DLL in a main application. In the main application I would create the teams and "plug" them into to the Teams Collection. Sort of by saying:
    MyFootballLeage.Teams.Team[1].Town="Munich" etc. (..sloppy syntax)
    The main application would be a c++ Windows Forms application.
    Or, ultimately, it would be Excel and the entire DLL would become a COM class. (but let me skip the COM issue for now... it will make work later on I already foresee..)

    ( Oh.. once I get the knack, I would also create a collection of players for each team. Each player would come with his age, his nationality, number of goals scored, red and yellow cards etc. Another function (of the class "team" would then compute the team's average age etc. - I think this would follow the same pattern that applies to the initial request. )

    According to my initial post, my early approach was to create a vector of teams within the DLL.
    My idea was that the DLL would hold a self-containing class which comes with the object-vector and with its own methods.

    I know how to do that using Visual Basic. You may also notice that my thinking is probably heavily geared towards VB. Using C++ I must probably change my thinking and my habits. May I ask for your advice on where I can learn the required tools/strategies using C++ to finally achieve the above described goal ? Is it feasible to get there with a reasonable amount of self-tutoring?

    All that sounds pretty standard to me. Are there eventually self-teaching samples that would show me the way? Or: if I did a google search which keywords may take me there? (probably the "vector" idea lead me into a wrong direction. Must I better search for alternative c++ categories?

    Best Regards,
    R
    Last edited by codecheater; March 15th, 2010 at 08:54 AM. Reason: fixing a typo or two.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)