C++/CLI and C#

J

jraul

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

William DePalo [MVP VC++]

jraul said:
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
 
J

jraul

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");
}
};
}
 
W

William DePalo [MVP VC++]

jraul said:
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
 
J

jraul

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();
}
}
 
B

Ben Voigt [C++ MVP]

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

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