Problem passing structs in C#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schorschi

    #1

    Problem passing structs in C#?

    Can someone please point out why I am getting an 87 error? I am sure
    it is obvious, but I am new to C# and seem to be having a lot of
    stress understanding managed versus unmanaged code when API calls are
    needed!

    //

    using System;
    using System.Runtime. InteropServices ;

    //

    namespace DeviceClassLibr ary
    {
    /// <summary>
    /// Device Class Library.
    /// </summary>

    //

    //typedef struct _SP_DEVINFO_DAT A
    // {
    // DWORD cbSize;
    // GUID ClassGuid;
    // DWORD DevInst;
    // ULONG_PTR Reserved;
    // } SP_DEVINFO_DATA , *PSP_DEVINFO_DA TA;

    [StructLayout(La youtKind.Sequen tial)]
    public struct _SP_DEVINFO_DAT A
    {
    //

    public Int32 theSize;
    [MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst=16)]
    public byte[] theClass;
    public Int32 theInstance;
    public Int64 theReserved;
    }

    //typedef struct _SP_DEVICE_INTE RFACE_DATA
    // {
    // DWORD cbSize;
    // GUID InterfaceClassG uid;
    // DWORD Flags;
    // ULONG_PTR Reserved;
    // } SP_DEVICE_INTER FACE_DATA, *PSP_DEVICE_INT ERFACE_DATA;

    [StructLayout(La youtKind.Sequen tial)]
    public struct _SP_DEVICE_INTE RFACE_DATA
    {
    //

    public Int32 theSize;
    [MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst=16)]
    public byte[] theClass;
    public Int32 theFlags;
    public Int64 theReserved;
    }

    //typedef struct _SP_DEVICE_INTE RFACE_DETAIL_DA TA
    // {
    // DWORD cbSize;
    // TCHAR DevicePath[ANYSIZE_ARRAY];
    // } SP_DEVICE_INTER FACE_DETAIL_DAT A,
    *PSP_DEVICE_INT ERFACE_DETAIL_D ATA;

    [StructLayout(La youtKind.Sequen tial)]
    public struct _SP_DEVICE_INTE RFACE_DETAIL_DA TA
    {
    //

    public int theSize;
    public byte[] thePath;
    }

    //

    public enum DeviceError
    {
    //

    Success = 0
    }

    public class DeviceClass
    {
    //

    [DllImport("SETU PAPI", SetLastError = true)]
    private static extern IntPtr SetupDiGetClass Devs (
    byte[] theClass,
    IntPtr theEnumerator,
    IntPtr theOwner,
    int theFlags);

    [DllImport("SETU PAPI", SetLastError = true)]
    private static extern bool SetupDiDestroyD eviceInfoList(
    IntPtr theHandle
    );

    [DllImport("SETU PAPI", SetLastError = true)]
    private static extern bool SetupDiEnumDevi ceInfo(
    IntPtr theHandle,
    int theIndex,
    _SP_DEVINFO_DAT A theData);

    // Constructor...

    public DeviceClass()
    {
    //

    }

    // Destructor...

    ~DeviceClass()
    {
    //

    }

    public int Enumerate(strin g theClass, int theCode, ref IntPtr
    theHandle)
    {
    int theResult = (int)DeviceErro r.Success;
    Guid theGuid;

    //


    theGuid = new Guid(theClass);

    theHandle = SetupDiGetClass Devs
    (theGuid.ToByte Array(), IntPtr.Zero, IntPtr.Zero, theCode);

    //

    return (theResult);
    }

    public int UnEnumerate(ref IntPtr theHandle)
    {
    int theResult = (int)DeviceErro r.Success;

    //

    theHandle = IntPtr.Zero;

    return (theResult);
    }

    public int RegistryPropert y(IntPtr theHandle, int theIndex)
    {
    int theResult = (int)DeviceErro r.Success;
    _SP_DEVINFO_DAT A theData = new _SP_DEVINFO_DAT A();
    //Buffer theBuffer;
    //int theSize;
    bool thePropertyOrNo t;

    //

    theData.theSize = Marshal.SizeOf( typeof(_SP_DEVI NFO_DATA));

    thePropertyOrNo t = SetupDiEnumDevi ceInfo(theHandl e, theIndex,
    theData);

    theResult = Marshal.GetLast Win32Error();

    return (theResult);
    }
    }
    }
  • Chris LaJoie

    #2
    Re: Problem passing structs in C#?

    what is SETUPAPI? is it a dll? If so, you should use the full name
    SETUPAPI.dll.
    Also, I wouldn't be able to test your code because I don't have that dll.
    Do you have a C/C++ header file that you pulled those APIs from? if you do,
    that would be extremely helpful in determining if your struct definitions
    are correct.




    "Schorschi" <Schorschi@DSLE xtreme.COM> wrote in message
    news:21027c56.0 308302016.6d849 87b@posting.goo gle.com...[color=blue]
    > Can someone please point out why I am getting an 87 error? I am sure
    > it is obvious, but I am new to C# and seem to be having a lot of
    > stress understanding managed versus unmanaged code when API calls are
    > needed!
    >
    > //
    >
    > using System;
    > using System.Runtime. InteropServices ;
    >
    > //
    >
    > namespace DeviceClassLibr ary
    > {
    > /// <summary>
    > /// Device Class Library.
    > /// </summary>
    >
    > //
    >
    > //typedef struct _SP_DEVINFO_DAT A
    > // {
    > // DWORD cbSize;
    > // GUID ClassGuid;
    > // DWORD DevInst;
    > // ULONG_PTR Reserved;
    > // } SP_DEVINFO_DATA , *PSP_DEVINFO_DA TA;
    >
    > [StructLayout(La youtKind.Sequen tial)]
    > public struct _SP_DEVINFO_DAT A
    > {
    > //
    >
    > public Int32 theSize;
    > [MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst=16)]
    > public byte[] theClass;
    > public Int32 theInstance;
    > public Int64 theReserved;
    > }
    >
    > //typedef struct _SP_DEVICE_INTE RFACE_DATA
    > // {
    > // DWORD cbSize;
    > // GUID InterfaceClassG uid;
    > // DWORD Flags;
    > // ULONG_PTR Reserved;
    > // } SP_DEVICE_INTER FACE_DATA, *PSP_DEVICE_INT ERFACE_DATA;
    >
    > [StructLayout(La youtKind.Sequen tial)]
    > public struct _SP_DEVICE_INTE RFACE_DATA
    > {
    > //
    >
    > public Int32 theSize;
    > [MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst=16)]
    > public byte[] theClass;
    > public Int32 theFlags;
    > public Int64 theReserved;
    > }
    >
    > //typedef struct _SP_DEVICE_INTE RFACE_DETAIL_DA TA
    > // {
    > // DWORD cbSize;
    > // TCHAR DevicePath[ANYSIZE_ARRAY];
    > // } SP_DEVICE_INTER FACE_DETAIL_DAT A,
    > *PSP_DEVICE_INT ERFACE_DETAIL_D ATA;
    >
    > [StructLayout(La youtKind.Sequen tial)]
    > public struct _SP_DEVICE_INTE RFACE_DETAIL_DA TA
    > {
    > //
    >
    > public int theSize;
    > public byte[] thePath;
    > }
    >
    > //
    >
    > public enum DeviceError
    > {
    > //
    >
    > Success = 0
    > }
    >
    > public class DeviceClass
    > {
    > //
    >
    > [DllImport("SETU PAPI", SetLastError = true)]
    > private static extern IntPtr SetupDiGetClass Devs (
    > byte[] theClass,
    > IntPtr theEnumerator,
    > IntPtr theOwner,
    > int theFlags);
    >
    > [DllImport("SETU PAPI", SetLastError = true)]
    > private static extern bool SetupDiDestroyD eviceInfoList(
    > IntPtr theHandle
    > );
    >
    > [DllImport("SETU PAPI", SetLastError = true)]
    > private static extern bool SetupDiEnumDevi ceInfo(
    > IntPtr theHandle,
    > int theIndex,
    > _SP_DEVINFO_DAT A theData);
    >
    > // Constructor...
    >
    > public DeviceClass()
    > {
    > //
    >
    > }
    >
    > // Destructor...
    >
    > ~DeviceClass()
    > {
    > //
    >
    > }
    >
    > public int Enumerate(strin g theClass, int theCode, ref IntPtr
    > theHandle)
    > {
    > int theResult = (int)DeviceErro r.Success;
    > Guid theGuid;
    >
    > //
    >
    >
    > theGuid = new Guid(theClass);
    >
    > theHandle = SetupDiGetClass Devs
    > (theGuid.ToByte Array(), IntPtr.Zero, IntPtr.Zero, theCode);
    >
    > //
    >
    > return (theResult);
    > }
    >
    > public int UnEnumerate(ref IntPtr theHandle)
    > {
    > int theResult = (int)DeviceErro r.Success;
    >
    > //
    >
    > theHandle = IntPtr.Zero;
    >
    > return (theResult);
    > }
    >
    > public int RegistryPropert y(IntPtr theHandle, int theIndex)
    > {
    > int theResult = (int)DeviceErro r.Success;
    > _SP_DEVINFO_DAT A theData = new _SP_DEVINFO_DAT A();
    > //Buffer theBuffer;
    > //int theSize;
    > bool thePropertyOrNo t;
    >
    > //
    >
    > theData.theSize = Marshal.SizeOf( typeof(_SP_DEVI NFO_DATA));
    >
    > thePropertyOrNo t = SetupDiEnumDevi ceInfo(theHandl e, theIndex,
    > theData);
    >
    > theResult = Marshal.GetLast Win32Error();
    >
    > return (theResult);
    > }
    > }
    > }[/color]


    Comment

    • Schorschi

      #3
      Re: Problem passing structs in C#?

      Willy,

      Thx. Will fix that and try again. Hey do you know of any offical
      SetupDi API call examples in VB .NET or C# .NET? Writing in straight
      C is where all the examples are on Google, MSDN, etc. How do we
      request of Microsoft to do a write-up on this? Maybe a rewrite of
      EnumDisks? The official SetupDi API example in the DDK? In .NET C#
      or VB? It is only in straight C.

      Seems like a look of interest in this based on all the questions
      asking for help?

      Schor-

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Problem passing structs in C#?

        Schorschi wrote:
        || Willy,
        ||
        || Thx. Will fix that and try again. Hey do you know of any offical
        || SetupDi API call examples in VB .NET or C# .NET? Writing in straight
        || C is where all the examples are on Google, MSDN, etc. How do we
        || request of Microsoft to do a write-up on this? Maybe a rewrite of
        || EnumDisks? The official SetupDi API example in the DDK? In .NET C#
        || or VB? It is only in straight C.
        ||
        || Seems like a look of interest in this based on all the questions
        || asking for help?
        ||
        || Schor-

        Schor,

        The Device management API's are specialized API's written to be used from an interactive management application (more specifically
        the "control panel" Devices applets and the Device Manager application), some API's expect a UI (HWND) to send messages to .
        Don't expect MSFT will ever release Managed classes wrapping this (and many other) API set.

        I would never use these API's from C# (or VB.NET) using PInvoke, instead I would:

        1 - use ME C++ to write a managed wrapper class if I need additional functionality not offered by 2
        2 - use the System.Manageme nt namespace classes and WMI.

        Following is a small sample using the latter:

        using System;
        using System.Manageme nt;
        // Enum Pnp Registered devices using WMI class Win32_PnPentity
        class App {
        public static void Main() {
        ManagementPath path = new ManagementPath( );
        ManagementClass devs = null;
        path.Server = "."; // local machine
        path.NamespaceP ath = @"root\CIMV2 ";
        path.RelativePa th = @"Win32_PnPenti ty";
        using(devs = new ManagementClass (new ManagementScope (path), path, new ObjectGetOption s(null, new TimeSpan(0,0,0, 2), true)))
        {
        ManagementObjec tCollection moc = devs.GetInstanc es();
        foreach(Managem entObject mo in moc) {

        PropertyDataCol lection devsProperties = mo.Properties;
        foreach (PropertyData devProperty in devsProperties ) {
        Console.WriteLi ne("Property = {0}\t Value = {1}", devProperty.Nam e, devProperty.Val ue);
        }
        }

        }

        }
        }

        Hope this helps.

        Willy.


        Comment

        Working...