Question : Include C++ header files in .Net Application

We are using a 3rd party API (having unmanaged dll) in our Windows application (VS 2008, C#).

In the API documentation, there is an instruction "If you are using C++ to create an application, you can use these include files directly; if you are using another language to create the application, you can use the include files as a template"

How to use the C++ header files in C#.Net windows application as a template and use it in .Net application ?

Any help would be highly appreciated.

Thanks
Raj

Answer : Include C++ header files in .Net Application

>> How to use the C++ header files in C#.Net windows application as a template and use it in .Net application ?

Wrap the unmanaged DLL in managed C++ as a mixed mode assembly and then call the managed C++ from C#. See below as a simple example

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

"Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform.

Alternative. It is possible to call exported function in DLLs and EXEs using Interop Services. If you can modify the code of the EXE to expert the functions you need to call you can use DllImport to import them into your C# code.

http://www.csharphelp.com/archives/archive52.html
http://msdn2.microsoft.com/en-us/library/9h658af8(VS.80).aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
// C++ (all in one IJW assembly)
 
#include <iostream>
 
namespace UnmanagedCode
{
        void foo()
        {
                std::cout << "Hello, world" << std::endl;
        }
}
 
namespace ManagedCode
{
        public ref class fooWrapper
        {
        public:
                static void foo()
                {
                        UnmanagedCode::foo();
                }
        };
}
 
 
// C# code (seperate assembly)
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace scratchcs
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagedCode.fooWrapper.foo();
        }
    }
}
Random Solutions  
 
programming4us programming4us