all groups > dotnet interop > september 2006 >
You're in the

dotnet interop

group:

C#, Problem trouble shooting PInvoke error.



C#, Problem trouble shooting PInvoke error. karan.shashi NO[at]SPAM gmail.com
9/29/2006 2:39:22 PM
dotnet interop: I keep getting an error while trying to pass a specific struct to a
PInvoked method.
The error code returned when calling Marshal.GetLastWin32Error() is
0x06F8.
I'm trying to consume the setupapi function
SetupDiGetDeviceInstallParams.
The problematic struct is SP_DEVINSTALL_PARAMS.


I found this in winerror.h as
#define ERROR_INVALID_USER_BUFFER 1784L

and a call to

System.ComponentModel.Win32Exception(0x06F8);

gives me this further information, "The supplied buffer is not valid
for the requested operation."

Since I couldn't figure out the size of the struct that the function is
expecting,
I decided to put the function call into a while loop initially setting
the struct's cbSize value to 0x0000 and increment it by 0x0001 after
each call to PInvoke function. Calling the function w/ the struct's
cbSize value ranging from 0x0000 to 0x10000, I still get the same exact
result. Always 0x06F8.

I dug deeper into the API's source code and saw that this error value
is only set in one place, where it compares the the passed in struct's
cbSize with sizeof(SP_DEVINSTALL_PARAMS). You'd figure I get a
successful hit trying each size value from 0x0000 to 0x10000 ! I'll try
it again w/ all possible that'll fit in a DWORD in hope to eliminate
the passing in the wrong size issue.

Any ideas anyone??
Has anyone used this API much with C# via PInvoke?

Thanks,

-ks
Re: C#, Problem trouble shooting PInvoke error. Willy Denoyette [MVP]
9/30/2006 8:43:01 PM

[quoted text, click to view]
|I keep getting an error while trying to pass a specific struct to a
| PInvoked method.
| The error code returned when calling Marshal.GetLastWin32Error() is
| 0x06F8.
| I'm trying to consume the setupapi function
| SetupDiGetDeviceInstallParams.
| The problematic struct is SP_DEVINSTALL_PARAMS.
|
|
| I found this in winerror.h as
| #define ERROR_INVALID_USER_BUFFER 1784L
|
| and a call to
|
| System.ComponentModel.Win32Exception(0x06F8);
|
| gives me this further information, "The supplied buffer is not valid
| for the requested operation."
|
| Since I couldn't figure out the size of the struct that the function is
| expecting,
| I decided to put the function call into a while loop initially setting
| the struct's cbSize value to 0x0000 and increment it by 0x0001 after
| each call to PInvoke function. Calling the function w/ the struct's
| cbSize value ranging from 0x0000 to 0x10000, I still get the same exact
| result. Always 0x06F8.
|
| I dug deeper into the API's source code and saw that this error value
| is only set in one place, where it compares the the passed in struct's
| cbSize with sizeof(SP_DEVINSTALL_PARAMS). You'd figure I get a
| successful hit trying each size value from 0x0000 to 0x10000 ! I'll try
| it again w/ all possible that'll fit in a DWORD in hope to eliminate
| the passing in the wrong size issue.
|
| Any ideas anyone??
| Has anyone used this API much with C# via PInvoke?
|
| Thanks,
|
| -ks
|

You don't need to supply the buffer, the API call returns a pointer to a
buffer. All you have to do is pass an "out IntPtr" a third argument and when
the call returns, you marshal the pointer to a managed structure by calling
Marshal.PtrToStructure.

Willy.

AddThis Social Bookmark Button