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.