473,805 Members | 1,882 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 2866
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
3432
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
3949
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
5634
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
10093
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
1830
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
1802
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
2398
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) <-- Is this function available in C + +?
2
20475
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
13217
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
5276
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 occurred: error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CshimiHalDlg::*...
0
9716
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
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10366
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
10105
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...
1
7646
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
6876
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();...
1
4323
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
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
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.