Visual C++ Community FAQ 
Author Message
 Visual C++ Community FAQ

            V I S U A L   C + +   C O M M U N I T Y   F A Q
            ===============================================

This is the FAQ for common questions in the Visual C++ community. This
document is not intended as a tutorial or reference guide, nor is it
documentation for Visual C++. This document was created to help address
the frequent problems, questions, and curiosities of Visual C++ users.

Questions and answers are supplied by those who participate in the
Visual C++ community. When possible, credit is given to the person
contributing the answer. If you have a question and answer that you feel

can also send mail there if you have a correction to an existing
question.

This FAQ is provided "As Is" with no warranties, and confers no
rights.  Use of any included samples are subject to the terms specified
at < http://www.*-*-*.com/ ;.  For information on

=========================
CONTENTS
=========================

1. PRODUCT INFORMATION
   1.1. What other FAQs are available for Visual C++?
   1.2. What are some frequent terms used by the Visual C++ community?
   1.3. What are the plans for the next version of Visual C++?
   1.4. How do I report a bug in Visual C++?

2. C++ LANGUAGE SUPPORT
   2.1. What C++ standard features does Visual C++ .NET not support?
   2.2. What is the for loop scoping behavior in Visual C++ .NET?

3. MANAGED EXTENSIONS TO C++
   3.1. Is there an equivalent of the C# 'is' keyword in MC++?
   3.2. How do you pass a managed array or string to a native function?
   3.3. How do you declare an array parameter as a 'ref' argument
        to a function?
   3.4. Does MC++ have equivalent accessibility specifiers to C#?
   3.5. How do you represent a C# foreach statement in MC++?
   3.6. Can the Visual C++ compiler create a .netmodule file?
   3.7. How can you write a C# ref param or C# out param in C++?
   3.8. How can you detect at compile-time if code is compiled as
        managed?

4. COMPILER, OPTIMIZER, AND LINKER
   4.1. Does Visual C++ .NET still support native code?
   4.2. What products are built with Visual C++ .NET?
   4.3. Where is the profiler in Visual Studio .NET?
   4.4. Is Visual C++ available for the IA-64 architecture?
   4.5. How do compiler security checks work? Should I use them?

5. PROJECT SYSTEM, DE{*filter*}
   5.1. How do I create a project for a native console application?
   5.2. Where is the 'export make file' feature in Visual Studio .NET?
   5.3 How do I rename a file in a C++ project?

6. LIBRARIES, CRT, AND STL
   6.1. What is the difference between msvcrt.dll and msvcr70.dll?
   6.2. What problems exist if an application uses both msvcrt.dll and
        msvcr70.dll?

A. More Information
   A.1. Contributors
   A.2. Posting Locations

=========================
PRODUCT INFORMATION
=========================

1.1 What other FAQs are available for Visual C++?
    The Microsoft most valuable professionals (MVP) have organized an
    extensive FAQ available at < http://www.*-*-*.com/ ;.

    Tomas Restrepo has started an FAQ focused on Managed C++ at
    < http://www.*-*-*.com/ ;.

1.2 What are some frequent terms used by the Visual C++ community?
    * Native code is the traditional model where a C++ program is
      compiled down to machine code.
    * Managed code is a C++ program compiled with the /clr switch. The
      executable contains MSIL consumed by the JIT in the common
      language runtime.
    * Managed data are classes that use the managed extensions to C++ to
      specify garbage collected or value type semantics.
    * MC++ is shorthand for managed extensions to C++.
    * The compiler front-end deals with the language (parsing and type
      checking). The compiler backend deals with code-generation and
      optimization.
    * VC7 is shorthand for Visual C++ .NET.
    * To RAID a bug means that Microsoft has entered the bug report in
      its bug tracking system.

1.3 What are the plans for the next version of Visual C++?
    The next release of Visual C++ is code named "Everett". More details
    about future versions of Visual Studio are available here:
      < http://www.*-*-*.com/ ;
    An excerpt from this linked article says, "In Visual Studio
    'Everett' Visual C++ will be in the upper 90-percent range for
    ANSI/ISO compatibility."  In various forums, the "Everett" release
    is often informally called "7.1".

