CString constructor failed when called from C#

  • Thread starter Patrik via DotNetMonster.com
  • Start date
P

Patrik via DotNetMonster.com

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 "CStringData* 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(object sender, System.EventArgs e)
{
CppLib.Class1 c = new CppLib.Class1();
c.DoAction();
}

thanx for any ideas..
pm
 
J

Jochen Kalmbach [MVP]

Hi Patrik!
Another C# windows application reference this C++ library.

You can´t use a LIB in an C# program! Maybe you mean: YOu have an
managed C++ assembly.
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 "CStringData* pData
= pStringMgr->GetNilString();".

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
http://support.microsoft.com/kb/814472/en-us

Mixed DLL Loading Problem
http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vcconmixeddllloadingproblem.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
P

Patrik Moravcik via DotNetMonster.com

Hi Jochen,
You can?t use a LIB in an C# program! Maybe you mean: YOu have an
managed C++ assembly.

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top