use the conventional C++ code with the managed code

A

Allen Maki

I would be pleased if someone gives me a general idea; how I am going to do
this. I am using visual C++ .NET 2003.



I made a simple program using conventional C++ code. This program accepts
data from the user, does some calculations and outputs the result back to
the user using the console.



Now, I want to use the .NET Framework to interact with the user through
Forms, Dialog Boxes and Controls.



I managed to create all the forms that I need to interact with the user and
they work very well together. But, now I want to use the first program that
I made from conventional C++ with .NET Framework. In other words I want the
user to input data (through Forms and controls) and then I want to
manipulate the data mathematically (using the conventional C++ code) and out
put the result (through other Forms and controls).



So my question is how can I use the conventional C++ code with the managed
code and which file(s) am I going to use? For example, I have the following
files:-



References:

...

...

Source Files:

AssemblyInfo.cpp

Form1.cpp

Form2.cpp

Form3.cpp

Stdafx.cpp



Header Files:

Form1.h

Form2.h

Form3.h

Resource.h

Stdafx.h



Resource Files:

...

...

...

ReadMe.txt



Thanks.



Allen
 
Joined
Sep 15, 2006
Messages
8
Reaction score
0
Allen,

It is a like the old days with VB when you used Windows Dlls. You will need to marshall the arguments between C# and C/C++. Simple structures in C# can map to C/C++ structures, but you need to decorate the C# structure with attributes if you have things like Char[30], etc to fix the C# string length and other attributes to fix structure field alignment.

[ DllImport( @"YourC.dll" ,
CharSet=CharSet.Auto ,
ExactSpelling=true,
EntryPoint="YourMethod",
SetLastError=true,
CallingConvention=CallingConvention.StdCall ) ]
public static extern int C#YourC
(
IntPtr hwnd, // Windows Handle
out IntPtr lphpa, // Address of output buffer
byte [] lpszError // Some String
);


public static byte [] StringToByte (String StringValue)
{
int LengthOfValue = StringValue.Length;
if (LengthOfValue > 0)
{
byte [] bValue = new byte [LengthOfValue];

System.Text.ASCIIEncoding stringConverter = new System.Text.ASCIIEncoding();
int n = stringConverter.GetBytes (StringValue,0,LengthOfValue,bValue,0);
return bValue;
}
return new byte[1];
}
// ********************************************************************************
// ********************************************************************************
// ********************************************************************************
public static String ByteToString (byte [] ByteValue)
{
String StringValue;
if (ByteValue.Length > 0)
{
System.Text.ASCIIEncoding byteConverter = new System.Text.ASCIIEncoding();
StringValue = byteConverter.GetString (ByteValue).TrimEnd ('\x00');
return StringValue;
}
return "";
}
 

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

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top