?
Solved

STL in class library project (DLL)

Posted on 2004-11-28
17
Medium Priority
?
9,641 Views
Last Modified: 2008-01-09
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).


0
Comment
Question by:anthon007r
  • 9
  • 5
  • 3
17 Comments
 
LVL 19

Assisted Solution

by:drichards
drichards earned 1020 total points
ID: 12691543
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12693009
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
 
LVL 6

Assisted Solution

by:Svetlin_Panayotov
Svetlin_Panayotov earned 980 total points
ID: 12694058
Try to add
MSVCRTD.LIB and LIBC.LIB in Linker->Input
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:anthon007r
ID: 12694248
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
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12694597
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
 
LVL 6

Assisted Solution

by:Svetlin_Panayotov
Svetlin_Panayotov earned 980 total points
ID: 12694607
Oh...sorry - forgot to mention that - adding oldnames.lib in Linker->Input should work too
0
 
LVL 1

Author Comment

by:anthon007r
ID: 12695016
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12695182
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12695295
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12695349
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12695385
i forgot to mention.. the line that generates the error in the 9th comment above is in IOS header.
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12695593
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12696230
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
 
LVL 19

Accepted Solution

by:
drichards earned 1020 total points
ID: 12697630
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
 
LVL 19

Assisted Solution

by:drichards
drichards earned 1020 total points
ID: 12697776
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
 
LVL 1

Author Comment

by:anthon007r
ID: 12697978
This IS the question i asked im most satisfied with the answers! May I know your secrets? LOL im really impressed =)
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12699551
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question