How to Marshal the struct array to the pointer?

  • Thread starter Thread starter behindname
  • Start date Start date
B

behindname

Guest
Greetings,

I am importing the C++ dll to send sms/mms message to the mobile device.

With this dll, I can send sms to the multiple mobile devices,

but having difficulty in marshaling.


This is the example code in C++

OBSIReceiver receivers[1];
strcpy_s(receivers[0].szName, CStringA(m_strReceiverName));
strcpy_s(receivers[0].szPhoneNumber, CStringA(m_strReceiverPhone));
msg.pReceivers = receivers;
msg.ulReceiverCount = _countof(receivers);


msg.pReceivers is the pointer of the receivers struct array,

so I need to marshal the struct array in C#, to use this dll in winform application.


This is my code that I have implemented with.

OBSIMessage msg = new OBSIMessage();
OBSIReceiver[] receivers = new OBSIReceiver[phoneNumberList.Count];
OBSISendResult result = new OBSISendResult();


msg.szSubject = strTextTitle; //제목
msg.szCallbackNumber = callbackNumber; //회신 번호

for (int i = 0; i < phoneNumberList.Count; i++)
{
receivers.szName = receiverName;
receivers.szPhoneNumber = phoneNumberList;
}

msg.ulReceiverCount = (UInt32)phoneNumberList.Count;
//Process of Marshalling, to pass the parameter to the C++ function.
IntPtr[] ptrReceivers = new IntPtr[phoneNumberList.Count];
for (int j = 0; j < phoneNumberList.Count; j++)
{
IntPtr pReceiver = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OBSIReceiver)));
Marshal.StructureToPtr(receivers[j], pReceiver, false);
ptrReceivers[j] = pReceiver;
}

msg.pReceivers = ptrReceivers[0];

but this code only works when phoneNumberList count is 1.

What can I do?


I wish someone can help me.

Have a good day, and Thanks for the reading.

Continue reading...
 
Back
Top