473,320 Members | 2,180 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,320 software developers and data experts.

Passing array as a parameter from C# to C++ dll

I'm using VS2005 to write a C++ dll to use some Win32 APIs, and I'm calling
the dll from a C# program. I've created the following C# class that I'm using
to create an array to pass to the dll. The C# program creates the array with
more than one dimension, but the C++ dll only sees the first dimension. Here
is the class that I used to create the array (it will eventually contain much
more than a string):

namespace TestObjectClass
{
public class TestObject
{
public TestObject(string Name)
{
name = Name;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
}

This is the C# caller:

....
using System.Runtime.InteropServices;
using TestObjectClass;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestObject[] testObjectArray = new TestObject[4];
testObjectArray[0] = new TestObject("Name0");
testObjectArray[1] = new TestObject("Name1");
testObjectArray[2] = new TestObject("Name2");
testObjectArray[3] = new TestObject("Name3");

// call C++ dll here
processTestObjectArrayClass.ProcessArray(this.Hand le,
testObjectArray);

}
}
public class processTestObjectArrayClass
{
[DllImport(@"..\..\..\Debug\TestArrayDll.dll")]
public static extern int ProcessArray(IntPtr handle, TestObject[]
testObjectArray);
}
}

And here is the C++ dll:
....
#using <TestObjectClass.dll>
....
using namespace TestObjectClass;
....
int __clrcall ProcessArray(HWND hWnd, array<TestObject^>^ testObjectArray)
{
for (int i = 0; i < testObjectArray->Length; i++)
{
pin_ptr<const wchar_t> name =
PtrToStringChars(testObjectArray[i]->Name->ToString());
::MessageBox(hWnd, name, L"Test Object Array", MB_OK);
}
return 0;
}

I created the dll to use shared MFC.

What am I doing to cause only the first dimension in the array to be seen?

Thanks,
David
May 10 '06 #1
5 2219
Hi David,

Thank you posting!

Would you please provide more detailed information about this issue?

1. How about your C++ DLL, Is it a MFC DLL?

2. How do you export the ProcessArray(...) function in that DLL, since you
Pinvoke it in the C# application, why do you specify it as a __clrcall
function?

3. Based on your code snippet, you only create 1 dimension's TestObject
array. If you create a 2 dimensions' array as [4,4], what's that array
length you get in the C++ dll's ProcessArray(...) function?

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
May 10 '06 #2
Gary,

1. Yes, it is an MFC dll.

2. The __clrcall was an attempt on my part to resolve a compilation error,
but I've removed that. The result is the same.

3. Yes, you are correct; the array is only one dimension. I should have used
the term "element," I believe. I create a one-dimension array in C# with 4
elements, and I get a one-dimension array with only 1 element in the C++ dll.

Thanks...David

""Gary Chang[MSFT]"" wrote:
Hi David,

Thank you posting!

Would you please provide more detailed information about this issue?

1. How about your C++ DLL, Is it a MFC DLL?

2. How do you export the ProcessArray(...) function in that DLL, since you
Pinvoke it in the C# application, why do you specify it as a __clrcall
function?

3. Based on your code snippet, you only create 1 dimension's TestObject
array. If you create a 2 dimensions' array as [4,4], what's that array
length you get in the C++ dll's ProcessArray(...) function?

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 10 '06 #3
Hi David,
2. The __clrcall was an attempt on my part to resolve a
compilation error, but I've removed that. The result is the same.


OK, I got it. If so how do you export that function?

Would you please send us the corresponding self-alone repro
projects(zipped) for repro, we need perform some research on them? (please
remove the "online" of my email address alias)

Thanks for your understanding.

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 11 '06 #4
Here is the export for the C++ dll (the .def file):

; TestArrayDll.def : Declares the module parameters for the DLL.

LIBRARY "TestArrayDll"

EXPORTS
; Explicit exports can go here

ProcessArray


""Gary Chang[MSFT]"" wrote:
Hi David,
2. The __clrcall was an attempt on my part to resolve a
compilation error, but I've removed that. The result is the same.


OK, I got it. If so how do you export that function?

Would you please send us the corresponding self-alone repro
projects(zipped) for repro, we need perform some research on them? (please
remove the "online" of my email address alias)

Thanks for your understanding.

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 11 '06 #5
Hi David,

Based on my experience, if you PInvoke a Win32 DLL function, the target
function should be an unmanaged one. However your exported DLL function is
not a pure native function, it has a parameter of a managed type.

In the general convension, the Pinvoke scenario will marshal and pass its
managed array object to a DLL function's unmanaged heap(via defining the
array's length).

Considered your DLL is already an assembly, I suggest you can reference
that assembly directly in your winform application, and use that function
as a managed one directly.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

May 15 '06 #6

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

Similar topics

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...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
by: Newsgroup Posting ID | last post by:
i'm new to c and have gotten my program to run but by passing hardcoded array sizes through the routines, i want to make the array extendable by using realloc but i'd be doing it two routines down,...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
0
by: amazon | last post by:
I have web service that acceping following parameters.. postev.PostEvent(authentication as ws.authentication, name as string,id as string, exdate as date, parameter() as ws.nameparametervaluepair...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
13
by: masso600 | last post by:
char word; in = fopen("test.txt", "r"); while(fscanf(in,"%s",&word)!=EOF) { /* Print all words */ /* printf("%s\n",&word); */
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.