Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2006

Tip: Looking for answers? Try searching our database.

C#, Problem trouble shooting PInvoke error.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
karan.shashi@gmail.com - 29 Sep 2006 22:39 GMT
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
Willy Denoyette [MVP] - 30 Sep 2006 19:43 GMT
|I keep getting an error while trying to pass a specific struct to a
| PInvoked method.
[quoted text clipped - 35 lines]
|
| -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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.