Marshaling an char[] array - Part 2

  • Thread starter Thread starter MarioSchymura
  • Start date Start date
M

MarioSchymura

Hi Everybody.

I've got to use the following function of a C++ DLL:
CT_data (
unsigned short Ctn,
unsigned char *Dad,
unsigned char *Sad,
unsigned short Lc,
unsigned char *Cmd,
unsigned short *Lr,
unsigned char *Rsp
);

I've declared the following

[DllImport("ctscrw2k.dll", SetLastError=true, CharSet=CharSet.Ansi)]
private static extern int CT_data(ushort Ctn, ref ushort Dad, ref ushort
Sad, ushort Lc, char[] Cmd, ref ushort Lr, ref char[] Rsp);


The parameters Cmd and Rsp each are char[] arrays. The last parameter Rsp
is used as an response buffer, an i need to get a reference to the
RSP value to the imported function.

I always get this Exception:
MarshalDirectiveException: Can not marshal parameter #7. Ansi char arrays
can not be Marshaled as byref or as an unmanaged-to-managed parameter.

How can i solve this problem?

Regards
Mario Schymura
 
Hi,
inline

MarioSchymura said:
Hi Everybody.

I've got to use the following function of a C++ DLL:
CT_data (
unsigned short Ctn,
unsigned char *Dad,
unsigned char *Sad,
unsigned short Lc,
unsigned char *Cmd,
unsigned short *Lr,
unsigned char *Rsp
);

I've declared the following

[DllImport("ctscrw2k.dll", SetLastError=true, CharSet=CharSet.Ansi)]
private static extern int CT_data(ushort Ctn, ref ushort Dad, ref ushort
Sad, ushort Lc, char[] Cmd, ref ushort Lr, ref char[] Rsp);

[DllImport("ctscrw2k.dll", SetLastError=true, CharSet=CharSet.Ansi)]
private static extern int CT_data(ushort Ctn, ref byte Dad, ref byte Sad,
ushort Lc, char[] Cmd, ref ushort Lr, [In,Out] char[] Rsp);

unsigned char* Rsp;

The pointer can't change if you only pass a pointer. So if Rsp is really
only a char*, then you must allocate memory for rsp (using new char[...])
before calling the function.


HTH,
greetings

The parameters Cmd and Rsp each are char[] arrays. The last parameter Rsp
is used as an response buffer, an i need to get a reference to the
RSP value to the imported function.

I always get this Exception:
MarshalDirectiveException: Can not marshal parameter #7. Ansi char arrays
can not be Marshaled as byref or as an unmanaged-to-managed parameter.

How can i solve this problem?

Regards
Mario Schymura
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top