By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

--
-----------------------
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."

Jul 21 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 21/07/2008 in message <#W**************@TK2MSFTNGP04.phx.gblKerem
Gümrükcü wrote:
>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;
}

--
Jeff Gaines Damerham Hampshire UK
640k ought to be enough for anyone.
(Bill Gates, 1981)
Jul 21 '08 #2

P: n/a
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.
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

--
-----------------------
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."
Jul 21 '08 #3

P: n/a
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" <wh*********@newsgroups.nospamschrieb im Newsbeitrag
news:xn****************@msnews.microsoft.com...
On 21/07/2008 in message <#W**************@TK2MSFTNGP04.phx.gblKerem
Gümrükcü wrote:
>>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;
}

--
Jeff Gaines Damerham Hampshire UK
640k ought to be enough for anyone.
(Bill Gates, 1981)
Jul 21 '08 #4

P: n/a
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

--
-----------------------
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."

Jul 21 '08 #5

P: n/a
Kerem Gümrükcü wrote:
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

Jul 21 '08 #6

P: n/a
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 [C++ MVP]" <rb*@nospam.nospamschrieb im Newsbeitrag
news:O3**************@TK2MSFTNGP04.phx.gbl...
Kerem Gümrükcü wrote:
>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

Jul 21 '08 #7

P: n/a
On 21/07/2008 in message <Oa**************@TK2MSFTNGP02.phx.gblKerem
Gümrükcü wrote:
>Any Idea for this:

WINSETUPAPI BOOL WINAPI
SetupDiGetClassImageList(
OUT PSP_CLASSIMAGELIST_DATA ClassImageListData
);
PInvoke is a good place to look for a lot of these definitions:

http://www.pinvoke.net/

--
Jeff Gaines Damerham Hampshire UK
If it's not broken, mess around with it until it is
Jul 21 '08 #8

P: n/a
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
--
-----------------------
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."

Jul 21 '08 #9

P: n/a
Thanks for all answers,...
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."

Jul 22 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.