CString constructor failed when called from C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrik via DotNetMonster.com

    CString constructor failed when called from C#

    Hi,

    I have simple managed C++ lib library with variable declaration of type
    CString.
    Another C# windows application reference this C++ library.
    When I try call method in C++ library from C#, line with CString
    declaration throw an NullReference exception 'Object reference not set to
    an instance of an object.' in file atlsimpstr.h at line "CStringDat a* pData
    = pStringMgr->GetNilString() ;".
    But if I try call this C++ method from managed C++ console application,
    everything is ok.

    Can anyone explain me, what I'm doing wrong ?

    managed C++ library:
    #pragma once
    #include <atlstr.h>
    #include "stdafx.h"

    using namespace System;

    namespace CppLib
    {
    public __gc class Class1
    {
    public:
    Class1(void)
    {
    }

    ~Class1(void)
    {
    }

    void DoAction()
    {
    CString s("abc"); //this line throw NullReference exception 'Object
    reference not set to an instance of an object.'
    }
    };
    }

    C# method:
    private void button1_Click(o bject sender, System.EventArg s e)
    {
    CppLib.Class1 c = new CppLib.Class1() ;
    c.DoAction();
    }

    thanx for any ideas..
    pm

    --
    Message posted via http://www.dotnetmonster.com
  • Jochen Kalmbach [MVP]

    #2
    Re: CString constructor failed when called from C#

    Hi Patrik!
    [color=blue]
    > Another C# windows application reference this C++ library.[/color]

    You can“t use a LIB in an C# program! Maybe you mean: YOu have an
    managed C++ assembly.
    [color=blue]
    > When I try call method in C++ library from C#, line with CString
    > declaration throw an NullReference exception 'Object reference not set to
    > an instance of an object.' in file atlsimpstr.h at line "CStringDat a* pData
    > = pStringMgr->GetNilString() ;".[/color]

    Are you linking with "/noentry" ?
    If yes, then you must explicit call the init-reoutine of the CRT.

    For more info see:
    PRB: Linker Warnings When You Build Managed Extensions for C++ DLL Projects


    Mixed DLL Loading Problem
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    --
    Greetings
    Jochen

    My blog about Win32 and .NET

    Comment

    • Patrik Moravcik via DotNetMonster.com

      #3
      Re: CString constructor failed when called from C#

      Hi Jochen,
      [color=blue]
      > You can?t use a LIB in an C# program! Maybe you mean: YOu have an[/color]
      managed C++ assembly.

      Yes, I mean assembly.
      [color=blue]
      > Are you linking with "/noentry" ?
      > If yes, then you must explicit call the init-reoutine of the CRT.[/color]

      No, but I find a solution in Microsoft article "PRB: Linker Warnings When
      You Build Managed Extensions for C++ DLL Projects", thank you.

      Just one small problem:
      When I made steps described in RESOLUTION section in above article, in
      debug configuration I obtain a linker error:

      LINK : error LNK2020: unresolved token (0A00003F) _CrtDbgReport
      LINK : fatal error LNK1120: 1 unresolved externals

      but in release is everything, ok.
      I hope that is one of the switches in linker options, but I don't know
      which.
      Can you suggest me some solution ?

      thanks,
      pm

      --
      Message posted via http://www.dotnetmonster.com

      Comment

      Working...