473,395 Members | 1,706 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,395 software developers and data experts.

Passing an long array from C++ to C#

I'm trying to pass an array of longs from my C++ DLL to C# but I'm not
having much success any help would be great.

Here's what I'm doing and when I compile the C++ I get the error:
error C2664: 'API::Maintenance::Maint::SetArrayElements' : cannot
convert parameter 2 from 'long *' to 'int __gc[]'
extern "C" __declspec(dllexport) void PP_ShowMaint( char *szStr)
{
DialogResult dialogResult;
long *ar = new long[8192];

for (int i=0; i<8192; i++)
ar[i] = i;

API::Maintenance::Maint *MaintScreen = new
API::Maintenance::Maint(szStrl);

MaintScreen->SetArrayElements(8192, ar);

dialogResult = MaintScreen->ShowDialog();
}

C#
public void SetArrayElements(int len,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]int[] ar)
{
int[] bbb = new int[10240];
Array.Copy(ar, 0, bbb, 0, len);
}

Dec 2 '06 #1
2 2637
KH
It would probably be easier to have your C# method accept a long[] (as in
System::Array of long) and in your c++ function allocate a managed array:

cli::array<Int64ar = gcnew cli::array<Int64>(8192);

May not work as you may need a native array in the C++ code because you're
doing something more than just filling it with an int in a loop, but just an
idea ;)

- KH
"ma**********@gmail.com" wrote:
I'm trying to pass an array of longs from my C++ DLL to C# but I'm not
having much success any help would be great.

Here's what I'm doing and when I compile the C++ I get the error:
error C2664: 'API::Maintenance::Maint::SetArrayElements' : cannot
convert parameter 2 from 'long *' to 'int __gc[]'
extern "C" __declspec(dllexport) void PP_ShowMaint( char *szStr)
{
DialogResult dialogResult;
long *ar = new long[8192];

for (int i=0; i<8192; i++)
ar[i] = i;

API::Maintenance::Maint *MaintScreen = new
API::Maintenance::Maint(szStrl);

MaintScreen->SetArrayElements(8192, ar);

dialogResult = MaintScreen->ShowDialog();
}

C#
public void SetArrayElements(int len,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]int[] ar)
{
int[] bbb = new int[10240];
Array.Copy(ar, 0, bbb, 0, len);
}

Dec 2 '06 #2
Beware:

long *ar = new long[8192];

and

long ar[8192];

are not the same.

chanmm
<ma**********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
I'm trying to pass an array of longs from my C++ DLL to C# but I'm not
having much success any help would be great.

Here's what I'm doing and when I compile the C++ I get the error:
error C2664: 'API::Maintenance::Maint::SetArrayElements' : cannot
convert parameter 2 from 'long *' to 'int __gc[]'
extern "C" __declspec(dllexport) void PP_ShowMaint( char *szStr)
{
DialogResult dialogResult;
long *ar = new long[8192];

for (int i=0; i<8192; i++)
ar[i] = i;

API::Maintenance::Maint *MaintScreen = new
API::Maintenance::Maint(szStrl);

MaintScreen->SetArrayElements(8192, ar);

dialogResult = MaintScreen->ShowDialog();
}

C#
public void SetArrayElements(int len,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)]int[] ar)
{
int[] bbb = new int[10240];
Array.Copy(ar, 0, bbb, 0, len);
}
Dec 3 '06 #3

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

Similar topics

3
by: Kevin | last post by:
Hi i'm having a problem passing an arrays of controls on a form to a function or sub. I can use this code for any datatype but when i change 'long' to 'label' in the public sub and i call the sub...
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
18
by: laclac01 | last post by:
Is there a way to pass a 2d array to a function in c++ with out having to specifiy the number of elements in the array. Here is an example #include<iostream> using namespace std; int...
2
by: Manish | last post by:
Hi, I m facing a problem in re-writing a code from VB to C #. We are using a third party DLL to develop a application of Fax. The current application is already written in VB and we have to...
4
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value...
2
by: Neil Munro | last post by:
I'm having some "type" difficulty in passing a .NET array (byref) to a COM based API (Autodesk Inventor). In VB6 the code to return an array of tolerance values is: Dim ToleranceCount As Long...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
7
by: =?Utf-8?B?YmVyaWNr?= | last post by:
New to this, I used to pass an array like this function BytesToString(byref myarray() as byte, somethingelse as long) as long and m = BytesToString(fooBar(), bluenose) This would send...
0
by: MichK | last post by:
Hi, I have a problem with passing a pointer in visual basic. The thing is I receive an array address of a certain API I call in visual basic. There is no value passed, just an address in the...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.