Converting C++ to C# issues

E

Eric Kinskofer

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
 
E

Eric

can anyone point me in the right direction? Should I split up my question to
make it easier to read? or have I asked a dumb question?

thanks.. much appreciated.

Eric
 
S

Sam Gentile [MVP - C#/.NET]

I'm sorry I lost track of the original question but I can help you with C++
Migration Issues. I have a couple of O'Reilly articles that I wrote that may
help:
http://www.ondotnet.com/pub/a/dotnet/2003/01/13/intromcpp.html
http://www.ondotnet.com/pub/a/dotnet/2003/03/03/mcppp2.html

Plus a book (out of print that you can find for $2 I'm told!)
http://www.amazon.com/exec/obidos/ASIN/1861005962/samgentile/104-0871852-3739140

Actually make that $1.97-)

If you restate your question, I will try to help you.

--
-----
Sam Gentile
Microsoft MVP - C#/.NET
..NET Blog http://samgentile.com/blog/

Please do NOT contact me directly but respond to
the newsgroup instead.
 

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

Top