Click here to Skip to main content
16,005,038 members
Home / Discussions / C#
   

C#

 
GeneralRe: firewall Pin
JimRivera28-Jun-04 16:14
JimRivera28-Jun-04 16:14 
GeneralDesigners serialization and code generation Pin
Jon_Slaughter27-Jun-04 1:19
Jon_Slaughter27-Jun-04 1:19 
GeneralRe: Designers serialization and code generation Pin
Heath Stewart27-Jun-04 17:54
protectorHeath Stewart27-Jun-04 17:54 
GeneralRe: Designers serialization and code generation Pin
Jon_Slaughter27-Jun-04 19:51
Jon_Slaughter27-Jun-04 19:51 
GeneralRe: Designers serialization and code generation Pin
Heath Stewart28-Jun-04 3:31
protectorHeath Stewart28-Jun-04 3:31 
GeneralRe: Designers serialization and code generation Pin
Heath Stewart27-Jun-04 18:47
protectorHeath Stewart27-Jun-04 18:47 
QuestionHow to get device info using SetupDiGetDeviceInterfaceDetail? Pin
Pain_Elemental27-Jun-04 0:35
Pain_Elemental27-Jun-04 0:35 
AnswerRe: How to get device info using SetupDiGetDeviceInterfaceDetail? Pin
Heath Stewart27-Jun-04 18:12
protectorHeath Stewart27-Jun-04 18:12 
Pain_Elemental wrote:
Is there a way to format the source code in these forums? [code][/code] doesn't seem to work.

Use <code></code> for inline code and <pre></pre> for block code (which is actually what you should use in your case).

First of all, I noticed a lot of problems with your marshaling and your declarations. A DWORD is actually a uint, but an int will work (though you should use MarshalAsAttribute to make sure it's marshaled correctly), and the second field of SP_DEVICE_INTERFACE_DETAIL_DATA is a TCHAR[], which is actually a string using the right encoding. Whenever you see that T in a char array type in Win32 APIs, it means that the string is ANSI on Windows and Unicode on Windows NT (depend on the #defines, of course; but Windows doesn't natively support Unicode). The correct declaration of the two structs and the function are as follows:
[StructLayout(LayoutKind.Sequential)]
private struct SpDeviceInterfaceData // name doesn't matter - keep it simple
{
  public uint cbSize;
  public Guid InterfaceClassGuid;
  public uint Flags;
  public IntPtr Reserved;
  // If you want, a helpful ctor
  public SpDeviceInterfaceData(uint cbSize, Guid InterfaceClassGuid,
    uint Flags)
  {
    this.cbSize = cbSize;
    this.InterfaceClassGuid = InterfaceClassGuid;
    this.Flags = Flags;
    this.Reserved = IntPtr.Zero;
  }
}
 
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct SpDeviceInterfaceDetailData
{
  public uint cbSize;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
    public string DevicePath; // Or public char DevicePath should work
}
 
[StructLayout(LayoutKind.Sequential)]
private struct SpDevInfoData
{
  // Actually, you can reuse SpDeviceInterfaceData - their declaration
  // is the same, but I'll type this out anyway...
  public uint cbSize;
  public Guid ClassGuid;
  public uint DevInst;
  public IntPtr Reserved;
}
 
[DllImport("setupapi.dll")]
private static extern bool SetupDiGetDeviceInterfaceDetail(
  IntPtr DeviceInfoSet,
  ref SpDeviceInterfaceData DeviceInterfaceData,
  out SpDeviceInterfaceDetailData DeviceInterfaceDetailData,
  uint DeviceInterfaceDetailDataSize,
  out uint RequiredSize,
  out SpDevInfoData DeviceInfoData);
From here you should be able to make the right calls. The problem with not marshaling data correctly is that things get in the wrong place. Lets say you just assume that a string field is Unicode string (how strings are stored in .NET, actually) but it's actually an ANSI field because it's running on Windows. Only half the string will be read (depending on the API implementation, a single character might only be read since the second byte may be interpreted as NULL). Or many people make the mistake of thinking that an unmanaged LONG is a long in C# (an Int64). A LONG is only 32 bits. This pushes the stack and extra 4 bytes, and it's that stack that provides the data for which a function executes.

That is most likely the reason for that error your getting. The wrong data was being sent (as far as the function call was concerned) so it returns a "wrong params" error.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: How to get device info using SetupDiGetDeviceInterfaceDetail? Pin
Pain_Elemental27-Jun-04 19:52
Pain_Elemental27-Jun-04 19:52 
GeneralDataGrid :- Events Pin
jk chan26-Jun-04 22:48
jk chan26-Jun-04 22:48 
Generalaccess to controls of a project in another project Pin
fatidarya26-Jun-04 21:08
fatidarya26-Jun-04 21:08 
GeneralRe: access to controls of a project in another project Pin
Stefan Troschuetz26-Jun-04 21:20
Stefan Troschuetz26-Jun-04 21:20 
GeneralRe: access to controls of a project in another project Pin
fatidarya26-Jun-04 23:48
fatidarya26-Jun-04 23:48 
GeneralRe: access to controls of a project in another project Pin
Stefan Troschuetz27-Jun-04 0:12
Stefan Troschuetz27-Jun-04 0:12 
GeneralRe: access to controls of a project in another project Pin
fatidarya27-Jun-04 1:54
fatidarya27-Jun-04 1:54 
GeneralRe: access to controls of a project in another project Pin
Stefan Troschuetz27-Jun-04 2:07
Stefan Troschuetz27-Jun-04 2:07 
QuestionMail Checker 1.0 extension? Pin
razzeler26-Jun-04 18:08
razzeler26-Jun-04 18:08 
AnswerRe: Mail Checker 1.0 extension? Pin
Heath Stewart27-Jun-04 5:51
protectorHeath Stewart27-Jun-04 5:51 
Generalproxy pass https question Pin
steven shingler26-Jun-04 8:54
steven shingler26-Jun-04 8:54 
GeneralRe: proxy pass https question Pin
Heath Stewart26-Jun-04 14:36
protectorHeath Stewart26-Jun-04 14:36 
GeneralRe: proxy pass https question Pin
steven shingler29-Jun-04 6:58
steven shingler29-Jun-04 6:58 
GeneralRe: proxy pass https question Pin
Heath Stewart29-Jun-04 8:45
protectorHeath Stewart29-Jun-04 8:45 
GeneralEvent Order Processing Problem Pin
Gazy26-Jun-04 5:48
Gazy26-Jun-04 5:48 
GeneralRe: Event Order Processing Problem Pin
Heath Stewart26-Jun-04 14:37
protectorHeath Stewart26-Jun-04 14:37 
Generalproblem with threads Pin
Ravikumar_mv26-Jun-04 3:47
Ravikumar_mv26-Jun-04 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.