STL in class library project (DLL)

Does anyone had tried to include ANY of the following lines in their C++.net class library project:

#include <bitset>
#include <queue>
#include <fstream>
#include <iostream>

im getting LNK2020 errors
(error LNK2020: unresolved token(0A000006) _CxxThrowException)
(error LNK2020: unresolved token(0A000015) delete)

Im using VS .net 2003 with .net 1.1 framework and Winxp with sp2 RC1.

The story behind:
---------------------
Actually, i don't get any error when I included those lines IF im going to create a C++.net windows application. And because FolderBrowseDialog don't seem to work with C++ .net, I wanted to create a DLL so that my GUI is C# or VB.net based and my architecture is lying behind a DLL.. because in that VB.net and C#, FolderBrowseDialog works just fine. I really wonder why. Could anyone answer why? The FolderBrowserDialog doesn't seem to display correctly in C++.net but works just fine in other languages (e.g. it doesn't shows up the treeview).


LVL 1
anthon007rAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

drichardsCommented:
A quick Google search shows that this is a defect in the FolderBrowserDialog.

"It's another fun bug with the FolderBrowserDialog (a bug that currently has no fix). In v1.1 of the .NET framework, if the thread apartment is not in STA mode, the FolderBrowserDialog will display only without a directory tree to select a folder from. This confused a lot of developers so in v1.1 SP 1 and v2.0 Beta an exception is thrown instead (which makes it nice and difficult for us developers to manage because it is not consistent).

This all stems from the fact that you can't set the apartment state in a C++ .NET application (for complex reasons)."

If you have 100% managed C++ code, it will actually work OK. You can work around it by wrapping "SHBrowseForFolder" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shbrowseforfolder.asp) instead.  Note in the docs that it must be run in an STA thread as well - you can call CoInitializeEx in this case and it wil work.

Not sure about the unresolved tokens.  I haven't seen that before.  Have you set the runtime library settings correctly?
0
anthon007rAuthor Commented:
Runtime library settings?  What specific settings I should configure?
What I did is I created a class library project under C++ and then under any header file, I tried to include those lines and the errors started to appear.
Note that i didn't do anything yet to the project except I added one of those lines.
I also tried to create a windows form application under C++ then tried to include the header files but there's no any errors. What's wrong? Is STL not allowed in a class library project? (O man, my architecture depends heavily on STL and file stream.. :( )

BTW, thanks drichards for some suggestions about the issue on browseFolderDialog. Is the wrapper a COM based component? CoInitializeEx seems familiar.
0
Svetlin_PanayotovCommented:
Try to add
MSVCRTD.LIB and LIBC.LIB in Linker->Input
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

anthon007rAuthor Commented:
Great man! That does the job! But actually, there's still a problem.. try this:

1. create a class library project under C++
2. do what you told me (Svetlin Panayotov)
3. create a C++ generic class

#pragma once
#include "string.h"
class sample
{
public:
      sample(void){}
      ~sample(void){}

      void myFunc(char* input)
      {
            input = input;
            input = strrev(input);
      }
};

4. use the new class' function in your main class

and that will lead you to:

1: "error LNK2001: unresolved exeternal symbol "char * __cdecl strrev(char*)"(?strrev@@$$J0YAPADPAD@Z)
2: "fatal error LNK1120: 1 unresolved externals"

Thanks Svetin_Payayotov, your information really helps alot =)

But what about those error? should i include another LIB-file in linker->input?
0
Svetlin_PanayotovCommented:
If you look in string.h where strrev() is defined you'll see something like:

#if     !__STDC__

/* prototypes for oldnames.lib functions */
_CRTIMP void * __cdecl memccpy(void *, const void *, int, size_t);
....
....
_CRTIMP char * __cdecl strrev(char *);
        char * __cdecl strset(char *, int);
_CRTIMP char * __cdecl strupr(char *);

#endif  /* !__STDC__ */


And just few lines above:
_strrev(char *)

Calling _strrev() instead strrev() should work fine.

Svetlin
0
Svetlin_PanayotovCommented:
Oh...sorry - forgot to mention that - adding oldnames.lib in Linker->Input should work too
0
anthon007rAuthor Commented:
O man, you're really an expert, that does the job! How could you know all of that stuff, which and what to include in linker->input?
Could you please give me some tables? =) please.
Thanks!

ei.. would this warnings be harmful?

