PC Review


Reply
Thread Tools Rate Thread

Calling managed C++ function from C# program

 
 
TxCHLInstructor
Guest
Posts: n/a
 
      8th Apr 2011
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/arch...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.
--
Howard
 
Reply With Quote
 
 
 
 
mick
Guest
Posts: n/a
 
      8th Apr 2011


"TxCHLInstructor" <(E-Mail Removed)> wrote in message
news:innl1n$li$(E-Mail Removed)...
> 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/arch...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.
> --
> Howard


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

 
Reply With Quote
 
TxCHLInstructor
Guest
Posts: n/a
 
      8th Apr 2011
On 4/8/2011 1:44 PM, TxCHLInstructor wrote:
> 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?
--
Howard



 
Reply With Quote
 
TxCHLInstructor
Guest
Posts: n/a
 
      8th Apr 2011
On 4/8/2011 1:56 PM, mick wrote:

>
> 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
--
Howard
 
Reply With Quote
 
TxCHLInstructor
Guest
Posts: n/a
 
      8th Apr 2011
On 4/8/2011 1:56 PM, mick wrote:
>
>
> "TxCHLInstructor" <(E-Mail Removed)> wrote in message
> news:innl1n$li$(E-Mail Removed)...
>> 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/arch...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.
>> --
>> Howard

>
> 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.
--
Howard
 
Reply With Quote
 
TxCHLInstructor
Guest
Posts: n/a
 
      12th Apr 2011
On 4/8/2011 7:39 PM, Peter Duniho wrote:
<snip>

> 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.
--
Howard
 
Reply With Quote
 
Jason Keats
Guest
Posts: n/a
 
      13th Apr 2011
"TxCHLInstructor" <(E-Mail Removed)> wrote in message
news:io2c3k$qm8$(E-Mail Removed)...
>
> 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_pr...d-edition.aspx


 
Reply With Quote
 
Curtis Newton
Guest
Posts: n/a
 
      15th Apr 2011
On 2011-04-12, TxCHLInstructor <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a managed function from native code Bob Altman Microsoft VC .NET 17 28th Feb 2008 02:14 AM
Calling Legacy C function from Managed C++ briankirkpatrick@cox.net Microsoft VC .NET 11 16th Aug 2007 07:14 PM
Calling managed function from unmanaged function in mixed mode dll slugster@gmail.com Microsoft VC .NET 1 25th Jul 2005 07:30 AM
Problem with calling a managed C++ function from managed C# Andre Microsoft C# .NET 2 2nd Aug 2004 02:24 PM
calling a function in a managed .dll - losing waitcursor Bernie Yaeger Microsoft VB .NET 0 21st Feb 2004 07:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:20 PM.