Marshalling Array 
Author Message
 Marshalling Array

I have a function defined in C++ as follows:
String *npt_ws::npt_array(double (*data) __gc[], int cols, int rows, ...)
This function is called from C# where that array "data" is filled.
I'm trying to convert the managed array data into something unmanaged
to pass it into a dll. So I allocate some memory:
IntPtr datapointer = Marshal::AllocHGlobal(rows*cols);
[I know I could also use (*data)->Length instead of rows*cols]
Then, I call Marshal::Copy as follows:
Marshal::Copy((*data), 0, datapointer, (cols*rows));
But that doesn't work properly it seems. When I run the code I'm
getting some error message:
The instruction at "0x..." referenced memory at "0x...". The memory
could not be "read".
Is what I'm doing incorrect and if yes what is the mistake? I don't
get it!

- Joerg



Mon, 19 Sep 2005 23:36:31 GMT  
 Marshalling Array

--------------------

Quote:

> Subject: Marshalling Array
> Date: Thu, 03 Apr 2003 15:36:31 +0000
> Lines: 19
> Organization: University of Pittsburgh
> User-Agent: KNode/0.7.1
> MIME-Version: 1.0
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7Bit

> Newsgroups: microsoft.public.dotnet.languages.vc
> NNTP-Posting-Host: weebl.phyast.pitt.edu 136.142.113.159
> Path:

cpmsftngxa08.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.
phx.gbl
Quote:
> Xref: cpmsftngxa08.phx.gbl microsoft.public.dotnet.languages.vc:21762
> X-Tomcat-NG: microsoft.public.dotnet.languages.vc

> I have a function defined in C++ as follows:
> String *npt_ws::npt_array(double (*data) __gc[], int cols, int rows, ...)
> This function is called from C# where that array "data" is filled.
> I'm trying to convert the managed array data into something unmanaged
> to pass it into a dll. So I allocate some memory:
> IntPtr datapointer = Marshal::AllocHGlobal(rows*cols);
> [I know I could also use (*data)->Length instead of rows*cols]
> Then, I call Marshal::Copy as follows:
> Marshal::Copy((*data), 0, datapointer, (cols*rows));
> But that doesn't work properly it seems. When I run the code I'm
> getting some error message:
> The instruction at "0x..." referenced memory at "0x...". The memory
> could not be "read".
> Is what I'm doing incorrect and if yes what is the mistake? I don't
> get it!

> - Joerg

I don't see anything you're doing wrong, I tried the following code and it
works just fine:
//t.cpp
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class A {
public:
        String* npt_array(double (__gc*data) __gc[], int cols, int rows);

Quote:
};

String* A::npt_array(double (*data) __gc[], int cols, int rows) {

        Console::WriteLine((*data)[0]);
        IntPtr datapointer = Marshal::AllocHGlobal(rows*cols);

        Marshal::Copy((*data), 0, datapointer, (cols*rows));

        return S"foo";

Quote:
}

//tc.cs
using System;

public class Test {
        public static void Main() {
                A a = new A();
                double[] d = {1,2,3};
                a.npt_array(ref d, 3, 1);
        }

Quote:
}

cl /clr /LD t.cpp
csc /r:t.dll tc.cs
tc.exe
[OUTPUT]
1
--
Ulzii Luvsanbat, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


Wed, 21 Sep 2005 03:49:15 GMT  
 Marshalling Array

Quote:
> I don't see anything you're doing wrong, I tried the following code and it
> works just fine:
> //t.cpp
> #using <mscorlib.dll>
> using namespace System;
> using namespace System::Runtime::InteropServices;

> public __gc class A {
> public:
> String* npt_array(double (__gc*data) __gc[], int cols, int rows);
> };

> String* A::npt_array(double (*data) __gc[], int cols, int rows) {

> Console::WriteLine((*data)[0]);
> IntPtr datapointer = Marshal::AllocHGlobal(rows*cols);

> Marshal::Copy((*data), 0, datapointer, (cols*rows));

> return S"foo";
> }

> //tc.cs
> using System;

> public class Test {
> public static void Main() {
> A a = new A();
> double[] d = {1,2,3};
> a.npt_array(ref d, 3, 1);
> }
> }

> cl /clr /LD t.cpp
> csc /r:t.dll tc.cs
> tc.exe
> [OUTPUT]
> 1

   I must be chasing after a red herring then. In fact, I changed the
code a little bit myself and I still get that "memory could not be read"
error message. This is quite puzzling!

- Joerg



Tue, 20 Sep 2005 23:04:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. C# - Marshalling arrays of fixed length strings to a external dll call.

2. COM - standard proxy/stub marshalling VS typelib marshalling ?

3. COM - standard proxy/stub marshalling VS typelib marshalling ?

4. Marshalling structures of unknown length (CMDINPARAMS, CMDOUTPARAMS)

5. Problem marshalling complex structures (invoking Win32 APIs)

6. Marshalling structs (Aaaaaarrrgh !!!)

7. Marshalling System.Boolean datatype.

8. data marshalling

9. marshalling OleDbConnection objects...

10. Marshalling char** from a C dll to c#

11. Marshalling

12. Marshalling OleDbConnection objects...

 

 
Powered by phpBB® Forum Software