473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
}
}
}
Nov 15 '05 #1
3 4145
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*******@DSLE xtreme.COM> wrote in message
news:21******** *************** ***@posting.goo gle.com...
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);
}
}
}

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.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.
Nov 15 '05 #4

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

Similar topics

7
1878
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 the first character is put into the destination struct for each field. void AssignValues(info *source, info *destination) { *destination->name = *source->name;
2
1601
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 (therefore global?) in one of the files. I can pass my information into the structs with no major difficulties, that was easy! anyway now im trying to read from the structure into a
6
3255
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
17
2811
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 free()? #include <stdlib.h> struct stype { int *foo; };
12
3022
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 function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
3
2468
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 unmanaged function from an unmanaged dll. On the dll-side i export this function:
10
2468
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: typedef struct { char name; int employeeID; } employee; My main program creates an array of the employees, with the line: employee employeeTable;
2
1915
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 <=0x3ca9b416 and tcp>=0 and tcp<=23923 and tcp>=0 and tcp<=64582 and tcp<=655355 I have many threads passing different filters of a similar form to
0
9325
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9716
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9571
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.