Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 82,680 times

Contents

Related Categories

DeviceIoControl & USB using Managed C++ & C# - Getting Started

Bill Burris

Getting Started

I started writing the code in C# using Platform Invoke, but was having some problems creating all the data structures to be used with DeviceIoControl.  I decided to write this code in managed C++ instead. When I first started using managed C++, I created .h and .cpp files, just like I did when using unmanaged C++.  I compiled dozens of unmanaged C++ classes and my new managed C++ wrapper code into a managed library dll.  To get it all to work properly I got rid of the Stdafx files and turned off precompiled headers.  When starting work on the USB code, I decided to eliminate the .h files in managed code.  This way the managed C++ code is structured much the same as the C# code.  Since this worked well here, I plan to try this technique on my legacy code wrapper as well.

At first I thought that I would need to have two versions of my data structures, one for the unmanaged code and one for the managed code.  As it turns out, it is easy to create structures in managed C++ and use them in both the C++ & C# code.  I am creating a C++ wrapper function for each USB function, which avoids the problems that I was having with passing different types of data structures to DeviceIoControl using Platform Invoke.

The include files in the managed C++ module:

#include "stdafx.h"  which contains:

  • #using <mscorlib.dll> which is the core of .NET
  • #include <windows.h> for the win32 API
  • #include <stdio.h> After all these years of using C++, I still like the I/O routines I used in C.

#using <UsbCs.dll> some C# classes which I use from the managed C++ code.
#include "ezusbsys.h" Comes with the Cypress development board
#include "usb100.h" Found in the Device Driver Kit, and contains the definitions for constants & structures used when working with the USB.

When using assemblies in managed C++ you need a #using <mydll.dll> statement in your code and need to tell the compiler where to find the assembly, using the Resolve #using References field in the properties for the project.

Then you have the using namespace statements:

using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

After programming in C# for a while, putting those double colons in there, takes some getting use to, even for an old C++ guy.

Comments

  • Re: [4392] DeviceIoControl &amp; USB using Managed C++ &amp; C#

    Posted by Yosef on 16 Aug 2006

     

    I want to build an application to Set/Get USB requests using the OS drivers, Can I do that ? How can I send request to USB for example : Set_Feature ?

     
    ...

  • IJW

    Posted by edinl on 15 Sep 2005

    Hei!

    You can not use IJW if you are programming with C#. You can use P/Invoke where you import unmanaged dll or to make a managed wrapper class in c++ and then write a class in C# to use unmanaged ...

  • Buggy C++ code

    Posted by dmw on 23 Nov 2004

    Be advised that there are bugs in this C++.

    Firstly, there is a line of code that reads

    (long *) &nBytes,

    where nBytes is declared as an int. This will lead to a stack corruption whenever siz...

  • Marshalling an argument of "char*" using IJW metho

    Posted by halise on 04 Nov 2004

    hello,

    i am trying to wrap an unmanaged C++ code to managed one so that i can use it within c#, and i am using IJW - "It Just Works" - method for that. In the code, there is a function which takes...