How to marshal array of custom data type using P/Invoke in C++ .Net


I am currently trying to call functions from an unmanaged c++ dll using P/Invoke in C++ .Net (2008).  I am completely new to P/Invoke, but have managed to stumble through by using various online references until now.

One particular function of the unmanaged dll is defined as follows:

PUNCH_BUREAU_RESULT_CODE PunchBureauDoAuthorisation(
      char            *pszPrimaryIPAddress,
      unsigned short usiPrimaryIPPort,
      char            *pszSecondaryIPAddress,
      unsigned short usiSecondaryIPPort,
      unsigned short usiResponseTimeoutInMs,
      char     rgcTransactionTimestamp[12],
      char        *pszPAN,
      char            rgcExpiryDate[4],
      char            *pszStationID,
        __int64        i64Amount,
        unsigned short usiBatch,
      unsigned short usiCurrencyCode,
      unsigned short usiNumberOfProducts,
      PRODUCT_DATA_DSCR rgProductData[18],
      char rgcActionCode[3],
      char rgcApprovalCode[6]);

PRODUCT_DATA_DSCR is a struct defined in the library.

I currently have the following for the P/Invoke:

static PUNCH_BUREAU_RESULT_CODE PunchBureauDoAuthorisation(
      String^ pszPrimaryIPAddress,
      System::UInt16 usiPrimaryIPPort,
      String^      pszSecondaryIPAddress,
      System::UInt16 usiSecondaryIPPort,
      System::UInt16 usiResponseTimeoutInMs,
      String^     rgcTransactionTimestamp,
      String^            pszPAN,
      String^            rgcExpiryDate,
      String^            pszStationID,
      System::Int64       i64Amount,
      System::UInt16 usiBatch,
      System::UInt16 usiCurrencyCode,
      System::UInt16 usiNumberOfProducts,
      ???PRODUCT_DATA_DSCR rgProductData[18]???,
      String^ rgcActionCode,
      String^ rgcApprovalCode);

The questionsI have currently are:

- What is the syntax for marshalling the arguments of the function?  I presume I have to marshall the char arrays to Strings, but if I try the follwing syntax:

[MarshalAs(UnmanagedType::ByValTStr, SizeConst = 12)]
String^     rgcTransactionTimestamp,

I get the compiliation error 'API call 'DefineCustomeAttribute" failed.

- How can I successfully marshal an array of a custom datatype?

I think I have been thrown in the deep end of P/Invoke!  Many thanks for your help in advance.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please read this article:
For my own experience, don't use attribute to do this work, you will be crazy and end up with no good solution with this. Using the IntPtr to handle all the pointers and you can parse any data yourself.
batbertramAuthor Commented:
Thanks for the link magicdlf.  

I have tried adapting the solution but have yet to achieve the desired result.  I don't think I have the expertise required!

We are going to opt for a slight recode of the C++ libraries where it is only accepting simple datatypes, with a delimited string passed instead of the array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.