472,353 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 1798
"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 ...
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...
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...
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...
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...
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....
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...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.