2

I am working on simple telephony application where I am changing the class of service of panasonic pbx extension. For that I am using "Tapi32.dll" which has methods in c++. Now as per my need I have to pass two argument both integer pointer type. One Argument is getting passed correctly but I am not able to pass second argumnet which is structure type.

Here is my code...

[DllImport("Tapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
unsafe private static extern int lineDevSpecific(int* hLine, int* lpParams);

[StructLayout(LayoutKind.Sequential)]
public struct UserRec {
    [MarshalAs(UnmanagedType.I4)]
    public int dwMode=4;
    public int dwParam1=8;
}

unsafe static void Main(string[] args) {
    int vline=int.Parse("Ext101");
    int* hline = &vline;
    lineDevSpecific(hline, ref UserRec userrec);
}
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
vikas
  • 339
  • 2
  • 6
  • 12
  • http://stackoverflow.com/questions/4558082/passing-an-struct-array-into-c-dll-from-c-sharp –  Mar 04 '13 at 08:16

1 Answers1

2
[DllImport("Tapi32.dll", SetLastError=true)]
unsafe private static extern int lineDevSpecific(int* hLine, IntPtr lpParams);

unsafe static void Main(string[] args) {
    int vline=int.Parse("Ext101");
    int* hline=&vline;

    var sizeUserRec=Marshal.SizeOf(typeof(UserRec));
    var userRec=Marshal.AllocHGlobal(sizeUserRec);
    lineDevSpecific(hline, userRec);
    var x=(UserRec)Marshal.PtrToStructure(userRec, typeof(UserRec));
    Marshal.FreeHGlobal(userRec);
}

Take a look of this answer of question

You can find some more to make marshalling easier and more reusable.

Community
  • 1
  • 1
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • ken Sir I am getting error "cannot have instance field initializers in structs c#" – vikas Mar 04 '13 at 09:08
  • @vikas: You cannot have the field initializers that way. Just remove the default values of `dwMode` and `dwParam1`. – Ken Kin Mar 04 '13 at 10:47