[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Problem using DeviceIoControl from C#

Asked by idelson3647 in .NET, Microsoft Visual C#.Net, Microsoft Visual C++.Net

Tags: C# .NET Runtime.Interop.Services Marshal Win32API

I am converting a C++ application to C#.  The original code contains a number of Win32API calls that must be marshalled.  I have successfully gotten them all to work except the DeviceIoControl calls.  I have gotten some of my DeviceIoControl calls to work but I still can't get 3 of them to work and I don't understand why.  (What I haven't shown is that I call the CreateFile function before the DeviceIoControl call as the CreateFile call works propertly)
For reference the IOWrap in the code is the class where I declare the structures to be used in the marshalled calls.

Below I have listed the structure definitions I'm using, the declaration for the DeviceIoControl import and the code I am having trouble with.  In the code below I have listed the in the appropriate places the error messages I receive from the calls.  Any help would be appreciated as I am out of ideas.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
// these struct definitions are in the class IOWrap
    [StructLayout(LayoutKind.Sequential)]
    public struct STORAGE_PROPERTY_QUERY 
    {
        public STORAGE_PROPERTY_ID PropertyId;          // ID of the property being retrieved
        public STORAGE_QUERY_TYPE QueryType;        // Flags indicating the type of query being performed
        public byte[] AdditionalParameters;                 // Space for additional parameters if necessary
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct STORAGE_DEVICE_DESCRIPTOR 
    {
        ulong Version;              
        ulong Size;                
        byte DeviceType;            
        byte DeviceTypeModifier;   
        bool RemovableMedia;        
        bool CommandQueueing;      
        ulong ProductIdOffset;      
        ulong ProductRevisionOffset
        ulong SerialNumberOffset;   
        STORAGE_BUS_TYPE BusType;  device.
        ulong RawPropertiesLength;  
        byte RawDeviceProperties;  
} 
 
    [StructLayout(LayoutKind.Sequential)]
    public struct STORAGE_ADAPTER_DESCRIPTOR
    {
        public uint Version;
        public uint Size;
        public uint MaximumTransferLength;
        public uint MaximumPhysicalPages;
        public uint AlignmentMask;
        public bool AdapterUsesPio;
        public bool AdapterScansDown;
        public bool CommandQueueing;
        public bool AcceleratedTransfer;
        public byte BusType;
        public ushort BusMajorVersion;
        public ushort BusMinorVersion;
 
    }
[StructLayout(LayoutKind.Sequential)]
        public struct DRIVE_LAYOUT_INFORMATION
        {
            public uint PartitionCount;
            public uint Signature;
            public PARTITION_INFORMATION PartitionEntry;
        }
 
 
   [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public extern static bool DeviceIoControl(IntPtr hDevice,
                                                   uint IoControlCode,
                                                   IntPtr lpInBuffer,
                                                   int InBufferSize,
                                                   IntPtr lpOutBuffer,
                                                   int OutBufferSize,
                                                   ref int lpBytesReturned,
                                                   IntPtr lpOverlapped);
 
STORAGE_PROPERTY_QUERY query = new STORAGE_PROPERTY_QUERY();
           query.PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty;
           query.QueryType = STORAGE_QUERY_TYPE.PropertyStandardQuery;
           query.AdditionalParameters = new byte[1];
 
           int returnedLength = 0;
           int nPtrQryBytes = Marshal.SizeOf(query);
           IntPtr ptrQry = Marshal.AllocHGlobal(nPtrQryBytes);
 
           STORAGE_DEVICE_DESCRIPTOR device = new STORAGE_DEVICE_DESCRIPTOR();
           int nDevicePtrBytes = Marshal.SizeOf(device);
           IntPtr ptrDevice = Marshal.AllocHGlobal(nDevicePtrBytes);
 
 
           bool status = IOWrap.DeviceIoControl(hDevice, IOWrap.IOCTL_STORAGE_QUERY_PROPERTY,
                                                ptrQry, nPtrQryBytes,
                                                ptrDevice, 512, //nDevicePtrBytes,
                                                ref returnedLength,
                                                IntPtr.Zero);
 
           string errMsg = new Win32Exception(Marshal.GetLastWin32Error()).Message;
// this errMsg says "The operation completed successfully"   HOWEVER, the value for returnedLength is 0 and the ptrDevice buffer has not been populated
 
STORAGE_PROPERTY_QUERY query = new STORAGE_PROPERTY_QUERY();
            query.PropertyId = STORAGE_PROPERTY_ID.StorageAdapterProperty;
            query.QueryType = STORAGE_QUERY_TYPE.PropertyStandardQuery;
            query.AdditionalParameters = new byte[1];
 
            // create ptr to query for DeviceIoControl
            int nPtrQryBytes = Marshal.SizeOf(query);
            IntPtr ptrQuery = Marshal.AllocHGlobal(nPtrQryBytes);
 
            STORAGE_ADAPTER_DESCRIPTOR adpDesc = new STORAGE_ADAPTER_DESCRIPTOR();
            int nAdpDescBytes = Marshal.SizeOf(adpDesc);
            IntPtr ptrAdpDesc = Marshal.AllocHGlobal(nAdpDescBytes);
            status = IOWrap.DeviceIoControl(hDevice,
                                            IOWrap.IOCTL_STORAGE_QUERY_PROPERTY,                                                ptrQuery,                                                                           
nPtrQryBytes,
  ptrAdpDesc,                                                                          512,                                                           
ref returnedLen,                                                                   I
ntPtr.Zero);
 
 
            
            string errMsg = new Win32Exception(Marshal.GetLastWin32Error()).Message;
           // note this call returns the following error - "An attempt was made to reference a token that does not exist"
 
// This following call works successfully every time
 IOWrap.DISK_GEOMETRY diskGeometry = new IOWrap.DISK_GEOMETRY();
           int nBytes = Marshal.SizeOf(diskGeometry);
           IntPtr pDiskGeometry = Marshal.AllocHGlobal(nBytes);
 
           status = IOWrap.DeviceIoControl(hDevice, (uint)IOWrap.EIOControlCode.DiskGetDriveGeometry,
                                           IntPtr.Zero, 0,
                                           pDiskGeometry, nBytes,
                                           ref returnedLength, IntPtr.Zero);
 
IOWrap.DRIVE_LAYOUT_INFORMATION layoutInfo = new IOWrap.DRIVE_LAYOUT_INFORMATION();
           nBytes = Marshal.SizeOf(layoutInfo);
           IntPtr pLayoutInfo = Marshal.AllocHGlobal(nBytes);
           returnedLength = 0;
 
           status = IOWrap.DeviceIoControl(hDevice, (uint)IOWrap.EIOControlCode.DiskGetDriveLayout,
                                           IntPtr.Zero, 0, 
                                           pLayoutInfo, 512,
                                           ref returnedLength,
                                           IntPtr.Zero);
 
 
// the following call sometimes works and sometimes throws an OutOfMemory Exception
IOWrap.DRIVE_LAYOUT_INFORMATION layoutInfo = new IOWrap.DRIVE_LAYOUT_INFORMATION();
           nBytes = Marshal.SizeOf(layoutInfo);
           IntPtr pLayoutInfo = Marshal.AllocHGlobal(nBytes);
           returnedLength = 0;
 
          status = IOWrap.DeviceIoControl(hDevice, (uint)IOWrap.EIOControlCode.DiskGetDriveLayout,
                                           IntPtr.Zero, 0, 
                                           pLayoutInfo, 512,
                                           ref returnedLength,
                                           IntPtr.Zero);
[+][-]08/29/09 04:09 PM, ID: 25215762Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/29/09 04:09 PM, ID: 25215763Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/30/09 07:49 AM, ID: 25217840Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/30/09 04:05 PM, ID: 25219782Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/31/09 06:22 AM, ID: 25222539Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/31/09 07:44 AM, ID: 25223192Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/31/09 08:40 AM, ID: 25223686Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/31/09 10:06 AM, ID: 25224433Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/31/09 10:32 AM, ID: 25224652Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/31/09 12:31 PM, ID: 25225627Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/01/09 05:40 AM, ID: 25230910Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/01/09 06:42 AM, ID: 25231464Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/01/09 09:21 AM, ID: 25233152Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/01/09 12:36 PM, ID: 25235127Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/01/09 01:57 PM, ID: 25235972Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/01/09 02:18 PM, ID: 25236282Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/03/09 12:38 PM, ID: 25254182Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/03/09 12:51 PM, ID: 25254290Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/03/09 01:06 PM, ID: 25254402Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/08/09 07:11 AM, ID: 25282024Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: .NET, Microsoft Visual C#.Net, Microsoft Visual C++.Net
Tags: C# .NET Runtime.Interop.Services Marshal Win32API
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
[+][-]09/09/09 05:00 AM, ID: 25290218Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/09/09 09:35 AM, ID: 25292977Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625