How to convert this from C to C#,...

K

Kerem Gümrükcü

Hi,

i am a little stuck here, how do i convert this from C Structure
to an structure in C# that can be marshalled:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
TCHAR dbcc_name[1];
}

what to take for GUID and TCHAR[1]?

it is for RegisterDeviceNotification(...):

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient,
ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
uint Flags);

Is the PInvoke declaration ok?

Regards

Kerem

--
 
J

Jeff Gaines

Hi,

i am a little stuck here, how do i convert this from C Structure
to an structure in C# that can be marshalled:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
TCHAR dbcc_name[1];
}

what to take for GUID and TCHAR[1]?

it is for RegisterDeviceNotification(...):

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient,
ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
uint Flags);

Is the PInvoke declaration ok?

Regards

Kerem

I have:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public UInt32 dbcc_size;
public UInt32 dbcc_devicetype;
public UInt32 dbcc_reserved;
public GUID dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
public string dbcc_name;
}

with:

[StructLayout(LayoutKind.Sequential)]
public struct GUID
{
public int a;
public short b;
public short c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] d;
}
 
J

Jeff Winn

The only piece that wouldn't convert directly is the TCHAR field in the
struct.

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_DEVICEINTERFACE {
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string dbcc_name;
}

There are a few differences with what you have and what has been defined in
the header file.

The NotificationFilter argument is defined as LPVOID, which means you should
be passing in an IntPtr there. However if you don't plan on using any of the
other structs available for this API I don't see any problem with what
you're doing since it'll just be type safe and less headache in the long
run. Also, the flags argument is a DWORD - so you should be using an int
rather than a uint.

[DllImport("User32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(
IntPtr hRecipient,
ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
int Flags);

None of this has been tested so take it with a grain of salt.
 
K

Kerem Gümrükcü

Hi Jeff,

this one works:

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public uint dbcc_size;
public uint dbcc_devicetype;
public uint dbcc_reserved;
public Guid dbcc_classguid; // GUID
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string dbcc_name1; // tchar[1]
}

it didnt worked with "ByValArray"

Any Idea for this:

WINSETUPAPI BOOL WINAPI
SetupDiGetClassImageList(
OUT PSP_CLASSIMAGELIST_DATA ClassImageListData
);

i got this:

[StructLayout(LayoutKind.Sequential)]
public class SP_CLASSIMAGE_DATA {
public uint cbSize;
public IntPtr ImageList;
public uint Reserved;
}
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiGetClassImageList(ref SP_CLASSIMAGE_DATA
ClassImageListData);

but on invoke it says that the buffer s invalid (natve error 1784),...why?

Regards

Kerem






Regards

Kerem


--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Jeff Gaines said:
Hi,

i am a little stuck here, how do i convert this from C Structure
to an structure in C# that can be marshalled:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
TCHAR dbcc_name[1];
}

what to take for GUID and TCHAR[1]?

it is for RegisterDeviceNotification(...):

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient,
ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
uint Flags);

Is the PInvoke declaration ok?

Regards

Kerem

I have:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public UInt32 dbcc_size;
public UInt32 dbcc_devicetype;
public UInt32 dbcc_reserved;
public GUID dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
public string dbcc_name;
}

with:

[StructLayout(LayoutKind.Sequential)]
public struct GUID
{
public int a;
public short b;
public short c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] d;
}
 
K

Kerem Gümrükcü

Another point is: How do i Convert/Copy/Whatever a HIMAGELIST
into a ImageList Class/Object,....?

[StructLayout(LayoutKind.Sequential)]
public class SP_CLASSIMAGE_DATA {
public uint cbSize;
public IntPtr ImageList;
public uint Reserved;
}

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiGetClassImageList(ref SP_CLASSIMAGE_DATA
ClassImageListData);

Regards

Kerem

--
 
B

Ben Voigt [C++ MVP]

Kerem said:
Hi,

i am a little stuck here, how do i convert this from C Structure
to an structure in C# that can be marshalled:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
TCHAR dbcc_name[1];
}

what to take for GUID and TCHAR[1]?

it is for RegisterDeviceNotification(...):

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr
hRecipient, ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
uint Flags);

Is the PInvoke declaration ok?

Might I suggest that this is much, much easier to do with C++ interop than
p/invoke?

I'd suggest deriving a class from Form using C++/CLI, using "It Just Works"
interop to call the Win32 API, override the WndProc member function to
handle WM_DEVICECHANGE and call a virtual function after converting things
to .NET data types.

Then from C# you'd just derive from your custom class instead of Form, and
override the OnDeviceChanged handler you declared in C++/CLI.

The whole class definition will be maybe about 20 lines, probably more like
16.
 
K

Kerem Gümrükcü

Hi Ben,

its already done and works fine,...i know that it would
be much much easier t do this with C++ but the entire
project is written in C# so i wont mix it with C++ stuff
and projects. The complete project is done on C#,...

Thanks anyway,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Ben Voigt said:
Kerem said:
Hi,

i am a little stuck here, how do i convert this from C Structure
to an structure in C# that can be marshalled:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE {
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
TCHAR dbcc_name[1];
}

what to take for GUID and TCHAR[1]?

it is for RegisterDeviceNotification(...):

[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr
hRecipient, ref DEV_BROADCAST_DEVICEINTERFACE NotificationFilter,
uint Flags);

Is the PInvoke declaration ok?

Might I suggest that this is much, much easier to do with C++ interop than
p/invoke?

I'd suggest deriving a class from Form using C++/CLI, using "It Just
Works" interop to call the Win32 API, override the WndProc member function
to handle WM_DEVICECHANGE and call a virtual function after converting
things to .NET data types.

Then from C# you'd just derive from your custom class instead of Form, and
override the OnDeviceChanged handler you declared in C++/CLI.

The whole class definition will be maybe about 20 lines, probably more
like 16.
Regards

Kerem
 
K

Kerem Gümrükcü

Hi Jeff,
PInvoke is a good place to look for a lot of these definitions:

Yeah, there are also a lot of examples from me,...:-D

But even with google i couldnt find any definitions, I guess i am
one of the first doing this with windows setup api in that range,...


Regards

Kerem


--
 

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