473,320 Members | 1,870 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.

pinvoke -- making byte** more efficient

Hi folks,

I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an
array of 3 pointers to bytes, typed as byte**.

[DllImport("foo.dll")]
public static extern void foo(byte **p3pb);

unsafe {
byte[] pB = new byte[3];
pB[0] = // irrelevent in example
pB[1] = // irrelevent in example
pB[2] = // irrelevent in example

fixed(byte **ppb = pB) {
foo(ppb);
}
}

So this requires an array allocation, a pinning operation, and a pinvoke.

I'm gonna be calling this routine a lot. So I thought maybe I could make
this more efficient by doing this:

[StructLayout(LayoutKind.Sequential)]
unsafe struct pByte3 {
public byte *p0;
public byte *p1;
public byte *p2;
}

pByte3 pB;
pB.p0 = // irrelevent in example
pB.p1 = // irrelevent in example
pB.p2 = // irrelevent in example

foo(&pB.p0);

So this removes the array allocation and the pinning operation. It would
seem to be more efficient -- since the struct is a value type, it's not in
any heap and so the interior pointer can just be used directly.

Can anyone see why this would not work or why it not be a gain in terms of
efficiency?
Nov 15 '05 #1
3 1864
"Ted Miller" <te*@nwlink.com> wrote in
news:vq************@corp.supernews.com:
Hi folks, [...]
Can anyone see why this would not work or why it not be a gain in
terms of efficiency?


byte** != byte[]{byte[],byte[],byte[]} in sequential order.

you could have something like this:

byte** = 0x1000

first byte* = 0x1004
second byte* = 0x5000
third byte* = 0x2000

this would break your sequential order.

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
Nov 15 '05 #2
I'm not following you.

The API I'm calling expects that it get a pointer to the 12 bytes that are
interpreted as 3 pointers. What those 3 pointers point to are are not
relevent to the discussion -- although I said they are byte*'s I never said
they were pointing into managed byte arrays.

I was just trying to make sure that when I call the API using my structure,
that the 3 byte*'s in the structure are passed in one block of 12 bytes.

[dllExport("foo.dll")]
public static extern void SomeApi(int *ppi); // expecting a pointer
to 3 integers

If I do this:

struct int3 {
int i0;
int i1;
int i2;
}

int3 i3;
i3.i0 = 1;
i3.i1 = 2;
i3.i2 = 3;

SomeApi(&i3.i0);

does the right thing happen? The compiler allows this, and it seems like it
should work. Also seems like it would be about as efficient as it could be,
since the struct would go on the stack and there's no need to pin anything.
Compare to allocating an array of 3 ints and using a fixed() construct.

int[] i3 = new int[3] { 1,2,3 };

fixed(int *p = i3) {
SomeApi(p);
}

"Peter Koen" <koen-newsreply&snusnu.at> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
"Ted Miller" <te*@nwlink.com> wrote in
news:vq************@corp.supernews.com:
Hi folks,

[...]

Can anyone see why this would not work or why it not be a gain in
terms of efficiency?


byte** != byte[]{byte[],byte[],byte[]} in sequential order.

you could have something like this:

byte** = 0x1000

first byte* = 0x1004
second byte* = 0x5000
third byte* = 0x2000

this would break your sequential order.

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------

Nov 15 '05 #3
Ted,
does the right thing happen?


Yes, this should work fine.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4

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

Similar topics

7
by: savage | last post by:
hey all i need to convert an unmanaged c++ dll and header to c#, ive done the header structures but having problems with pinvoke signatures method 1: typedef int (_stdcall *pOmniConnect) (char...
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...
4
by: TT (Tom Tempelaere) | last post by:
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...
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. ...
45
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
2
by: steve | last post by:
Hi all, I want to understand more about how the pinvoke pinning process works. I'm writing some code that calls DeviceIoControl. DeviceIoControl provides a generic interface to device drivers....
2
by: runner | last post by:
I'm trying to call some functions from OpenSSL library but I'm a bit confused when I have to use pinvoke. first function should create key from some input data, it's declared : void...
4
by: Sagaert Johan | last post by:
Hi I am struggling with pinvoke I have dll a function that is declared as : sf_command (IntPtr sndfile,int command, IntPtr data, int datasize); i need to pass a pointer to a double in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.