1.4 How do I report a bug in Visual C++?
    First, are you sure it is a bug? It could be a limitation of a
    feature. Once you have found a bug, the most important thing to do
    is create a clear repro case. If it involves a compiler bug, source
    code that reproduces the problem is necessary. Without, the product
    team can neither fix the bug nor can the product team create a
    regression test to keep the bug from reappearing.

    With the repro case available, you have several options. The
    quickest thing to do is file a bug at
      < http://www.*-*-*.com/ ;

    Sometimes readers of various newsgroups can help you find
    workarounds to your problems. Also, make sure to look at the
    "Technical Support" menu item in the Help menu of Visual
    Studio. Lastly, it is worthwhile looking up the help article from
    the index of MSDN titled "feedback for Visual C++ documentation",
    which lists a number of mailboxes you can write to about flaws in
    the documentation and the product.

=========================
C++ LANGUAGE SUPPORT
=========================

2.1 What C++ standard features does Visual C++ .NET not support?
    A complete list is available in the MSDN documentation in an article
    named "Standard Compliance Issues in Visual C++". Here is a link
    < http://www.*-*-*.com/ ;.

    The following is an overview of the compliance issues:
     * Compiler limits are in some instances less than required by the
       standard
     * Some alternative keyword representations are not supported
     * Koenig lookup is only supported for operators
     * Scope of name declarations in selection statements (see FAQ 2.2)
     * The export keyword is not supported
     * Template template arguments are not supported
     * Out-of-class template definitions is not supported
     * Partial template specialization is not supported
     * Exception specifications are not enforced

2.2 What is the for loop scoping behavior in Visual C++ .NET?
    For 7.0 we tried to strike a balance here and I think the default
    7.0 for scoping behavior is quite reasonable as in most cases it
    lets old code work as well as allowing most standard conformant code
    using for-loops to work properly.

    The behavior by default in VC7 is designed to try to avoid breaking
    existing code while letting most conformant cases work.  The #pragma
    conform(forScope, off) behavior is such that the declaration in the
    for-init-statement is "weakly" injected into the outer scope such
    that it is not an error to have another variable declared in the
    outer scope, but that there are instead some warnings used.

    So, under a compiler working under the ARM rules for
    for-init-statements like VC6:

        void foo()
        {
          for(int i=0;i<10;++i)
          {
            //...
          }
          some_global[i] = 10;
          for(int i=0;i<10;++i) //error a variable 'i' is already
          {                     //defined in this scope
            //...
          }
        }

    Under a compiler conforming to the standard for for-init-statements
    like VC7 with -Zc:forScope/#pragma conform(forScope, on):

        void foo()
        {
          for(int i=0;i<10;++i)
          {
            //...
          }
          some_global[i] = 10; //error 'i' is not in scope
          for(int i=0;i<10;++i) //ok
          {
            //...
          }
        }

    Under VC7 with -Zc:forScope-/#pragma conform(forScope, off):

        void foo()
        {
          for(int i=0;i<10;++i)
          {
            //...
          }
          some_global[i] =10; //ok, use 'i' introduced in the first loop
          for(int i=0;i<10;++i) //ok, hides the 'i' introduced in the
          {                     //first loop
            //...
          }
        }

=========================
MANAGED EXTENSIONS TO C++
=========================

3.1 Is there an equivalent of the C# 'is' keyword in MC++?
    The workaround is using dynamic_cast to test the type. One way of
    wrapping this in an easier to use package is this:

        template <typename T1, typename T2>
        inline bool istypeof ( T2* t )
        {
          return (dynamic_cast<T1*>(t) != 0);
        }

    So now you can do something like this:

        if ( istypeof<System::String>(object) ) {}

    More information is available at:
      < http://www.*-*-*.com/ ;

3.2 How do you pass a managed array or string to a native function?
    Any data that exists on the garbage collected heap needs to be
    "pinned" before passing a pointer to the data off to a native
    function. To pin something, you define a pinning pointer and point
    that to the data you want pinned. To pin an array, you just need to
    pin any element of the array. Consider this:

        void f(int*);
        Int32 arr[] = {1, 2, 3, 4, 5};   // Managed array
        int __pin * p = &arr[0];         // Pin the array
        f(p);

    To pass a System::String to native function, it is necessary to
    convert the string to a wchar_t* or char*. This can be done with the
    PtrToStringChars function from vcclr.h. Consider this:

        #using <mscorlib.dll>

        #include <stdio.h>
        #include <stdlib.h>
        #include <vcclr.h>

        using namespace System;

        void main(){
          String* str = S"Hello";

          // Conversion from String* to wchar_t* :
          const wchar_t __pin * p = PtrToStringChars(str);
          const wchar_t * p2 = p;
          printf("%S\n", p2);

          // Conversion from wchar_t* to char* :
          char* ch = (char*)malloc((str->Length + 1) * 2);
          wcstombs(ch, p, (str->Length + 1) * 2);
          printf("%s\n", ch);
        }

