473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 DeviceClassLibrary
{
/// <summary>
/// Device Class Library.
/// </summary>

//

//typedef struct _SP_DEVINFO_DATA
// {
// DWORD cbSize;
// GUID ClassGuid;
// DWORD DevInst;
// ULONG_PTR Reserved;
// } SP_DEVINFO_DATA, *PSP_DEVINFO_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVINFO_DATA
{
//

public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theInstance;
public Int64 theReserved;
}

//typedef struct _SP_DEVICE_INTERFACE_DATA
// {
// DWORD cbSize;
// GUID InterfaceClassGuid;
// DWORD Flags;
// ULONG_PTR Reserved;
// } SP_DEVICE_INTERFACE_DATA, *PSP_DEVICE_INTERFACE_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DATA
{
//

public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theFlags;
public Int64 theReserved;
}

//typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA
// {
// DWORD cbSize;
// TCHAR DevicePath[ANYSIZE_ARRAY];
// } SP_DEVICE_INTERFACE_DETAIL_DATA,
*PSP_DEVICE_INTERFACE_DETAIL_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DETAIL_DATA
{
//

public int theSize;
public byte[] thePath;
}

//

public enum DeviceError
{
//

Success = 0
}

public class DeviceClass
{
//

[DllImport("SETUPAPI", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs (
byte[] theClass,
IntPtr theEnumerator,
IntPtr theOwner,
int theFlags);

[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(
IntPtr theHandle
);

[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(
IntPtr theHandle,
int theIndex,
_SP_DEVINFO_DATA theData);

// Constructor...

public DeviceClass()
{
//

}

// Destructor...

~DeviceClass()
{
//

}

public int Enumerate(string theClass, int theCode, ref IntPtr
theHandle)
{
int theResult = (int)DeviceError.Success;
Guid theGuid;

//
theGuid = new Guid(theClass);

theHandle = SetupDiGetClassDevs
(theGuid.ToByteArray(), IntPtr.Zero, IntPtr.Zero, theCode);

//

return (theResult);
}

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

//

theHandle = IntPtr.Zero;

return (theResult);
}

public int RegistryProperty(IntPtr theHandle, int theIndex)
{
int theResult = (int)DeviceError.Success;
_SP_DEVINFO_DATA theData = new _SP_DEVINFO_DATA();
//Buffer theBuffer;
//int theSize;
bool thePropertyOrNot;

//

theData.theSize = Marshal.SizeOf(typeof(_SP_DEVINFO_DATA));

thePropertyOrNot = SetupDiEnumDeviceInfo(theHandle, theIndex,
theData);

theResult = Marshal.GetLastWin32Error();

return (theResult);
}
}
}
Nov 15 '05 #1
3 4114
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" <Sc*******@DSLExtreme.COM> wrote in message
news:21**************************@posting.google.c om...
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 DeviceClassLibrary
{
/// <summary>
/// Device Class Library.
/// </summary>

//

//typedef struct _SP_DEVINFO_DATA
// {
// DWORD cbSize;
// GUID ClassGuid;
// DWORD DevInst;
// ULONG_PTR Reserved;
// } SP_DEVINFO_DATA, *PSP_DEVINFO_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVINFO_DATA
{
//

public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theInstance;
public Int64 theReserved;
}

//typedef struct _SP_DEVICE_INTERFACE_DATA
// {
// DWORD cbSize;
// GUID InterfaceClassGuid;
// DWORD Flags;
// ULONG_PTR Reserved;
// } SP_DEVICE_INTERFACE_DATA, *PSP_DEVICE_INTERFACE_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DATA
{
//

public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theFlags;
public Int64 theReserved;
}

//typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA
// {
// DWORD cbSize;
// TCHAR DevicePath[ANYSIZE_ARRAY];
// } SP_DEVICE_INTERFACE_DETAIL_DATA,
*PSP_DEVICE_INTERFACE_DETAIL_DATA;

[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DETAIL_DATA
{
//

public int theSize;
public byte[] thePath;
}

//

public enum DeviceError
{
//

Success = 0
}

public class DeviceClass
{
//

[DllImport("SETUPAPI", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs (
byte[] theClass,
IntPtr theEnumerator,
IntPtr theOwner,
int theFlags);

[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(
IntPtr theHandle
);

[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(
IntPtr theHandle,
int theIndex,
_SP_DEVINFO_DATA theData);

// Constructor...

public DeviceClass()
{
//

}

// Destructor...

~DeviceClass()
{
//

}

public int Enumerate(string theClass, int theCode, ref IntPtr
theHandle)
{
int theResult = (int)DeviceError.Success;
Guid theGuid;

//
theGuid = new Guid(theClass);

theHandle = SetupDiGetClassDevs
(theGuid.ToByteArray(), IntPtr.Zero, IntPtr.Zero, theCode);

//

return (theResult);
}

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

//

theHandle = IntPtr.Zero;

return (theResult);
}

public int RegistryProperty(IntPtr theHandle, int theIndex)
{
int theResult = (int)DeviceError.Success;
_SP_DEVINFO_DATA theData = new _SP_DEVINFO_DATA();
//Buffer theBuffer;
//int theSize;
bool thePropertyOrNot;

//

theData.theSize = Marshal.SizeOf(typeof(_SP_DEVINFO_DATA));

thePropertyOrNot = SetupDiEnumDeviceInfo(theHandle, theIndex,
theData);

theResult = Marshal.GetLastWin32Error();

return (theResult);
}
}
}

Nov 15 '05 #2
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-
Nov 15 '05 #3
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.Management namespace classes and WMI.

Following is a small sample using the latter:

using System;
using System.Management;
// 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.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_PnPentity";
using(devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, new TimeSpan(0,0,0,2), true)))
{
ManagementObjectCollection moc = devs.GetInstances();
foreach(ManagementObject mo in moc) {

PropertyDataCollection devsProperties = mo.Properties;
foreach (PropertyData devProperty in devsProperties ) {
Console.WriteLine("Property = {0}\t Value = {1}", devProperty.Name, devProperty.Value);
}
}

}

}
}

Hope this helps.

Willy.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bleakcabal | last post by:
Keep in mind my program is written in C ( not C++ ). I have a function which takes two structs as parameters is supposed to put the values of the source struct in the destination struct. Only...
2
by: spleen | last post by:
Hiya, Im a newbie to C so please excuse my ignorance... Ok, so I have several C files, with my functions in, one has main in of course! and I have 2 structs as outside of the funtions...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
3
by: =?Utf-8?B?b2xpZGVt?= | last post by:
Hi! I am confused about the abnormal behaviour of my project. Please consider the following situation: I have written an application in C++.NET 2003 (managed) which loads and executes an...
10
by: joeSquid | last post by:
I am trying to write a C program that makes use of an array of structs, but I am having a problem passing the array as a parameter. The struct is an employee, defined in a header file as: ...
2
by: Shashank | last post by:
Hi, I am passing a tcpdump filter to a function which compiles the filter using pcap_compile and then sets it. Here is the filter, ip>=0x0000 and ip <=0xd9295a3 and ip >=0x0000 and ip...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.