473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Compile error when calling managed C++ from C#

Hello. I am new to .net .
I have a managed C++ library. It looks like this.

// header file
namespace cppnetdll
{
public __gc class Class1
{
public:
static int foo_int(void);
};
}

// source file
namespace cppnetdll
{
int Class1::foo_int(void)
{
return 123;
}
}

I can call this from a managed c++ program. When I try to call it from
a C# program, I get the compiler error: "The type or namespace name
'Class1' could not be found (are you missing a using directive or an
assembly reference?)" The error refers to the DllImport line below.

Here is the C# code
[code:1:a72c1df571]
namespace csuser
{
public class xxx
{
[DllImport("cppnetdll.dll")] extern
int Class1.foo_int();

private void yyy() { int i =
foo_int(); }
}
}[/code:1:a72c1df571]
I have tried various approaches but no success. What is the magic
syntax ?

It's funny that I can call unmanaged C++ functions from C# fairly
easily by declaring the functions as "C" and exporting from the DLL.
I expected calling managed code to be easier. Maybe it's so easy
that no one thought of documenting it !
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
4 4330
Hi,

You should add a reference to the library in your C# project.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"zlatko" <zl***********@opg-dot-com.no-spam.invalid> je napisal v sporocilo
news:42********@127.0.0.1 ...
Hello. I am new to .net .
I have a managed C++ library. It looks like this.

// header file
namespace cppnetdll
{
public __gc class Class1
{
public:
static int foo_int(void);
};
}

// source file
namespace cppnetdll
{
int Class1::foo_int(void)
{
return 123;
}
}

I can call this from a managed c++ program. When I try to call it from
a C# program, I get the compiler error: "The type or namespace name
'Class1' could not be found (are you missing a using directive or an
assembly reference?)" The error refers to the DllImport line below.

Here is the C# code
[code:1:a72c1df571]
namespace csuser
{
public class xxx
{
[DllImport("cppnetdll.dll")] extern
int Class1.foo_int();

private void yyy() { int i =
foo_int(); }
}
}[/code:1:a72c1df571]
I have tried various approaches but no success. What is the magic
syntax ?

It's funny that I can call unmanaged C++ functions from C# fairly
easily by declaring the functions as "C" and exporting from the DLL.
I expected calling managed code to be easier. Maybe it's so easy
that no one thought of documenting it !
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2

"zlatko" <zl***********@opg-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
Hello. I am new to .net .
I have a managed C++ library. It looks like this.

// header file
namespace cppnetdll
{
public __gc class Class1
{
public:
static int foo_int(void);
};
}

// source file
namespace cppnetdll
{
int Class1::foo_int(void)
{
return 123;
}
}

I can call this from a managed c++ program. When I try to call it from
a C# program, I get the compiler error: "The type or namespace name
'Class1' could not be found (are you missing a using directive or an
assembly reference?)" The error refers to the DllImport line below.

Here is the C# code
[code:1:a72c1df571]
namespace csuser
{
public class xxx
{
[DllImport("cppnetdll.dll")] extern
int Class1.foo_int();

private void yyy() { int i =
foo_int(); }
}
}[/code:1:a72c1df571]
I have tried various approaches but no success. What is the magic
syntax ?

It's funny that I can call unmanaged C++ functions from C# fairly
easily by declaring the functions as "C" and exporting from the DLL.
I expected calling managed code to be easier. Maybe it's so easy
that no one thought of documenting it !


You don't have to use Dllimport, your C++ class is a managed class, so you
have to set a reference to the assembly containing this class when compiling
your C# program.

Willy.
Nov 16 '05 #3
Oh !
I have to add a reference to the dll project !

Why didn't I have to do that when calling from C++ ?

Tell me, why does everything have to be so hidden with microsoft.
Sometimes the solution is in syntax, sometimes in the IDE. I want my
UNIX box back.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #4


"zlatko" <zl***********@opg-dot-com.no-spam.invalid> wrote in message
news:42**********@127.0.0.1...
Oh !
I have to add a reference to the dll project !

Why didn't I have to do that when calling from C++ ?

Tell me, why does everything have to be so hidden with microsoft.
Sometimes the solution is in syntax, sometimes in the IDE. I want my
UNIX box back.

Because C++ is not C# and .... there is nothing hidden at all, you just have
to start reading the docs (MSDN).

Willy.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
10
by: Jim H | last post by:
I sometimes get the following error from my Form's Dispose Method when the application is closing: ------------------------------------------------- An unhandled exception of type...
3
by: Wayne Allen | last post by:
I am receiving a compile error when attempting to include Directshow in a managed C++ program using Visual C++ 2005 Express Edition Version 8. The ..NET framework is version 2.0.50727. To...
2
by: Richard B | last post by:
I added a .NET dll as a reference to my new .NET dll. When I went to build, I received error "Unable to emit assembly: Referenced assembly 'CrystalReport' does not have a strong name". ...
1
by: Allen Maki | last post by:
//These codes will build but wil not compile. I hope someone would know why; and tell me. //I got the below error message during compile time #include "stdafx.h" #include <string.h> ...
13
by: Genady Beryozkin | last post by:
Hi, I've read the article at http://msdn2.microsoft.com/en-us/library/ms235211.aspx about how to compile MFC/ATL code, but I still have one question unanswered: Should I have the CComModule...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
6
by: guxu | last post by:
I have a managed C++ code which calls some methods in unmanaged C++ DLL. In my unmanaged DLL, I have a PROTECTED virutal function in a base class and derived class has a PRIVATE function which...
2
by: KevinMurphyRJLG | last post by:
I have a managed C++ class with a number of static methods, all of which are declared public. After switching from .NET 1.1 to 2.0 (Visual Studio 2003 to VS 2005), I'm now getting compiler...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.