3.3 How do you declare an array parameter as a 'ref' argument to a
  function?
    To declare an array variable:

        Byte    myArray[] = new Byte[ 12 ];    // Byte is a value type
        Byte    myArray[] = { 12, 23, 32 };

        String*    myArray[] = new String*[12];
        String*    myArray[] = { S"onut", S"onutaou" };

    To return an array from a function:

        Byte    Function() []
        {
          return new Byte[12];
        }

        String*    Function() []
        {
          return new String*[12];
        }

    To pass an 'IN' array argument to a function

        void Function( Byte theArray[] )
        {
        }

        void Function( String* theArray[] )
        {
        }

    To pass a 'REF' array argument to a function

        typedef Byte ByteArray[];
        void Function( /*REF*/ ByteArray& theArray  )
        {
          theArray = new Byte[ 123 ];
        }

        typedef String* StringArray[];
        void Function( /*REF*/ StringArray& theArray )
        {
        }

3.4 Does MC++ have equivalent accessibility specifiers to C#?
    Managed C++ actually has more accessibility specifiers than C#,
    supporting:

        public:             internally/externally public
        protected:          internally/externally protected
        private:            internally/externally private
        private public:     externally private/internally public
        protected public:   externally protected/internally public
        private protected:  externally private/internally protected

    Note: ordering of the double accessibility specifiers doesn't
    matter; the least visible is taken for external. So: C# "internal"
    is "private public:" and C# "internal protected" is "private
    protected:"

3.5 How do you represent a C# foreach statement in MC++?
    The C# and VB foreach statement is simply syntactic sugar for
    stepping through an instance of the IEnumerator interface in the
    System::Collections namespace.

        foreach(string str in value) /*...*/;

    The above fragment of C# code is equivalent to the following MC++
    code.

        {
          IEnumerator* iter = values->GetEnumerator();
          String* str;
          while(iter->MoveNext()) {
            str = __try_cast<String*>(iter->Current);
            // ...
          }
        }

3.6 Can the Visual C++ compiler create a .netmodule file?
    A .netmodule file is just a library containing metadata but no
    assembly manifest. Visual C++ can create these files. By default it
    gives them a .dll extension instead. If you want to create the file
    with the .netmodule extension, use the linker /out:filename
    switch. Otherwise, there is no difference between the two. To create
    a DLL without an assembly manifest, pass the /clr:noAssembly and /LD
    switches to cl.exe. If you link in a separate stage, you need to
    pass /NOASSEMBLY to link.exe. Use the /ASSEMBLYMODULE switch for
    link.exe to include a module in a multi-file assembly.

3.7 How can you write a C# ref param or C# out param in C++?
    A CLS ref paramater is written in C++ by passing a pointer to the
    parameter. Consider the following code:

        // In Managed C++
        using namespace System;
        using namespace System::Runtime::InteropServices;
        public __gc struct S {
          static void RefParam(String** str) {
            *str = S"My String";
          }

          static void OutParam([Out] String** str) {
            *str = S"Another String";
          }
        };

        // In C#
        MyCSFunc() {
          String r = "";   // Must be initialized
          S.RefParam(ref r);

          String o;
          S.OutParam(out o);
        }

3.8 How can you detect at compile-time if code is compiled as managed?
    Whenever the code is compiled as managed via the /clr command-line
    option, the _MANAGED and _M_CEE macros are defined to 1. Consider
    the following:

        #ifdef _MANAGED
        // Managed code
        #else
        // Unmanaged code
        #endif

=========================
COMPILER, OPTIMIZER, AND LINKER
=========================

4.1 Does Visual C++ .NET still support native code?
    Yes! Visual C++ .NET is the only compiler from Microsoft that can
    compile to native code, managed code, or both. There are significant
    benefits for native applications by moving to Visual C++ .NET,
    including better optimizations, whole program optimization, security
    checks, debug run-time checks, and a better de{*filter*}.

4.2 What products are built with Visual C++ .NET?
    Visual Studio .NET, Windows XP, and all Xbox games were built with
    the compiler from Visual C++ .NET. Visual C++ .NET is the most
    tested, most used compiler to ever come out of Microsoft.

