Mixed programming method for C # and C + +

Source: Internet
Author: User

Passing arrays

Passing a fixed-length array is simple, not described here. The following code implements the passing of a variable-length array:

C + + code:

int staticelementnumber = 10;

extern "C" afx_api_export bool GetArray (int elementnumber, double *baseaddress)

{

if (Elementnumber < Staticelementnumber)

{

return false;

}

for (int i = 0; i < Staticelementnumber; ++i)

{

Baseaddress[i] = 1/((double) i + 1);

}

return true;

}

extern "C" Afx_api_export int getarrayelementnumber ()

{

return staticelementnumber;

}

C # code:

[DllImport ("MFCDll.dll")]

public static extern bool GetArray (int elementnumber, [MarshalAs (unmanagedtype.lparray, SizeParamIndex = 0)] double[] Bas eaddress);

private void Menuitemfilegetarrayonclicked (object sender, RoutedEventArgs e)

{

Get array data.

int elementnumber = Getarrayelementnumber ();

double[] Doublearray = new Double[elementnumber];

GetArray (Elementnumber, Doublearray);

Show the data.

MainGrid.Children.Clear ();

ListBox listbox = new ListBox ();

foreach (double in Doublearray)

{

LISTBOX.ITEMS.ADD (number);

}

MAINGRID.CHILDREN.ADD (ListBox);

}

With these three features, generally speaking C + + code reuse to the C # platform is a simpler thing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.