473,547 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr hRecipient,
ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
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 2849
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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr hRecipient,
ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
uint Flags);

Is the PInvoke declaration ok?

Regards

Kerem
I have:

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_D EVICEINTERFACE
{
public UInt32 dbcc_size;
public UInt32 dbcc_devicetype ;
public UInt32 dbcc_reserved;
public GUID dbcc_classguid;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 260)]
public string dbcc_name;
}

with:

[StructLayout(La youtKind.Sequen tial)]
public struct GUID
{
public int a;
public short b;
public short c;
[MarshalAs(Unman agedType.ByValA rray, 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(La youtKind.Sequen tial)]
struct DEV_BROADCAST_D EVICEINTERFACE {
public int dbcc_size;
public int dbcc_devicetype ;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(Unman agedType.ByValT Str, 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 NotificationFil ter 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("User 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(
IntPtr hRecipient,
ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
int Flags);

None of this has been tested so take it with a grain of salt.
"Kerem Gümrükcü" <ka*******@hotm ail.comwrote in message
news:%2******** ********@TK2MSF TNGP04.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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr hRecipient,
ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
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(La youtKind.Sequen tial,CharSet=Ch arSet.Auto)]
public struct DEV_BROADCAST_D EVICEINTERFACE
{
public uint dbcc_size;
public uint dbcc_devicetype ;
public uint dbcc_reserved;
public Guid dbcc_classguid; // GUID
[MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
public string dbcc_name1; // tchar[1]
}

it didnt worked with "ByValArray "

Any Idea for this:

WINSETUPAPI BOOL WINAPI
SetupDiGetClass ImageList(
OUT PSP_CLASSIMAGEL IST_DATA ClassImageListD ata
);

i got this:

[StructLayout(La youtKind.Sequen tial)]
public class SP_CLASSIMAGE_D ATA {
public uint cbSize;
public IntPtr ImageList;
public uint Reserved;
}
[DllImport("setu papi.dll", SetLastError = true)]
private static extern bool SetupDiGetClass ImageList(ref SP_CLASSIMAGE_D ATA
ClassImageListD ata);

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*********@ne wsgroups.nospam schrieb 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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr hRecipient,
ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
uint Flags);

Is the PInvoke declaration ok?

Regards

Kerem

I have:

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_D EVICEINTERFACE
{
public UInt32 dbcc_size;
public UInt32 dbcc_devicetype ;
public UInt32 dbcc_reserved;
public GUID dbcc_classguid;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 260)]
public string dbcc_name;
}

with:

[StructLayout(La youtKind.Sequen tial)]
public struct GUID
{
public int a;
public short b;
public short c;
[MarshalAs(Unman agedType.ByValA rray, 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(La youtKind.Sequen tial)]
public class SP_CLASSIMAGE_D ATA {
public uint cbSize;
public IntPtr ImageList;
public uint Reserved;
}

[DllImport("setu papi.dll", SetLastError = true)]
private static extern bool SetupDiGetClass ImageList(ref SP_CLASSIMAGE_D ATA
ClassImageListD ata);

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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr
hRecipient, ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
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.nos pamschrieb im Newsbeitrag
news:O3******** ******@TK2MSFTN GP04.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 RegisterDeviceN otification(... ):

[DllImport("user 32.dll", SetLastError = true)]
private static extern IntPtr RegisterDeviceN otification(Int Ptr
hRecipient, ref DEV_BROADCAST_D EVICEINTERFACE NotificationFil ter,
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
SetupDiGetClass ImageList(
OUT PSP_CLASSIMAGEL IST_DATA ClassImageListD ata
);
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
3420
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 System.Data.SqlClient.SqlDataReader' Argument '3': cannot convert from 'System.Text.StringBuilder' to 'ref System.Text.StringBuilder'
2
3936
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
5624
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 func(char c). How to convert from String *s to char *c? thanks Keith
12
10061
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 it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
2
1822
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 int.Parse or
14
1784
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
2385
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 textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Select Case Asc(e.KeyChar) <--...
2
20419
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 str (string1);
2
13173
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
5248
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 for it in a mfc project. i searched for it in the web and found some solution like change it's prototype from void to LRESULT i do it but this error...
0
7510
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...
0
7797
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...
0
6032
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...
0
5081
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...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
748
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...

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.