4.3 Where is the profiler in Visual Studio .NET?
    Rather than provide a not-too-great profiler in Visual Studio, we
    opted to work with Compuware to provide a much better integrated
    profiler. You can go get their DevPartner Profiler Community Edition
    for free. Here's a link
      < http://www.*-*-*.com/ ;.

4.4 Is Visual C++ available for the Itanium Processor Family
  architecture?
    The VC++ compiler, libs (CRT, STL, ATL, MFC), and de{*filter*} are
    already available for the Intanium Processor Family through our
    platform SDKs and have been so for quite a while.  These are
    currently cross development (run on 32-bit machines targeting the
    Itanium Processor Family) and are mature.

    The Intel compiler is not the only compiler in good shape.  Ours has
    been very, very stable (including C++ EH) and more than competitive
    with respect to benchmarks.  Many very large applications are up and
    running with the VC++ compiler (including NT and SQL) and have been
    doing so for a very long time.  You will not need to use any other
    compiler unless you choose to do so.

    The Itanium Processor Family architecture is difficult to optimize
    for (many compiler efforts are struggling), but the VC++ team has
    had a dedicated set of developers for multiple years on this
    project.  The VC++ team has added several optimizations to help this
    architecture out including profile-guided feedback, data and control
    speculation, predication and branch prediction, and software
    pipelining.  As time goes on compilers get better at optimization,
    but any rumor that compiler optimizations are holding it back are
    completely erroneous.

4.5 How do compiler security checks work? Should I use them?
    I wrote a rather detailed article available here
    < http://www.*-*-*.com/ ;. The Visual C++ team
    thoroughly recommends building with the /GS switch. It is the
    default for any project created with Visual C++ .NET and beyond.

=========================
PROJECT SYSTEM, DE{*filter*}
=========================

5.1 How do I create a project for a native console application?
    In VS.NET there's only one template project type for all unmanaged
    EXE projects. Just use the "new Win32 application" wizard, and
    before clicking finish in the wizard, switch to the options view and
    select "Console".

5.2 Where is the 'export make file' feature in Visual Studio .NET?
    Export Make File was dropped from VS7.

    For nightly builds you can use the project and solution as is using
    devenv at the command line instead of nmake. This way you save
    yourself any extra steps for maintaining an extra file, plus you can
    use all the features in the IDE that would be lost if you exported
    it. If you have existing make files in your source tree, then you
    can add them to the IDE using a Make File projects.

    For more information about using devenv at the command line, you can
    run Common7\tools\VSVars32.bat at the command line, and then run
    'devenv /?' for more details.

5.3 How do I rename a file in a C++ project?
    1. Remove .h, .cpp and .resx files you want to rename.
    2. Rename the .h and .cpp files on the disk via Windows Explorer or
       the command line.  Delete the .resx files because these will be
       regenerated by the IDE.
    3. Right click the project, choose "Add existing items".  Add the
       .cpp and .h files.

    This is different from VB/VC# projects because the those projects
    interact with the file system in a different manner. VB/VC# are
    directory based which means they show what is on disk and you can
    manipulate the files directly and the VC++ one is reference based
    which means we just keep references to files that are on disk. That
    is why when you delete a file from a VC# project it is deleted
    permanently from disk but in VC++ we just remove the reference and
    the file still exists on disk.

=========================
LIBRARIES, CRT, STL
=========================

6.1 What is the difference between msvcrt.dll and msvcr70.dll?
    Prior to Visual C++ .NET, every release of Visual C++ put the C
    runtime in msvcrt.dll. This created a servicing nightmare as updates
    to msvcrt.dll to fix bugs had the possibility of breaking existing
    applications, and indeed this has happened. The problem is
    compounded by the fact that msvcrt.dll is now a "known DLL" in
    the Windows operating system, meaning that the operating system
    itself is the only one that can service msvcrt.dll. All applications
    use the system's version of msvcrt.dll and cannot supply their
    own. More information about known DLLs is available here
    < http://www.*-*-*.com/ ;.

    In light of these difficulties, we decided the best solution would
    be to explicitly version the C runtime in the same manner that the
    C++ runtime, such as msvcp50.dll and msvcp60.dll, have been
    versioned for some time. The msvcrt.dll is now a system component
    owned and built by Windows. It is intended for future use only by
    system level components. Applications should use and redistribute
    msvcr70.dll. Many of us in Visual C++ are advocating that
    msvcr70.dll does not become a known DLL. Therefore, applications
    should avoid placing a copy, or using an existing copy, of
    msvcr70.dll in the system directory. Instead, the application should
    keep a copy of msvcr70.dll in its application directory with the
    program executable. Any application built with Visual C++ .NET using
    the /MD switch will necessarily use msvcr70.dll.

