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

PInvoke, marshalling pointer to array

Hi people

I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C

int dma_start( const UCHAR* data, UINT data_length )

The function should start with a DMA operation, and I have to use it from my C# program. I am really unsure what type of marshalling I should use.

The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET framework should not move the data around in memory until the method returns. Currently, I've made the interop signature as follows

[DllImport("UsedLib.dll", EntryPoint="dma_start")
[return : MarshalAs(UnmanagedType.I4)
public static extern bool DMAStart (
[MarshalAs(UnmanagedType.U1)] ref byte DMAData
[MarshalAs(UnmanagedType.U4)] uint DMALength
)

Is this a good way to define the pointer to the array (DMAData)
How do I say to the .NET framework that it shouldn't move the byte data around in memory, until the method returns

Thanks a lot
--
Tom Tempelaere.
Nov 16 '05 #1
4 10649
n!
> The function should start with a DMA operation, and I have to use it from
my C# program. I am really unsure what type of marshalling I should use.

The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET framework should not
move the data around in memory until the method returns. Currently, I've
made the interop signature as follows:
[DllImport("UsedLib.dll", EntryPoint="dma_start")]
[return : MarshalAs(UnmanagedType.I4)]
public static extern bool DMAStart (
[MarshalAs(UnmanagedType.U1)] ref byte DMAData,
[MarshalAs(UnmanagedType.U4)] uint DMALength
);

Is this a good way to define the pointer to the array (DMAData)?
How do I say to the .NET framework that it shouldn't move the byte data

around in memory, until the method returns?

When I last exported an array to unmanaged code I used the GCHandle class
inside System.Runtime.InteropServices:

For more complete sample, I have a project on GotDotNet which is a wrapper
around the OpenExr graphics library (still in development). If you search on
http://www.gotdotnet.com for the SharpExr workspace, there is some code that
exports an array to unmanaged code inside the 'InternalReadPixels' method
(within the IO\ExrReader.cs file). Sorry I haven't got a direct link, out
http web connection here is broken and I can't look up the web address at
the moment. Failing that, there's an older code drop at
http://www.btinternet.com/~nfactoria.../exrdotnet.zip but
I'm not sure how old that code is compared to the one on gotdotnet.

It basically looks like:

using System.Runtime.Interop;

private void InternalReadPixels( Rgba[] pixels, int start, int end )
{
if ( disposed )
throw new ObjectDisposedException( ... );

if ( null == pixles )
throw new ArgumentNullException( ... );

GCHandle handle = GCHandle.Alloc( pixels, GCHandleType.Pinned );

SomeImportedFunction( handle.AddrOfPinnedObject() );

handle.Free();
}

Note, the final free refers to the pinning of the object rather than the
actual data.

n!
Nov 16 '05 #2
Hi,

You can also declare it as :
public extern int dma_start ( byte[] data, uint data_length );

An array of bytes is blittable (same layout managed vs unmanaged), therefore
the framework will pin the array and pass a pointer. The pointer remains
valid during function call.
HTH,
greetings
"TT (Tom Tempelaere)" <_N_0SPA|/\|t*******@hotmail.com|/\|APS0_N_> wrote in
message news:6E**********************************@microsof t.com...
Hi people,

I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C:
int dma_start( const UCHAR* data, UINT data_length );

The function should start with a DMA operation, and I have to use it from my C# program. I am really unsure what type of marshalling I should use.
The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET framework should not
move the data around in memory until the method returns. Currently, I've
made the interop signature as follows:
[DllImport("UsedLib.dll", EntryPoint="dma_start")]
[return : MarshalAs(UnmanagedType.I4)]
public static extern bool DMAStart (
[MarshalAs(UnmanagedType.U1)] ref byte DMAData,
[MarshalAs(UnmanagedType.U4)] uint DMALength
);

Is this a good way to define the pointer to the array (DMAData)?
How do I say to the .NET framework that it shouldn't move the byte data around in memory, until the method returns?
Thanks a lot.
---
Tom Tempelaere.

Nov 16 '05 #3
"BMermuys" <so*****@someone.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
You can also declare it as :
public extern int dma_start ( byte[] data, uint data_length );

An array of bytes is blittable (same layout managed vs unmanaged), therefore the framework will pin the array and pass a pointer. The pointer remains
valid during function call.


I wouldn't have to decorate the data parameter? What would be the right
decoration?

Thanks
---
Tom Tempelaere
Nov 16 '05 #4
Hi,

"TT (Tom Tempelaere)" <_N_0SPA|/\|t*******@hotmail.com|/\|APS0_N_> wrote in
message news:2H*********************@phobos.telenet-ops.be...
"BMermuys" <so*****@someone.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
You can also declare it as :
public extern int dma_start ( byte[] data, uint data_length );

An array of bytes is blittable (same layout managed vs unmanaged), therefore
the framework will pin the array and pass a pointer. The pointer remains valid during function call.


I wouldn't have to decorate the data parameter?


You don't have to decorate it.

HTH,
greetings

What would be the right
decoration?

Thanks
---
Tom Tempelaere

Nov 16 '05 #5

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

Similar topics

4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
1
by: Reynardine | last post by:
I am calling a C/C++ DLL from C# and I am marshalling the parameters to the API call by doing a type conversion for each parameter. For example, here is my C++ API method : short int XENO_API...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
2
by: shankgreen | last post by:
My question is related to what happens under the hood when marshalling happens. When you pass a pointer to a structure from managed to unmanaged, and if the unmanaged code modified the structure,...
1
by: Beorne | last post by:
I have a cpp application with this structure: //////////////C++/////////////// typedef struct StatusStructure { char FixedLenString; long LongVariable; double DoubleVariable; BOOL...
2
by: Beorne | last post by:
I have to call a c++ library funtion returning a string with the following signature: char *get_identifier(); Usually when I have to marshal a function with a char* output parameter I do: ...
11
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.