Returns Wrong Parameter Error for PInvoke Call,..

K

Kerem Gümrükcü

Hi,

i get a "Wrong Parameter" from Marshal.GetLastWin32Error() on using this:


[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid classGuid;
public int devInst;
public ulong reserved;
};

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
internal class SP_DRVINFO_DATA
{
public System.UInt32 cbSize;
public System.UInt32 DriverType;
public System.IntPtr Reserved;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public System.String Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public System.String MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public System.String ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public System.UInt64 DriverVersion;
}

[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError =
true)]
private static extern bool SetupDiGetSelectedDriver
(
System.IntPtr DeviceInfoSet,
[In]ref SP_DEVINFO_DATA DeviceInfoData,
[Out]out SP_DRVINFO_DATA DriverInfoData
);

The "DeviceInfoSet" and "DeviceInfoData" of the Call are valid,
but something goes wrong while marshalling data between the
managed and unmanaged border. Something must be wrong
with my declarations,.....but what? I am certain, that it is some
stuff on the "SP_DRVINFO_DATA",...any ideas?

TIA,...

Regards

Kerem

--
 
P

Pavel Minaev

Hi,

i get a "Wrong Parameter" from Marshal.GetLastWin32Error() on using this:

        [StructLayout(LayoutKind.Sequential)]
        public class SP_DEVINFO_DATA
        {
            public int cbSize;
            public Guid classGuid;
            public int devInst;
            public ulong reserved;

Why do you use "ulong" for "reserved" here? It's ULONG_PTR in the API
declaration, and, unless you're on x64, this is equivalent to
"unsigned long", which is uint in C#, not ulong. Though in this case,
to remain 64-bit clean, it's best to just use UIntPtr.

Anyway, assuming you used Marshal.SizeOf() to set cbSize, you've got a
wrong size in it with your declaration - perhaps it is the reason.
 

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