0

Possible Duplicate:
Marshal C++ struct array into C#

I have an C++ .dll and I must use it with C# but there are some problems. First of all this is one of them.

Here for using C++ .dll file I must rechange below C++ struct to C# struct. Help me pls.

C++ sturcture:

typedef struct USMC_Devices_st{
      DWORD NOD;             // Number of the devices ready to work

      char **Serial;        // Array of 16 byte ASCII strings
      char **Version;       // Array of 4 byte ASCII strings
} USMC_Devices;
Community
  • 1
  • 1
AlpK
  • 13
  • 1
  • 4

2 Answers2

1

struct USMC_DEVICES_st would translate to something akin to:

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
    struct USMC_DEVICES_st
    {
        public UInt32 NOD;

        public IntPtr serial;

        public IntPtr Version;
    }

Accessing Serial and Version could be done with code such as:

IntPtr ser;

for (var i = 0;
     (ser = Marshal.ReadIntPtr(um.serial, i)) != IntPtr.Zero;
     i += Marshal.SizeOf(ser))
{
    var serial = Marshal.PtrToStringAnsi(ser);
}

Edit - Following your comments here is a more detailed example of accessing the struct's members:

static void PrintDevices(USMC_DEVICES_st um)
        {
            const int serialSize = 16;
            const int verSize = 4;
            int j = 0;
            for (var i= 0; i < um.NOD; i++, j+= IntPtr.Size)
            {
                var ser = Marshal.ReadIntPtr(um.serial, j);
                var ver = Marshal.ReadIntPtr(um.Version, j);
                // ensure we check for null pointers - just in case
                if (ver == IntPtr.Zero || ser == IntPtr.Zero) break;

                Console.WriteLine("Device {0}, \tSerial number {1}",
                                    Marshal.PtrToStringAnsi(ser, serialSize),
                                    Marshal.PtrToStringAnsi(ver, verSize));
            }
        }
Anthill
  • 1,219
  • 10
  • 20
  • Thanks for your answer and one more question to you how can I rechange void PrintDevices(USMC_Devices &DVS) { for(DWORD i = 0; i < DVS.NOD; i++) { printf("Device - %d,\tSerial Number - %.16s,\tVersion - %.4s\n",i+1,DVS.Serial[i],DVS.Version[i]); } } – AlpK Nov 27 '12 at 08:58
0

First if you have the C++ dll, then you you can use those dll into C# Project. follow Below methods

  1. Open Visual Studio Command Prompt (Run as Administartor)
  2. Change the Path where the dll located (cd C:/User/Douments/Visual Studio 2010/)
  3. type the command

tlbimp .dll /transform:dispret /out:

You will get result like this below.

Microsoft (R) .NET Framework Type Library to Assembly Converter 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved.

TlbImp : Type library imported to C:\Users\082043\Desktop\Interop.CACDirect.dll

C:\Users\082043\Desktop>

Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
  • Thank you for your answers,Can you explain more clearly pls. Because I didnt eve know that there is a simple way to use c++dll instead of rechange code by import mothed to C# – AlpK Nov 27 '12 at 08:53
  • Suppose you are having MySample.dll (in C++ DLL). then Open Visual Studio Command Prompt (Run as Administartor) Change the Path where the dll located (cd C:/User/Douments/Visual Studio 2010/) type the command tlbimp MySample.dll /transform:dispret /out: Interop.MySample.dll , Then you will get .Net CLR dll in the folder. then you can add this interop dll to ur project. consume all c++ functions. Please let me know if you have still issues. – Akshay Joy Nov 28 '12 at 06:19