471,616 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

C++/CLI and C#

I have created a C++/CLI class library and I will want to use the
classes made in it in a C# application. What do I need to do in the
C# project to get access to the code from the C++/CLI class library
project?

Am I supposed to add a reference? If so, to what? My class library
didn't seem to create a DLL or .LIB.

Jul 2 '07 #1
6 3146
"jraul" <jr*******@yahoo.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
>I have created a C++/CLI class library and I will want to use the
classes made in it in a C# application. What do I need to do in the
C# project to get access to the code from the C++/CLI class library
project?

Am I supposed to add a reference? If so, to what? My class library
didn't seem to create a DLL or .LIB.
Yes, you add a reference to the C++/CLI assembly (which is a DLL btw).

Are you sure that you compiled _and_ linked your C++/CLI project?

Regards,
Will
Jul 2 '07 #2
Yes, you add a reference to the C++/CLI assembly (which is a DLL btw).
>
Are you sure that you compiled _and_ linked your C++/CLI project?

Regards,
Will

I was looking in my class library's debug folder instead of the
solution's debug folder. Okay, so I added a reference to the DLL but
the following code doesn't work. I get the error in the C# project:

error CS0117: 'UnmanagedBackEnd.Test' does not contain a definition
for 'foobar'
// C# CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UnmanagedBackEnd;

namespace Forms_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Test test; // Comes from C++/CLI
Class Library, namespace UnmanagedBackEnd

test.foobar();
}
}
}

// C++/CLI Class library

#pragma once
using namespace System;

namespace UnmanagedBackEnd {
public ref class Test
{
Test()
{

}

void foobar()
{
System::Console::WriteLine("Hello");
}
};
}

Jul 2 '07 #3
"jraul" <jr*******@yahoo.comwrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
I was looking in my class library's debug folder instead of the
solution's debug folder. Okay, so I added a reference to the DLL but
the following code doesn't work. I get the error in the C# project:

error CS0117: 'UnmanagedBackEnd.Test' does not contain a definition
for 'foobar'
As you have shown it, the function is not visible not publicly.

Regards,
Will
Jul 2 '07 #4
On Jul 2, 9:02 am, "William DePalo [MVP VC++]"
<willd.no.s...@mvps.orgwrote:
"jraul" <jrauli...@yahoo.comwrote in message

news:11**********************@d30g2000prg.googlegr oups.com...
I was looking in my class library's debug folder instead of the
solution's debug folder. Okay, so I added a reference to the DLL but
the following code doesn't work. I get the error in the C# project:
error CS0117: 'UnmanagedBackEnd.Test' does not contain a definition
for 'foobar'

As you have shown it, the function is not visible not publicly.

Regards,
Will
Doh :)

Thanks for the help so far. Okay, now both the class library (C++/
CLI) and C# project compile and link, but when I execute I get the
error:

An unhandled exception of type 'System.BadImageFormatException'
occurred in Forms Test.exe

Additional information: Could not load file or assembly 'Unmanaged
BackEnd, Version=1.0.2739.15785, Culture=neutral, PublicKeyToken=null'
or one of its dependencies. An attempt was made to load a program with
an incorrect format.

I'm not sure what's wrong with the DLL. I tried deleting the
reference, and adding a new reference to the latest class library
build. The code is basically the same as above except I made the
methods of Test public:

public ref class Test
{
public:
Test()
{

}

void foobar()
{
System::Console::WriteLine("Hello");
}
};

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Test test = new Test();

test.foobar();
}
}

Jul 2 '07 #5
"jraul" <jr*******@yahoo.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...
Thanks for the help so far.
You are welcome.
Okay, now both the class library (C++/
CLI) and C# project compile and link, but
when I execute I get the
error:

An unhandled exception of type 'System.BadImageFormatException'
occurred in Forms Test.exe
Is the problem the one described here:

http://msdn2.microsoft.com/en-us/lib...fe(VS.80).aspx

Regards,
Will
Jul 2 '07 #6

"jraul" <jr*******@yahoo.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...
On Jul 2, 9:02 am, "William DePalo [MVP VC++]"
<willd.no.s...@mvps.orgwrote:
>"jraul" <jrauli...@yahoo.comwrote in message

news:11**********************@d30g2000prg.googleg roups.com...
I was looking in my class library's debug folder instead of the
solution's debug folder. Okay, so I added a reference to the DLL but
the following code doesn't work. I get the error in the C# project:
error CS0117: 'UnmanagedBackEnd.Test' does not contain a definition
for 'foobar'

As you have shown it, the function is not visible not publicly.

Regards,
Will

Doh :)

Thanks for the help so far. Okay, now both the class library (C++/
CLI) and C# project compile and link, but when I execute I get the
error:

An unhandled exception of type 'System.BadImageFormatException'
occurred in Forms Test.exe

Additional information: Could not load file or assembly 'Unmanaged
BackEnd, Version=1.0.2739.15785, Culture=neutral, PublicKeyToken=null'
or one of its dependencies. An attempt was made to load a program with
an incorrect format.

I'm not sure what's wrong with the DLL. I tried deleting the
reference, and adding a new reference to the latest class library
build. The code is basically the same as above except I made the
methods of Test public:
I see you figured out to make an instance before calling its methods.

You need to use a "Project" reference instead of pointing to a particular
file. The project reference will automatically pick debug or release
version automatically to match the main application, and will automatically
copy the assembly into the bin directory of the main app.

You can get BadImageFormatException also if you build a C++/CLI assembly
using mixed-mode (not /clr:pure) as an exe, and try to reference it from
another program. Mixed-mode C++/CLI exes are non-relocatable, so they can
only be the main program.

Jul 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William C. White | last post: by
2 posts views Thread by Albert Ahtenberg | last post: by
3 posts views Thread by James | last post: by
reply views Thread by Ollivier Robert | last post: by
1 post views Thread by Richard Galli | last post: by
4 posts views Thread by Albert Ahtenberg | last post: by
1 post views Thread by inderjit S Gabrie | last post: by
2 posts views Thread by Jack | last post: by
3 posts views Thread by Sandwick | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.