std::string vs char* DLL problem

This is a discussion on std::string vs char* DLL problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I made a DLL to easily create windows and handle messages. I tried to use std::string for the class ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    std::string vs char* DLL problem

    Hi, I made a DLL to easily create windows and handle messages. I tried to use std::string for the class and title name, but it causes a crash on the client application. I changed the string type from std::string to char* and everything worked good.
    Im confused here, what may be the problem?

    Thank you
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Don't forget to use the std::string member function c_str() when you are using it for something that requires a NULL terminated array of characters.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    I do use c_str() of course. This problem exists *ONLY* in a DLL project. In a normal win32 app it works.. Other ideas?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Not a clue, perhaps the folks in the Windows forum could help you better.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    k thanks.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    im not sure how to fix it but its something about using the stl in a DLL, look on msdn for how to get around it.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have noticed some issues when doing this myself. The best way to work around this problem is to either copy the string into a char array or to stick with char arrays and not use strings. The char array solution is best since if you absolutely need a string you can always make one using the char array.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    c_str() gives me invalid data sometimes, any idea on that too?

    I mean I convert a String to a Char using the c_str() function and it would give me something like "[][]|||^*" back, unless there is better ways off converting a String to a char* ?

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I've has similar problems with the vector template class.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    best bet is don't use stl objects across DLL's. Really you ought to make your DLL interfaces flexible enough to use in other languages so STL objects as parameters are obviously out.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    It's due to static members in the Dinkumware STL classes.

    http://support.microsoft.com/default...EN-US;Q172396&

    It might be fixed in VC.NET (I haven't tried), or you could use an alternative STL implementation.
    Joe

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    I see! I could use const char and char, but I am not thinking of only string. I need to use the vector template later too. What do you mean by other stl implementations?

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    JoeSixpack: Thanks for the info!!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    > best bet is don't use stl objects across DLL's.

    FillYourBrain is right. It is a very precarious to pass objects between a DLL & client application. This is because both object internals must match on a byte per byte basis between client & DLL, otherwise disasterous things happen. This is especially true if the DLL was written with one compiler & the client with another. But you will no doubt run into problems in anycase even if using the same compiler.

    Unless you want to get into pure virtual class interfaces and/or COM, I suggest you don't pass object between your client & DLL.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Problem creating dll with gcc/minGW
    By 6tr6tr in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2008, 03:19 PM
  3. Problem when works with DLL in BCC55
    By leiming in forum C Programming
    Replies: 0
    Last Post: 01-31-2008, 06:52 PM
  4. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:07 AM

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