6.2 What problems exist if an application uses both msvcrt.dll and
  msvcr70.dll?
    If you've got a .lib or .obj that needs to link to msvcrt.lib, then
    you shouldn't have to recompile it to work with the new msvcrt.lib
    in VC7. The lib/obj may rely on the sizes/field offsets/member func
    names of various CRT classes or variables, and those should all
    still exist in a compatible way. Of course, once you relink against
    msvcrt.lib, your final exe/dll image will now have a dependency on
    msvcr70.dll instead of msvcrt.dll.

    That is all within the context of a single dll or exe.  If you have
    more than one dll/exe, then you have the possibility of more than
    one CRT. The problem with cross-CRT resource allocation/deallocation
    isn't so much a problem of VC7 -vs- older versions. Instead, it's
    something that's always been a factor when using the CRT -
    e.g. statically linking the CRT into multiple DLLs has the same
    problem.  People who run into this problem with static CRTs have
    always been instructed to compile /MD to use the CRT DLL. That's
    still true.  But now that the CRT DLL has been renamed to
    msvcr70.dll, you have to worry about an app with some older
    components linked to msvcrt.dll, some to msvcr70.dll.  Usually
    that's not a problem. But if those dlls pass CRT resources across
    the msvcrt/msvcr70 boundary, you'll get the same kind of problems
    you always do with mismatched CRTs.

    Contrast that to the situation with linking to msvcprt.lib when
    using the DLL version of the C++ Standard Library.  You will not be
    able to link an old .lib/.obj against the new msvcprt.lib.  You'll
    almost certainly get linker errors, and if you don't, you'll still
    run into problems because class field items have different offsets.
    You must recompile, which means if you've got a 3rd party library
    that used msvcprt.lib, you'll have to get a new version recompiled
    with VC7.

    BTW, this meaning of binary compatibility is what restricts us so
    strongly when it comes to updating the C++ Standard Library portion
    of the CRT in service packs.  Dinkumware has had an upgrade version
    of the C++ Standard Library available for VC6 for years, but the
    changes in there did not appear in VC6 service packs.  That's
    because there was no way to pick up those changes and still produce
    an msvcp60.dll which exported all the entry points that were present
    in the original version, and also kept all class sizes and offsets
    identical.  Failure to do that would mean that dropping a new
    msvcp60.dll in on an app compiled against the original release of
    VC6 would result in a broken app.

=========================
MORE INFORMATION
=========================

A.1 Contributors
    The maintainers of this FAQ would like to thank the following people
    for contributing answers: Anson Tsao [MSFT], Brandon Bray [MSFT],
    Brick Baldwin [MSFT], Diana Kumar [MSFT], Jeff Peil [MSFT], Phil
    Lucido [MSFT], Ronald Laeremans [MSFT], Terry Leeper [MSFT], and
    Tomas Restrepo [MVP].

A.2 Posting Locations
    This FAQ is posted to the following newsgroups frequently:
      microsoft.public.vc.language
      microsoft.public.vc.ide_general
      microsoft.public.vc.mfc
      microsoft.public.vc.stl
      microsoft.public.dotnet.faqs
      microsoft.public.dotnet.languages.vc
      microsoft.public.dotnet.languages.vc.libraries
      comp.os.ms-windows.programmer



Wed, 06 Apr 2005 01:52:15 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Visual C++ Community FAQ

2. Visual C++ Community FAQ

3. Visual C++ Community FAQ

4. FAQ : Visual C++ 5.0 ????

5. FAQ websites for Visual C++?

6. Pointer to the MFC/Visual C++ FAQ...

7. FAQ websites for Visual C++?

8. MFC/Visual C++ FAQ at

9. Pointer to the MFC/Visual C++ FAQ...

10. Pointer to the MFC/Visual C++ FAQ...

11. Visual C++ .NET coexisting with Visual C++ v6.0 SP5

12. Visual C++ to eMbedded Visual C++ problem

 

 
Powered by phpBB® Forum Software