Using a C++ library.

  • Thread starter Thread starter Simon Jefferies
  • Start date Start date
S

Simon Jefferies

Hello,

How do I use a C++ library in a C# project? Can it be done under 2005? Do I
need to anything special, I remember reading somewhere that they included
this feature in the latest .NET.

Cheers,
Simon.
 
AFAIK, it if is a managed C++ library, you can use it as a .NET reference
and if it is a COM C++ library, you can use it as COM Reference. Also, you
can use its functions using DllImport if they are exposed directly. The
bottom line is that once compiled, the language used to create a library is
not important.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Simon Jefferies said:
Hello,

How do I use a C++ library in a C# project? Can it be done under 2005? Do
I need to anything special, I remember reading somewhere that they
included this feature in the latest .NET.

Cheers,
Simon.

Please be more explicit, what do you mean with library, if it's a dll then
you can call exported C style functions, anything else can't be used
directly.

Willy.
 
The issues of connecting .NET to a C++ library are many

1) C++ by default uses name mangling for its entry points

2) Do you have access to the C++ source?

3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?

The following .NET program is a sample caller of a C++ DLL.

using System;
using System.Runtime.InteropServices;
namespace TestingDataProject
{
public class TestDataPassing
{
[DllImport("TestDataPassingDLL.dll")]
public static extern void TestDataPassingEntry(ref MyStruct TestData, int
iDataLength);
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
public byte [] abMyData;
public int iMyLength;
public MyStruct (int iDummy)
{
abMyData = new byte[64];
iMyLength = 64;
} // public MyStruct ()
} // public struct MyStruct
[STAThread]
static void Main(string[] args)
{
MyStruct MyTestData = new MyStruct(1);

TestDataPassingEntry(ref MyTestData, 64);
Console.WriteLine(MyTestData.abMyData[0]);
Console.WriteLine("{0,8:X}",MyTestData.iMyLength);
} // public static void Main ()
} // public class TestDataPassing
} // namespace TestingDataProject

The following is the C++ DLL

#include <stdio.h>

extern "C" __declspec(dllexport) void TestDataPassingEntry

(unsigned char * pabData,
int iDataLength)

{
printf("Hello world\n");
for (int iNdx = 0; iNdx < 10; iNdx++)
*(pabData + iNdx) = 48 + iNdx;
*((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
} // extern "C" __declspec(dllexport) void TestDataPassingEntry ()

HIH,

Pat
 
Its a standard C++ library (.lib). The only way I have seen that I can do it
is to create a managed C++ project and link to the .lib this way. Is this
the only way with .lib as it is?

Cheers,
Simon.

"XP 64 Exchange screen problems"
The issues of connecting .NET to a C++ library are many

1) C++ by default uses name mangling for its entry points

2) Do you have access to the C++ source?

3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?

The following .NET program is a sample caller of a C++ DLL.

using System;
using System.Runtime.InteropServices;
namespace TestingDataProject
{
public class TestDataPassing
{
[DllImport("TestDataPassingDLL.dll")]
public static extern void TestDataPassingEntry(ref MyStruct TestData, int
iDataLength);
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
public byte [] abMyData;
public int iMyLength;
public MyStruct (int iDummy)
{
abMyData = new byte[64];
iMyLength = 64;
} // public MyStruct ()
} // public struct MyStruct
[STAThread]
static void Main(string[] args)
{
MyStruct MyTestData = new MyStruct(1);

TestDataPassingEntry(ref MyTestData, 64);
Console.WriteLine(MyTestData.abMyData[0]);
Console.WriteLine("{0,8:X}",MyTestData.iMyLength);
} // public static void Main ()
} // public class TestDataPassing
} // namespace TestingDataProject

The following is the C++ DLL

#include <stdio.h>

extern "C" __declspec(dllexport) void TestDataPassingEntry

(unsigned char * pabData,
int iDataLength)

{
printf("Hello world\n");
for (int iNdx = 0; iNdx < 10; iNdx++)
*(pabData + iNdx) = 48 + iNdx;
*((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
} // extern "C" __declspec(dllexport) void TestDataPassingEntry ()

HIH,

Pat

Willy Denoyette said:
Please be more explicit, what do you mean with library, if it's a dll
then
you can call exported C style functions, anything else can't be used
directly.

Willy.
 
Simon,

This is a C++ DLL built with Visual Studio 6.0, so the C++ code is NOT managed

HTH,

Pat

Simon Jefferies said:
Its a standard C++ library (.lib). The only way I have seen that I can do it
is to create a managed C++ project and link to the .lib this way. Is this
the only way with .lib as it is?

Cheers,
Simon.

"XP 64 Exchange screen problems"
The issues of connecting .NET to a C++ library are many

1) C++ by default uses name mangling for its entry points

2) Do you have access to the C++ source?

3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?

The following .NET program is a sample caller of a C++ DLL.

using System;
using System.Runtime.InteropServices;
namespace TestingDataProject
{
public class TestDataPassing
{
[DllImport("TestDataPassingDLL.dll")]
public static extern void TestDataPassingEntry(ref MyStruct TestData, int
iDataLength);
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
public byte [] abMyData;
public int iMyLength;
public MyStruct (int iDummy)
{
abMyData = new byte[64];
iMyLength = 64;
} // public MyStruct ()
} // public struct MyStruct
[STAThread]
static void Main(string[] args)
{
MyStruct MyTestData = new MyStruct(1);

TestDataPassingEntry(ref MyTestData, 64);
Console.WriteLine(MyTestData.abMyData[0]);
Console.WriteLine("{0,8:X}",MyTestData.iMyLength);
} // public static void Main ()
} // public class TestDataPassing
} // namespace TestingDataProject

The following is the C++ DLL

#include <stdio.h>

extern "C" __declspec(dllexport) void TestDataPassingEntry

(unsigned char * pabData,
int iDataLength)

{
printf("Hello world\n");
for (int iNdx = 0; iNdx < 10; iNdx++)
*(pabData + iNdx) = 48 + iNdx;
*((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
} // extern "C" __declspec(dllexport) void TestDataPassingEntry ()

HIH,

Pat

Willy Denoyette said:
Hello,

How do I use a C++ library in a C# project? Can it be done under 2005?
Do
I need to anything special, I remember reading somewhere that they
included this feature in the latest .NET.

Cheers,
Simon.


Please be more explicit, what do you mean with library, if it's a dll
then
you can call exported C style functions, anything else can't be used
directly.

Willy.
 
Simon Jefferies said:
Its a standard C++ library (.lib). The only way I have seen that I can do
it is to create a managed C++ project and link to the .lib this way. Is
this the only way with .lib as it is?

Cheers,
Simon.

Yes, you can only bind dynamically and call exported C style functions from
C#.
Static libraries can only be bound by using a linker.

Willy.
 
"XP 64 Exchange screen problems"
The issues of connecting .NET to a C++ library are many

1) C++ by default uses name mangling for its entry points

2) Do you have access to the C++ source?

3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?

None of these are real issue's as long as you have a DLL that exports C
style functions or exposes a COM interface.

Willy.
 
Back
Top