msvcrtd.lib(secchk.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(xlock.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(cout.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(iosptrs.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
WArchiver.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
libcpmtd.lib(locale0.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
nochkclr.obj : warning LNK4099: PDB 'libc.pdb' was not found with 'C:\Software\SoftDev\Microsoft Visual Studio .NET 2003\Vc7\lib\nochkclr.obj' or at 'C:\Anton\Archiver\WArchiver\Debug\libc.pdb'; linking object as if no debug info

Thanks again Svetlin_Panayotov
0
anthon007rAuthor Commented:
now i changed the points value, because im chaining some related questions.

Now I've successfully compiled and built the DLL that contains my architecture with a lot of STL.

But a new problem now arises, if I tried to add my DLL as a reference and tried to call some functions that make use of STL, im getting a very deep error from istream and ios:

"An unhandled exception of type 'System.NullReferrenceException' occured in warchiver.dll

Additional information: Object reference not set to an instance of an object"

0
anthon007rAuthor Commented:
To be more detailed, i got those error and the error points to "_Init();"

void init(_Mysb *_Strbuf = 0,
            bool _Isstd = false)
            {      // initialize with stream buffer pointer
            _Init();      // initialize ios_base
            _Mystrbuf = _Strbuf;
            _Tiestr = 0;
            _Fillch = widen(' ');

            if (_Mystrbuf == 0)
                  setstate(badbit);

            if (_Isstd)
                  _Addstd();      // special handling for standard streams
            else
                  _Stdstr = 0;
            }

In my class library DLL project, i only initiatized a fstream variable to isolate the problem, and it seems the declaration of sampleObj below triggers the problem, and when you try to "step-into" during debugging, you'll be pointed in the code i mentioned above.

#include "fstream"
using namespace std;
using namespace System;

namespace myNamespace
{
   public __gc class  myClass()
   {
        public:
           void myFunc()
           {
                fstream sampleObj;
           }
   }

}

Please help. Thanks.

0
anthon007rAuthor Commented:
i forgot, I got those errors when I tried to call a function in my DLL class library in my C# Windows application project.
The function I called from my DLL class library instantiated a fstream class which triggers the error.
0
anthon007rAuthor Commented:
i forgot to mention.. the line that generates the error in the 9th comment above is in IOS header.
0
Svetlin_PanayotovCommented:
Haven't seen soo many mails later :)
Now - one by one - I don't have a table...you just have to know this stuff...it comes with the time :)
The warnings should not be harmful.
About your last problem - I don't know what went wrong.I even created test library in managed c++ and c# app that calls it - but nothing wrong with fstream.The constructor works fine - writing to file too.
Maybe you need to take a closer look at your code (or do the same - just create test lib and app).
Or...maybe you need some sleep? ;)

Svetlin
0
anthon007rAuthor Commented:
yup i did create a sample test lib.. and it didn't worked.. maybe you could post here what you did?
Here are things what i did:

1. I created a class library project named SampleCL
2. I edited the SampleCL.h to:

#pragma once

#include "fstream" // added by me

using namespace std; // added by me
using namespace System;

namespace SampleCL
{
      public __gc class Class1
      {
      public: // added by me
            Class1() // added by me
            {
                  fstream fileIO;
            }
      };
}

3. in all configuration in linker->input i added MSVCRTD.LIB
4. built the DLL and that produced the SampleCL.dll
5. Create a new Windows Application in C#.net
6. I add SampleCL.dll as a reference
7. I dragged a button
8. Double clicked it and edited the eventhanlder to this:

            private void button1_Click(object sender, System.EventArgs e)
            {
                  SampleCL.Class1 obj = new SampleCL.Class1();
            }

9. Running the winapp and clicking the button1 will produce the error..

i dunno why.. its just a simple application.. im getting desperate.. please help
0
drichardsCommented:
Most likely you selected the incorrect runtime library.  Since you used "msvcrtd.lib" in the linker step (you should remoce libc.lib if you used that one too), go to the C++ Code Generation property page and change the "Runtime Library" setting from "Multi-threaded Debug" to "Multi-threaded Debug DLL".  That should get things working properly.  You need to be consistent with the comlile and link settings.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drichardsCommented:
Another note - you should use "msvcrt.lib" and "Multi-threaded DLL" for the release build.  The previous settings were for debug build.

And some more background:

By default, managed C++ library projects do not include C runtime linkage.  Here are the relevant references you need to choose the correct settings for the library you want to use.  You can see that libc and msvcrt are two different libraries and should not be used together.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_c_run.2d.time_libraries.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_.2f.MD.2c_2f.ML.2c_2f.MT.2c_2f.LD.asp
0
anthon007rAuthor Commented:
This IS the question i asked im most satisfied with the answers! May I know your secrets? LOL im really impressed =)
0
Svetlin_PanayotovCommented:
Well...thanks - I'm happy to help :)
And about the secrets - you need - PC, good books and a lot of cigarettes & coffee ;)
Happy coding

Svetlin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.