Compile error when calling managed C++ from C#

Z

zlatko

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 !
 
W

Willy Denoyette [MVP]

zlatko said:
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.
 
Z

zlatko

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.
 
W

Willy Denoyette [MVP]

zlatko said:
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.
 

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