Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:51+00:00 2026-06-04T04:22:51+00:00

I am new to .NET compact framework. I need to call a DeviceIoControl function

  • 0

I am new to .NET compact framework. I need to call a DeviceIoControl function and pass structures as input and output parameters to the IOControl function.

In PInvoke/DeviceIoControl I found how to get access to the function itself. But how can I pass a pointer the structure as InBuf and OutBuf parameter?

The DeviceIoControl is defined as P/Invoke:

[DllImport("coredll", EntryPoint = "DeviceIoControl", SetLastError = true)]
  internal static extern int DeviceIoControlCE(
    int hDevice, int dwIoControlCode,
    byte[] lpInBuffer, int nInBufferSize,
    byte[] lpOutBuffer, int nOutBufferSize,
    ref int lpBytesReturned, IntPtr lpOverlapped);

The structures in question have this layout:

struct Query
{
  int a;
  int b;
  char x[8];
}

struct Response
{
  int result;
  uint32 success;
}

void DoIoControl ()
{
  Query q = new Query();
  Response r = new Response();
  int inSize = System.Runtime.InteropServices.Marshal.SizeOf(q);
  int outSize = System.Runtime.InteropServices.Marshal.SizeOf(r);
  NativeMethods.DeviceIoControlCE((int)handle, (int)IOCTL_MY.CODE,
    ref q, inSize, ref r, outSize, ref bytesReturned, IntPtr.Zero);   
}

Edit:
When I try to compile this code I get the error:

cannot convert from 'ref MyNamespace.Response' to 'byte[]'

How can I pass the address of the struct to the DeviceIoControl function what expects a pointer to byte instead of struct ref?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:22:52+00:00Added an answer on June 4, 2026 at 4:22 am

    The issue is that your P/Invoke declaration doesn’t match your call. DeviceIoControl takes in pointers for the in/out paramters:

    BOOL DeviceIoControl(
      HANDLE hDevice, 
      DWORD dwIoControlCode, 
      LPVOID lpInBuffer, 
      DWORD nInBufferSize, 
      LPVOID lpOutBuffer, 
      DWORD nOutBufferSize, 
      LPDWORD lpBytesReturned, 
      LPOVERLAPPED lpOverlapped
    );
    

    So you can “adjust” your declaration in a lot of ways. The one in the link you provide uses a byte[] probably for convenience where they were using it. In your case, since you’re passing simple structs (i.e. no internal pointers to other data), then the easiest “fix” is to just change you P/Invoke declaration:

    [DllImport("coredll", SetLastError = true)]    
    internal static extern int DeviceIoControl(    
        IntPtr hDevice, 
        IOCTL.MY dwIoControlCode,    
        ref Query lpInBuffer,
        int nInBufferSize,    
        ref Response lpOutBuffer, 
        int nOutBufferSize,    
        ref int lpBytesReturned, 
        IntPtr lpOverlapped);    
    

    And you code should work. Note I also changed the types of the first two parameters to allow making your calling code more clear without casts.

    EDIT 2

    If you find you need different signatures, simply overload the P/Invoke. For example, the Smart Device Framework code has at least 11 overloads for DeviceIoControl. Here are just some of them to give you a flavor:

        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern int DeviceIoControl<TInput, TOutput>(
            IntPtr hDevice,
            uint dwIoControlCode,
            ref TInput lpInBuffer,
            int nInBufferSize,
            ref TOutput lpOutBuffer,
            int nOutBufferSize,
            out int lpBytesReturned,
            IntPtr lpOverlapped)
            where TInput : struct
            where TOutput : struct;
    
        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal unsafe static extern int DeviceIoControl(
            IntPtr hDevice,
            uint dwIoControlCode,
            void* lpInBuffer,
            int nInBufferSize,
            void* lpOutBuffer,
            int nOutBufferSize,
            out int lpBytesReturned,
            IntPtr lpOverlapped);
    
        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern int DeviceIoControl(
            IntPtr hDevice,
            uint dwIoControlCode,
            IntPtr lpInBuffer,
            uint nInBufferSize,
            IntPtr lpOutBuffer,
            uint nOutBufferSize,
            out int lpBytesReturned,
            IntPtr lpOverlapped);
    
        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern int DeviceIoControl(
            IntPtr hDevice,
            uint dwIoControlCode,
            byte[] lpInBuffer,
            int nInBufferSize,
            IntPtr lpOutBuffer,
            int nOutBufferSize,
            out int lpBytesReturned,
            IntPtr lpOverlapped);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to the .NET compact framework (and pretty much new to C# as
I need to display the .NET Compact Framework version number on the screen. I
Old .NET Compact Framework apps still run on Windows Phone 7? Will new apps
I'm brand new to Compact Framework and I need to compile a small project
I am new to .Net framework. I want to develop a PC application, probably
Working in .NET Compact Framework, C#, .Net 3.5, Visual Studio 2008. Targeting Windows Mobile
I am making a program for handheld PDAs using .net 2.0 compact framework and
This question ( .net Compact Framework 4.0 ) asked this back before the release
I have a .NET Compact Framework app that can runs on three windows machines
I´m trying to use a Webservice in a .Net Compact Framework 3.5 Project which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.