C++ ClassLib for C#

M

mike

I'd like to port some C++ code to a .NET ClassLib for C# consumers.

Am using VC2005, .NET 2.0

However I find that If I simply generate a C++ ClassLib using the wizard...

// SimpleCLib.h
#pragma once
using namespace System;
namespace SimpleCLib {
public ref class Class1
{
// TODO: Add your methods for this class here.
};

}

Then generate a C# console app, add the Lib Reference and a new instance of Class1 it generates a runtime error. The error is 'System.BadImageFormatException'

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpConsoleTest {
class Program
{
static void Main(string[] args)
{
SimpleCLib.Class1 scl = new SimpleCLib.Class1;
}
}
}

After some investigation, I am not seeing anything other than verifying /FIXED:no setting in the linker.

Any help would be appreciate, thanks - Mike
 
M

matthew breedlove

mike said:
Then generate a C# console app, add the Lib Reference and a new instance
of Class1 it generates a runtime error. The error is
'System.BadImageFormatException'

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpConsoleTest {

class Program
{
static void Main(string[] args)
{
SimpleCLib.Class1 scl = new SimpleCLib.Class1;
}
}
}

I setup two projects as you described, and added a reference to the C#
project pointing to the DLL output of the C++ project. The only change
I made to your code was to add parens after the class name:

SimpleCLib.Class1 x = new SimpleCLib.Class1();

Everything compiled fine. I also tested by adding a project reference
with both projects in the same solution, and that worked fine.

How are you adding the reference in your C# project to the C++ output?
Could you possibly copy/paste the output of the build?

-Matthew
 
M

mike

Thanks for the aid Mathew,

I do forget those parens often.

But this is good news.

Yes, the Reference added to the C# project is for the .dll in the C++ project's output dir.

So it did execute alright? I will have to dig into another workstation a give a try.

I have been testing all week with a C++ CLR console app and now that I have switched to the C# console I hit this runtime exception.
 
M

matthew breedlove

mike said:
Thanks for the aid Mathew,

I do forget those parens often.

But this is good news.

Yes, the Reference added to the C# project is for the .dll in the C++
project's output dir.

So it did execute alright? I will have to dig into another workstation a
give a try.

I have been testing all week with a C++ CLR console app and now that I
have switched to the C# console I hit this runtime exception.

It executed just fine when I ran it.

Another thing to check is to make sure both the C# app and the C++
library are built for the same version of the .NET runtime.
 
M

mike

Yep, works great at other machines.

You got me in the right direction.

Turns out I have to compile the C++ ClassLib to "x64" for it to work on my
x64 machine.

Thanks,

- Mike
 

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