Calling managed C++ function from C# program

  • Thread starter TxCHLInstructor
  • Start date
T

TxCHLInstructor

I have a 3rd-party program in managed C++ (source code) that I need to
add to a C# project.

I can compile the C++ program as a library. The C++ module includes a
class HIDBootLoaderFORM, but when I try to create an object of
HIDBootLoaderFORMin my C# program, it can't find the class.

Application.Run(new HIDBootLoaderFORM());

C:\TAOS\TAOS_DLSv3.x\TAOS_DigitalLightSensor\cTAOS_Device_Mgr.cs(412,33): error
CS0246: The type or namespace name 'HIDBootLoaderFORM' could not be
found (are you missing a using directive or an assembly reference?)

The sln file contains both the HIDBootLoaderFORM (in managed C++), and
the C# program from which I'm trying to call the HIDBootLoaderFORM ctor.

I've spent some time looking through the Microsoft documentation, but I
keep running into references that must be assuming that I already know
how to do this. For instance, there was one reference to linking a
..netmodule, but I can't find anything about linking anywhere in the C#
properties. (the article:
http://blogs.msdn.com/b/junfeng/archive/2005/05/19/420186.aspx)

Since I can't seem to find anything about this in my various searches, I
have to assume that this is a very basic problem that nobody else has.
I'm hoping someone here can enlighten me.
 
M

mick

TxCHLInstructor said:
I have a 3rd-party program in managed C++ (source code) that I need to add
to a C# project.

I can compile the C++ program as a library. The C++ module includes a
class HIDBootLoaderFORM, but when I try to create an object of
HIDBootLoaderFORMin my C# program, it can't find the class.

Application.Run(new HIDBootLoaderFORM());

C:\TAOS\TAOS_DLSv3.x\TAOS_DigitalLightSensor\cTAOS_Device_Mgr.cs(412,33):
error CS0246: The type or namespace name 'HIDBootLoaderFORM' could not be
found (are you missing a using directive or an assembly reference?)

The sln file contains both the HIDBootLoaderFORM (in managed C++), and the
C# program from which I'm trying to call the HIDBootLoaderFORM ctor.

I've spent some time looking through the Microsoft documentation, but I
keep running into references that must be assuming that I already know how
to do this. For instance, there was one reference to linking a .netmodule,
but I can't find anything about linking anywhere in the C# properties.
(the article:
http://blogs.msdn.com/b/junfeng/archive/2005/05/19/420186.aspx)

Since I can't seem to find anything about this in my various searches, I
have to assume that this is a very basic problem that nobody else has. I'm
hoping someone here can enlighten me.

Have you added the reference to the .dll? Right-click Solution
Explorer/References then
choose Add Reference then point to the .dll. Then in your C# code add a
using statement.

mick
 
T

TxCHLInstructor

I have a 3rd-party program in managed C++ (source code) that I need to
add to a C# project.
<snip>

I tried to add a reference to the C++ library, but I got a message
saying the reference could not be added. Also, I see a warning when
compiling the C++ code: "This object file does not define any previously
undefined public symbols, so it will not be used by any link operation
that consumes this library"

I don't know how to get past this. The class is declared as:

namespace HIDBootLoader {
....
public ref class HIDBootLoaderFORM : public System::Windows::Forms::Form
{
....
}
}

What else do I need to do?
 
T

TxCHLInstructor

Have you added the reference to the .dll? Right-click Solution
Explorer/References then
choose Add Reference then point to the .dll. Then in your C# code add a
using statement.

mick

I was not using a DLL. The C++ is managed code; I was trying to refer to
it in the same sln.

When I tried to add a reference, it failed with a non-helpful error
message. The MSDN had a cryptic reference to "compiled for an
unsupported platform", which I did not understand. Both modules are
built for .NET 4.0
 
T

TxCHLInstructor

Have you added the reference to the .dll? Right-click Solution
Explorer/References then
choose Add Reference then point to the .dll. Then in your C# code add a
using statement.

mick

The C++ code is publicly available, so I could post it if needed.
 
T

TxCHLInstructor

On 4/8/2011 7:39 PM, Peter Duniho wrote:
So the first step is to make sure you understand how to use DLLs
generally. As mick said, you need to add the DLL assembly as a reference
in your project.

If that doesn't work, then there's something wrong with the way you
built your managed C++ DLL. If you create a new project in VS, then as
long as you make sure you choose the correct project type in the Add
Project… dialog, there's nothing else to do except write the code. If
you are trying to change an existing DLL project into a managed code
project, you've got your work cut out for you, as there are a number of
settings you'll have to fiddle with.

I recommend the former approach. :)

Pete

I did manage to get everything to play together, compiling the C++ into
a library (DLL). The folks here do not want to ship DLLs, but just a
single monolithic EXE. Is is possible to combine a managed C++ module
into a C# program in the form of a single monolithic EXE? The obvious
step of compiling the C++ project as a static library did not work
because the C# portion refused to accept a reference other than EXE or DLL.
 
J

Jason Keats

TxCHLInstructor said:
I did manage to get everything to play together, compiling the C++ into a
library (DLL). The folks here do not want to ship DLLs, but just a single
monolithic EXE. Is is possible to combine a managed C++ module into a C#
program in the form of a single monolithic EXE? The obvious step of
compiling the C++ project as a static library did not work because the C#
portion refused to accept a reference other than EXE or DLL.


If ilmerge is not an option, then you could embed the DLLs you want to merge
as resources and load them on demand...

http://blogs.msdn.com/b/microsoft_p...r-excerpt-2-from-clr-via-c-third-edition.aspx
 
C

Curtis Newton

single monolithic EXE. Is is possible to combine a managed C++ module
into a C# program in the form of a single monolithic EXE? The obvious

Us the dll as an embedded resource? And then extract the dll from the
exe and use it?

static public string ExtractFromAssembly(string manifest_filename)
{
string tmp_filename = System.IO.Path.GetTempFileName();

Assembly assembly = Assembly.GetExecutingAssembly();

Stream input = assembly.GetManifestResourceStream(manifest_filename);
Stream output = File.Open(tmp_filename, FileMode.Append);

CopyStream(input, output);

input.Dispose();
output.Dispose();

return tmp_filename;
}



C.
 

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