473,395 Members | 2,006 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,395 software developers and data experts.

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

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
9 2837
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Hi, I have the following procedure below: I am getting 2 errors on compile: Argument '2': cannot convert from 'System.Data.SqlClient.SqlDataReader' to 'ref...
2
by: Ketil B | last post by:
Hi All Im geting the compiler error error C2440: '=' : cannot convert from 'short (__thiscall CMSComm::*)(void)' to 'short on this line sCommEvent = m_ctlMSComm.GetCommEvent
3
by: keith | last post by:
In managed C++, there is variable String *s. The variable got value from a C# assembly. Then I need to convert it into char *c in order to call an external function in a dll that accepts parameter...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
2
by: tony | last post by:
Hello!! string number = "13"; int num; Is it exactly the same if I use num = Convert.ToInt32(number); OR num = int.Parse(number) Is it always in this case that I can choose whichever of...
14
by: John Smith | last post by:
Can someone convert from C# into VB this line for me: if (c is System.Web.UI.HtmlControls.HtmlForm)
2
by: Galil | last post by:
I have a subroutine I need to convert from vb.net 2005 to visual C++ ( .net 2005) The function only allows specific characters to be typed into a textbox. the backspace and 0 -9. Private Sub...
2
by: slizorn | last post by:
error is as stated in the topic above: error C2440: '=' : cannot convert from 'char *' to 'char' code is below void handleOneLine(string string1) { char * cstr, *p; int counter; string...
2
by: Frostmur | last post by:
Hi !! I'm trying to convert C code to C++. This is my function: static void (*selection)(void) = NULL; static void (*pick)(GLint name) = NULL; void zprSelectionFunc(void (*f)(void))
3
by: zahraZ | last post by:
Hello , i want a solution for this error: 'static_cast' : cannot convert from 'void (__thiscall CShimiHalDlg::* )(void)' to 'AFX_PMSG' this error occurred when i added a button and writing code...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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.