P/Invoke tutorials

P

pigeonrandle

Hi,
Anyone know any good tutorials for p/invoke? specifically converting
statements like

WINBASEAPI DWORD WINAPI GetTempPathW( DWORD nBufferLength, LPWSTR
lpBuffer);

into

[DllImport("kernel32.dll")]
public static extern Int32 GetTempPathW (UInt32 nBufferLength, String
lpBuffer);

Is there a definitive guide somewhere?

Thankyou,
James Randle.
 
P

pigeonrandle

Both,

P/Invoke says,

"It's time to stop writing PInvoke signatures from scratch!"

but i want to :0).

Cheers,
James.
Samuel said:
http://pinvoke.net/



------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



Hi,
Anyone know any good tutorials for p/invoke? specifically converting
statements like

WINBASEAPI DWORD WINAPI GetTempPathW( DWORD nBufferLength, LPWSTR
lpBuffer);

into

[DllImport("kernel32.dll")]
public static extern Int32 GetTempPathW (UInt32 nBufferLength, String
lpBuffer);

Is there a definitive guide somewhere?

Thankyou,
James Randle.
 
P

pigeonrandle

More specifically, i would like to be able to check the signatures on
p/invoke since i keep finding errors and have no real reference to
marshaling values/specifying sequential layout/etc.

Cheers,
James.
Both,

P/Invoke says,

"It's time to stop writing PInvoke signatures from scratch!"

but i want to :0).

Cheers,
James.
Samuel said:
http://pinvoke.net/



------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



Hi,
Anyone know any good tutorials for p/invoke? specifically converting
statements like

WINBASEAPI DWORD WINAPI GetTempPathW( DWORD nBufferLength, LPWSTR
lpBuffer);

into

[DllImport("kernel32.dll")]
public static extern Int32 GetTempPathW (UInt32 nBufferLength, String
lpBuffer);

Is there a definitive guide somewhere?

Thankyou,
James Randle.
 
K

KWienhold

I have been struggeling with the same problem for a while now,
p/invoke-ing is a pain to do in some cases.
Unfortunately I haven't found any real guide on how to convert these
signatures. It all depends on the function you want to call, in
many cases there are several implementations that will work. In general
it helps if you know exactly what the function expects and what it
wants to do with the parameters you pass. If you want to call a windows
API function Microsoft's MSDN is probably a good place to start looking
for information on the function, if you call some undocumented third
party function its mostly down to trial and error though...
In the example you posted one DWORD is being translated as Int32 (the
return value), the other as UInt32 (Buffer length), this implies that
the author knows (or assumes) something about the function that isn't
clearly deductable from its signature (in this case that a buffer
length can never be negative, but the return value can).
I have often used pinvoke.net to get a general idea of what to do,
unfortunately the authors there don't seem to check their code much
beyond the specific functionality they need.
If you need to call a function that doesn't work with the
implementation pinvoke supplies you will have to look up more
information on the function itself and make sure your implementation
really fits.
For example, I had a really hard time getting some seldom used parts of
the IStream/IStorage implementation to work, turns out that the pinvoke
implementation passed a single object as a parameter where an array of
objects was expected, since this was normally null anyway the error
wasn't noticable at first, but when it did show up, it was a pain to
figure out.

Sincerely,
Kevin Wienhold

More specifically, i would like to be able to check the signatures on
p/invoke since i keep finding errors and have no real reference to
marshaling values/specifying sequential layout/etc.

Cheers,
James.


P/Invoke says,
"It's time to stop writing PInvoke signatures from scratch!"
but i want to :0).
Cheers,
James.
Samuel said:
http://pinvoke.net/
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 23 Jan 2007 13:04:09 -0800, "pigeonrandle"
Hi,
Anyone know any good tutorials for p/invoke? specifically converting
statements like
WINBASEAPI DWORD WINAPI GetTempPathW( DWORD nBufferLength, LPWSTR
lpBuffer);
into
[DllImport("kernel32.dll")]
public static extern Int32 GetTempPathW (UInt32 nBufferLength, String
lpBuffer);
Is there a definitive guide somewhere?
Thankyou,
James Randle.- Zitierten Text ausblenden -- Zitierten Text anzeigen -
 
P

pigeonrandle

Kevin,
Thanks for your reply. I will keep searching!

James.

I have been struggeling with the same problem for a while now,
p/invoke-ing is a pain to do in some cases.
Unfortunately I haven't found any real guide on how to convert these
signatures. It all depends on the function you want to call, in
many cases there are several implementations that will work. In general
it helps if you know exactly what the function expects and what it
wants to do with the parameters you pass. If you want to call a windows
API function Microsoft's MSDN is probably a good place to start looking
for information on the function, if you call some undocumented third
party function its mostly down to trial and error though...
In the example you posted one DWORD is being translated as Int32 (the
return value), the other as UInt32 (Buffer length), this implies that
the author knows (or assumes) something about the function that isn't
clearly deductable from its signature (in this case that a buffer
length can never be negative, but the return value can).
I have often used pinvoke.net to get a general idea of what to do,
unfortunately the authors there don't seem to check their code much
beyond the specific functionality they need.
If you need to call a function that doesn't work with the
implementation pinvoke supplies you will have to look up more
information on the function itself and make sure your implementation
really fits.
For example, I had a really hard time getting some seldom used parts of
the IStream/IStorage implementation to work, turns out that the pinvoke
implementation passed a single object as a parameter where an array of
objects was expected, since this was normally null anyway the error
wasn't noticable at first, but when it did show up, it was a pain to
figure out.

Sincerely,
Kevin Wienhold

More specifically, i would like to be able to check the signatures on
p/invoke since i keep finding errors and have no real reference to
marshaling values/specifying sequential layout/etc.
Cheers,
James.

pigeonrandle said:
Both,
P/Invoke says,
"It's time to stop writing PInvoke signatures from scratch!"
but i want to :0).
Cheers,
James.
Samuel R. Neff wrote:
http://pinvoke.net/
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 23 Jan 2007 13:04:09 -0800, "pigeonrandle"
Hi,
Anyone know any good tutorials for p/invoke? specifically converting
statements like
WINBASEAPI DWORD WINAPI GetTempPathW( DWORD nBufferLength, LPWSTR
lpBuffer);
into
[DllImport("kernel32.dll")]
public static extern Int32 GetTempPathW (UInt32 nBufferLength, String
lpBuffer);
Is there a definitive guide somewhere?
Thankyou,
James Randle.- Zitierten Text ausblenden -- Zitierten Text anzeigen -- Hide quoted text -- Show quoted text -
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

|I have been struggeling with the same problem for a while now,
| p/invoke-ing is a pain to do in some cases.

Most true, try to do it with the RAS API in a PPC :)

| Unfortunately I haven't found any real guide on how to convert these
| signatures. It all depends on the function you want to call, in
| many cases there are several implementations that will work. In general
| it helps if you know exactly what the function expects and what it
| wants to do with the parameters you pass.

It's a try and error process I think, that's why references like pinvoke.net
are so useful , please colaborate and post there your declarations that
works !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top