[RESOLVED] Making dll with one of the Codeguru articles
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Making dll with one of the Codeguru articles

  1. #1
    Join Date
    May 2012
    Posts
    22

    [RESOLVED] Making dll with one of the Codeguru articles

    Hi!
    I make a dll and its using by this tutorial. I make absolutely the same files with the same code. Ok. It works just fine. Now I add my function with #include vector, using namespace std and fstream of course:

    Code:
    DECLDIR void toFileWrite(vector<int> v, const char *fName)
    {
        ofstream inf;
    
        inf.open(fName,inf.trunc);      
        if (inf!=NULL)
        {
    
            for (int i = 0; i < v.size(); i=i+2)
            {       
                inf << v.at(i)<<"\t"<<v.at(i+1)<<"\n";
            }   
        }       
        inf.close();        
    }
    Building a release causes no problems. Then, exactly by the author model, I add using of my function in my application.

    But when I call it

    Code:
    vector<int> a(4,100);
    _toFileWriteFunc(a,"hey.txt");
    I got error window with text: "Unhandled exception at0x7855db75 in a.exe: Access violation reading location 0x2982d311" VS opens cpp-file of my dll and points with green arrow to line: if (inf!=NULL)

    So, what the author didn't mention, and what I should do to make such a simple function work properly?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,008

    Re: Making dll with one of the Codeguru articles

    Quote Originally Posted by 330xi View Post
    But when I call it

    Code:
    vector<int> a(4,100);
    _toFileWriteFunc(a,"hey.txt");
    I got error window with text: "Unhandled exception at0x7855db75 in a.exe: Access violation reading location 0x2982d311" VS opens cpp-file of my dll and points with green arrow to line: if (inf!=NULL)
    What is _toFileWriteFunc? Where and how is it defined?
    Victor Nijegorodov

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

    Re: Making dll with one of the Codeguru articles

    Quote Originally Posted by 330xi View Post
    Hi!
    I make a dll and its using by this tutorial. I make absolutely the same files with the same code. Ok. It works just fine. Now I add my function with #include vector, using namespace std and fstream of course:
    You should not be passing vectors and any objects such as this acrosss DLL boundaries. You cannot guarantee that

    1) the vector that your application uses is exactly the same one that exists in the DLL (and I mean exactly the same one, down to the byte level).

    2) The vector that your application uses is using the exact same heap manager internally as the one in the DLL.

    If the error wasn't caused by the vector, it still doesn't matter -- you would have run into it anyway as soon as you try and do anything with vector. The reason for the error would be more than likely caused by using different vector classes (the public interface is the same, but the internals are different), or that your application uses one heap, the DLL uses another heap, and you're trying to manipulate the vector in some way that causes the wrong heap manager to be used.

    Look at the tutorial -- you see simple, integral types passed and returned (BOOL, DWORD, pointers to these types, etc.). You don't see things such as vectors, list, streams, etc. being passed. There is a reason for this, as I've pointed out above.

    Also on a side note, you should be passing objects by reference or const reference, not by value. Why are you passing vector by value? You're invoking the call to the copy constructor when you pass objects by value, and this is probably where everything falls apart. Even so, don't pass vectors or any "complex" C++ type (including your own classes) unless you can make the guarantee that the internals of the class are exactly the same, and the DLL and the application is using the same heap manager.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 11th, 2012 at 02:40 PM.

  4. #4
    Join Date
    May 2012
    Posts
    22

    Re: Making dll with one of the Codeguru articles

    Oh, thanks a lot for such detailed answer, Paul! It really helped. At least directed me for many other interesting articles. As for my application and dll I have reorganised them in the other way, with no such objects passing, with no problems at all.

  5. #5
    Join Date
    May 2012
    Posts
    22

    Re: Making dll with one of the Codeguru articles

    Quote Originally Posted by VictorN View Post
    What is _toFileWriteFunc? Where and how is it defined?
    in different places of code:

    Code:
    typedef void (*toFileWrite)(vector<int>, const char *);
    [...]
    toFileWrite _toFileWriteFunc;
    [...]
    _toFileWriteFunc = (toFileWrite)GetProcAddress(hInstLibrary, "toFileWrite");

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center