Hi all,
I'm a noobie to C# and am having probs with tyring to convert some legacy
C++ code to C#. I've attached some psuedo code to try and explain the
problem:
---code--
int headersize, bodysize, nLibIndex;
HEADER_STRUCT* header = new HEADER_STRUCT();
header->member = value;
.... assign more members
headersize = sizeof(header);
nLibIndex = LEGACY_WRAPPER_ALLOCATE();
LEGACY_WRAPPER_MEMBER( somevalue) ;
.... assign more members
bodysize = LEGACY_WRAPPER_MEMBER(nLibIndex);
//this generates an array of chars and returns a *char
LEGACY_WRAPPER_GENERATE(buffer, nLibIndex);
//create a buffer append the header and body together
byte msg [headersize + bodysize];
memcpy(msg,header,headersize);
memcpy(msg+headersize,buffer,bodysize);
//this Legacy API is a DLL call to the following Legacy_API(int MsgLen,
char* buffer);
int result = Legacy_API_Send(MsgLen, buffer);
-- End code --
Ok here is where I'm confused:
1) How do I retrieve a buffer (char*) from the Generate method? I've tried
doing the following however, I'm not sure how to convert this into a byte
array in which to pass to the Legacy_API call:
LEGACY_WRAPPER_GENERATE(unsigned char* szBuffer, int nIndex);
[DllImport("LegacyWrapper.dll")]
public static extern bool
LEGACY_WRAPPER_GENERATE([MarshalAs(UnmanagedType.LPStr)] out StringBuilder
szBuffer, int nIndex);
2) What is the equivilant in C# to memcpy? How do I perform the above memcpy
code using C# so that I can send the buffer to the API call
(Legacy_API_Send)?
3) What does the DLLImport look like for passing in a char*? I've seen
different posts floating around, however, they seem to differ and I'm
confused as to which method is used in different scenarios. Examples:
[DllImport("LegacyAPI.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Legacy_API_Send(int MsgLen,
[MarshalAs(UnmanagedType.LPStr)] string pBuffer);
[DllImport("LegacyAPI.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Legacy_API_Send(int MsgLen,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pBuffer);
thanks for any help in advance, sorry for the essay, I'm a bit frusturated.
Eric