473,388 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Converting C++ to C# issues

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

Nov 15 '05 #1
2 1235
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
Nov 15 '05 #2
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...intromcpp.html
http://www.ondotnet.com/pub/a/dotnet...03/mcppp2.html

Plus a book (out of print that you can find for $2 I'm told!)
http://www.amazon.com/exec/obidos/AS...871852-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.
---------
"Eric" <ek******@hotmail.com> wrote in message
news:c2*************@ID-226255.news.uni-berlin.de...
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

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
2
by: mike | last post by:
regards: I follow the following steps to converting from HTML to XHTML http://webpageworkshop.co.uk/main/xhtml_converting My parser is http://htmlparser.sourceforge.net/ Xhtml version is 1.0...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
3
by: Derek Martin | last post by:
Hi there, kind of a dumb question, but it falls in line with not understanding (go figure) every nuance about .Net. Is it relatively straight forward to convert a VB.Net built app to an ASP.Net...
0
by: Sugan | last post by:
Hi, I'm converting a VB 6.0 project to VB 2005. There are few issues, where i got stuck up. They are 1. 'Ambient' is not a member of 'System.Windows.Forms.UserControl' This is the code...
2
by: sparks | last post by:
When I converted a database yesterday I noticed one little thing I had not seen before. Any fields on the forms that were visible=no became visible = yes after the conversion.. Is there any...
5
by: BenG | last post by:
Hi. I have a gridview control on a web form (asp.net 2.0) that's bound to a objectDataSource. The objectdatasource which is bound to a class I've written in the DAL to read and update the database....
8
by: tbh | last post by:
for historical reasons i need to be able to call, from C# under DotNet 2, as COM+ DLL function that returns a "string" which is really an array of seemingly arbitrary bytes (presumably non-zero)....
6
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, I'm defining a report layout using an XML document, plugging data values into fields before outputting the whole doc as an HTML page. I wrap each of the data fields in CDATA section (in case...
1
by: SLC via DBMonster.com | last post by:
Hello I'm a newbie to this and I need help, please My question is, we currently have a IDMS db and will be converting to DB2. What are the things I should do? The IDMS has not been documented very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.