Using C++ for USB interfacing

Hi guys, I'm trying to link a Micro-controller board to my C++ program to control the relays on it. The vendor provided a test software in VB that involved entering the COM port number (4 in my case) and clicking a button to sync up with it, the full software and info on the board is here: [Electronics & Automation Engineering T/A Ocean Controls > PC Based > Relayduino USB/RS-485 IO Module (8-28VDC)] under the link 'VB6 test software' link (I've also include .txt attachments of the VB code). I've tried converting the code to C++ along with using namespaces, etc, but I seem to have hit some problems. The code is pretty long so I'll post the relevant things and the errors.

Namespaces being used

using namespace System;
using namespace System::ComponentModel::Container;
using namespace System::ComponentModel;
using namespace System::Windows;
using namespace System::Windows::Forms;

Component Initialisation from VB changed to C++

public ref class zebra : public System::Windows::Forms::Form
      {
      public:
            zebra(void)
            {
                  InitializeComponent();
                  {
            this->components = gcnew System::ComponentModel::Container();
            System::ComponentModel::ComponentResourceManager ^resources = gcnew System::ComponentModel::ComponentResourceManager(zebra::typeid);
            this->SerialPort1 = gcnew System::IO::Ports::SerialPort(this->components);
                  }
            }
}
internal:
array<System::IO::Ports::SerialPort^> ^SerialPort1 = gcnew array<System::IO::Ports::SerialPort^>(this->components + 1);

For the above code, I got the following errors:
error C3083: 'Windows': the symbol to the left of a '::' must be a type
error C3083: 'Forms': the symbol to the left of a '::' must be a type
error C2039: 'Form' : is not a member of 'System'
error C2504: 'Form' : base class undefined
error C2470: 'internal' : looks like a function definition, but there is no parameter list; skipping apparent body
error C3861: 'InitializeComponent': identifier not found
error C2039: 'components' : is not a member of 'zebra'.\zebraDlg.cpp(44) : see declaration of 'zebra'
'ComponentModel': the symbol to the left of a '::' must be a type

and many others. I suspect I may gave failed to include something or that my syntax symbols and function calls are in error.

Lastly, the button that reads the COM port number from a text box to sync with the device (code below).

void CzebraDlg::OnBnClickedButton5()
{
       try
 {
                  if (SerialPort1::IsOpen)
                  {
                        SerialPort1->Close();
                  }
                  SerialPort1->PortName = "COM" + IDC_EDIT2->Text;
                  SerialPort1->Open();
            }
            catch (Exception ^ex)
            {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
            }
      
}

For the button control, I get errors like
error C2227: left of '->Close' must point to class/struct/union/generic type
error C2227: left of '->PortName' must point to class/struct/union/generic type
and others all dealing with the '->' symbols.

I'm not really sure what the compiler is telling me but I suspect it is just a few lines I may not have. Can anyone help? The VB code I derived it from is from the link above. Thanks a lot in advance!! I'm using MFC to build the GUI.

The whole reason for the using statements is so that you can shorten the names used in the rest of the code.

There is a System object created when your code runs. You access things with that object using System. not System::.

This is wrong:

public ref class zebra : public System::Windows::Forms::Form

It should be simply

public ref class zebra : public Form

But, if you want (or think you need) to use the fully qualified names, though, it would be:

public ref class zebra : public System.Windows.Forms.Form

Hi there Paul, I tried what you said and used

public ref class zebra : public System.Windows.Forms.Form

but it gave a error C2504: 'System' : base class undefined

so I tried to use only

public ref class zebra

and that line stopped giving me errors, I've also changed the "::"s to "."s but now, I seem to be having problems with these lines

int components = gcnew System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);

with error C2882: 'System' : illegal use of namespace identifier in expression
and
error C2228: left of '.ComponentModel' must have class/struct/union along with others. The button problems with the '->' symbols are still there too though. The whole part of what I've changed (including the 2 lines above) is below. Thanks a lot!! Hope you can help.

public ref class zebra //: public System.Windows.Forms.Form 
{
      public:
            void InitializeComponent()
                  {
            /*this->*/int components = gcnew System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);
            /*this->*/int SerialPort1 = gcnew System.IO.Ports.SerialPort(this->components);
                  }
}            

internal:
      {
array<System.IO.Ports.SerialPort^> ^SerialPort1 = gcnew array<System.IO.Ports.SerialPort^>(this->components + 1);
      };

Post all of the file, please.

Hi Paul, the full program is really long as this is just a small part of it, here is the code from the beginning, leading up the the point where I'm having problems

// zebraDlg.cpp : implementation file
//
#include "stdafx.h"
#include "zebra.h"
#include "zebraDlg.h"
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include "usb.h"
#include <setupapi.h>
#include <hidsdi.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Container;
using namespace System::Collections;
using namespace System::Windows;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
extern "C" {
#include "hidsdi.h"
}

#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")

#include "bliondi.h" //self defined header

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

public ref class zebra// : public System.Windows.Forms.Form 
{
      public:
            void InitializeComponent()
                  {
            /*this->*/int components = gcnew System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);
            /*this->*/int SerialPort1 = gcnew System.IO.Ports.SerialPort(this->components);
                  }
}            

internal:
      {
//VB TO C++ CONVERTER TODO TASK: In C++, an object cannot reference itself in its class-level declarations:
array<System.IO.Ports.SerialPort^> ^SerialPort1 = gcnew array<System.IO.Ports.SerialPort^>(this->components + 1);
      };

No errors till all the way to the button

void CzebraDlg::OnBnClickedButton5()
{
       try
 {
                  if (SerialPort1.IsOpen)
                  {
                        SerialPort1->Close();
                  }
                  SerialPort1->PortName = "COM" + IDC_EDIT2->Text;
                  SerialPort1->Open();
            }
            catch (Exception ^ex)
            {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
            }

                  
}

Sorry I can't pass you the full code, it is over 40k characters long, even when I removed a lot of lines I don't think I need to post, it is still 30k chars long. I was told to build on this existing project for facial recognition. If you need the full code, I have saved it to a .txt file as well, just let me know where to send it. Thanks a lot!! Hope this helps.

            if (SerialPort1.IsOpen)
            {
                SerialPort1->Close();
            }
            SerialPort1->PortName = "COM" + IDC_EDIT2->Text;
            SerialPort1->Open();

Is SerialPort1 an object (which uses the . notation) or a pointer (which uses the -> notation)?

This isn't right, in either case:

        /*this->*/int SerialPort1 = gcnew System.IO.Ports.SerialPort(this->components);

SerialPort1's type isn't int, that's for sure.

Hi Paul, when I use the code here,

if (SerialPort1.IsOpen)
            {
                SerialPort1->Close();
            }
            SerialPort1->PortName = "COM" + IDC_EDIT2->Text;
            SerialPort1->Open();

I get the following errors
1>.\zebraDlg.cpp(1693) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1695) : error C2227: left of '->PortName' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1695) : error C2882: 'Text' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(1696) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

But when I use it this way,

if (SerialPort1.IsOpen)
                  {
                        SerialPort1.Close();
                  }
                  SerialPort1.PortName = "COM" + IDC_EDIT2.Text;
                  SerialPort1.Open();

1>.\zebraDlg.cpp(1691) : error C2228: left of '.IsOpen' must have class/struct/union
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1693) : error C2228: left of '.Close' must have class/struct/union
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1695) : error C2228: left of '.PortName' must have class/struct/union
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1695) : error C2882: 'Text' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(1696) : error C2228: left of '.Open' must have class/struct/union

These are my errors instead.

Since an error above stated that "SerialPort1" was not part of "SerialPort", I replaced "SerialPort1" with "SerialPort" alone. It seemed to have fixed that one error I've also changed a few things, with some reference to this: SerialPort Class (System.IO.Ports) | Microsoft Learn

I've changed my code a little

// zebraDlg.cpp : implementation file
//
#using <System.dll>
#include "stdafx.h"
#include "zebra.h"
#include "zebraDlg.h"
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include "usb.h"
#include <setupapi.h>
#include <hidsdi.h>
using namespace System;
using namespace System::Object;
using namespace System::MarshalByRefObject;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Component;
using namespace System::ComponentModel::Container;
using namespace System::Collections;
using namespace System::Windows;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::ComponentModel::ComponentResourceManager
extern "C" {
#include "hidsdi.h"
}

#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "hid.lib")

#include "bliondi.h" //self defined header

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//class for error dialog
class CerrorDlg : public CDialog
{
public:
      CerrorDlg() :CDialog(IDD) {} //*******Updated constructor

// Dialog Data
      enum { IDD = IDD_error };
};
//public ref class zebra : public System::Windows::Forms::Form

public ref class SerialPort : public Component;
{
      public:
            void InitializeComponent()
                  {
            this->components = gcnew System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);
            this->SerialPort = gcnew System.IO.Ports.SerialPort(this->components);
                  }
}            

internal:
      {
//VB TO C++ CONVERTER TODO TASK: In C++, an object cannot reference itself in its class-level declarations:
array<System.IO.Ports.SerialPort^> ^SerialPort1 = gcnew array<System.IO.Ports.SerialPort^>(this->components + 1);
      };

But now, I've got this load of errors for the above part.

1>.\zebraDlg.cpp(54) : error C2504: 'Component' : base class undefined
1>.\zebraDlg.cpp(54) : error C2143: syntax error : missing ',' before ';'
1>.\zebraDlg.cpp(65) : error C2470: 'internal' : looks like a function definition, but there is no parameter list; skipping apparent body
1>.\zebraDlg.cpp(59) : error C2039: 'components' : is not a member of 'SerialPort'
1> .\zebraDlg.cpp(54) : see declaration of 'SerialPort'
1>.\zebraDlg.cpp(59) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(60) : error C2882: 'System' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(60) : error C2228: left of '.ComponentModel' must have class/struct/union
1>.\zebraDlg.cpp(60) : error C2228: left of '.ComponentResourceManager' must have class/struct/union
1>.\zebraDlg.cpp(60) : error C2065: 'resources' : undeclared identifier
1>.\zebraDlg.cpp(60) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(61) : error C2273: 'function-style cast' : illegal as right side of '->' operator
1>.\zebraDlg.cpp(61) : error C2061: syntax error : identifier 'System'

As for the button, the new code is as follows

void CzebraDlg::OnBnClickedButton5()
{
       try
 {
                  if (SerialPort.IsOpen)
                  {
                        SerialPort.Close();
                  }
                  SerialPort.PortName = "COM" + IDC_EDIT2.Text;
                  SerialPort.Open();
            }
            catch (Exception ^ex)
            {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
            }

                  
}

And the errors for the button now are

1>.\zebraDlg.cpp(1691) : error C2059: syntax error : '.'
1>.\zebraDlg.cpp(1692) : error C2143: syntax error : missing ';' before '{'
1>.\zebraDlg.cpp(1693) : error C2143: syntax error : missing ';' before '.'
1>.\zebraDlg.cpp(1693) : error C2143: syntax error : missing ';' before '.'
1>.\zebraDlg.cpp(1695) : error C2143: syntax error : missing ';' before '.'
1>.\zebraDlg.cpp(1695) : error C2143: syntax error : missing ';' before '.'
1>.\zebraDlg.cpp(1696) : error C2143: syntax error : missing ';' before '.'
1>.\zebraDlg.cpp(1696) : error C2143: syntax error : missing ';' before '.'

I'm a little puzzled on what SerialPort is, but I think it is an object.

You class definition should include a definition for the serial port instance, like this:

SerialPort *mySerialPort;

Then, the constructor for the class can have a statement like:

mySerialPort = gcnew System.IO.Ports.SerialPort(this->components);

Then, the botton click callback would have stuff like:

if (mySerialPort->IsOpen)
{
    mySerialPort->Close();
}
mySerialPort->PortName = "COM" + IDC_EDIT2->Text;
mySerialPort->Open();

I have no idea why you are creating an array of serial port objects. I suspect that you don't either.

Hi Paul, I've tried various ways to create a definition and this was the one with the fewest errors. Am I doing it right? Sorry, I'm still new at classes and such.

public ref class SerialPort : public Component;
{
      SerialPort *mySerialPort;
      //public:
            void InitializeComponent()
                  {
      components = gcnew System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);
            mySerialPort = gcnew System.IO.Ports.SerialPort(this->components); 
                  }
};

But I still get errors like

1>.\zebraDlg.cpp(54) : error C2504: 'Component' : base class undefined
1>.\zebraDlg.cpp(54) : error C2143: syntax error : missing ',' before ';'
1>.\zebraDlg.cpp(56) : error C3699: '' : cannot use this indirection on type 'SerialPort'
1> compiler replacing '
' with '^' to continue parsing
1>.\zebraDlg.cpp(60) : error C2065: 'components' : undeclared identifier
1>.\zebraDlg.cpp(60) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(61) : error C2882: 'System' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(61) : error C2228: left of '.ComponentModel' must have class/struct/union
1>.\zebraDlg.cpp(61) : error C2228: left of '.ComponentResourceManager' must have class/struct/union
1>.\zebraDlg.cpp(61) : error C2065: 'resources' : undeclared identifier
1>.\zebraDlg.cpp(61) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(63) : error C2065: 'mySerialPort' : undeclared identifier
1>.\zebraDlg.cpp(63) : error C2061: syntax error : identifier 'System'

Like you said, I removed the 'internal' array parts thanks for the heads up, it was simply put in when converting.

The new button code is as you suggested as follows

void CzebraDlg::OnBnClickedButton5()
{
       try
                  {
                  if (mySerialPort->IsOpen)
                        {
                              mySerialPort->Close();
                        }
                  mySerialPort->PortName = "COM" + IDC_EDIT2->Text;
                  mySerialPort->Open();
                  }
            catch (Exception ^ex)
                  {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
                  }      
}

But since the class was not declared properly I'm still having errors like
1>.\zebraDlg.cpp(1693) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1695) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1697) : error C2227: left of '->PortName' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1697) : error C2227: left of '->Text' must point to class/struct/union/generic type
1> type is 'int'
1>.\zebraDlg.cpp(1698) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

Which part of my code needs changing? Thanks again!

All the errors in the 2nd block of code come from that fact that the mySerialPort object is not properly defined. You need to first concentrate in getting a serial port object created.

There are 7 SerialPort constructors. The most confusing one, to me, is the one that you are trying to use. If seems it would be far simpler to create a serial port object like this:

SerialPort *mySerialPort = gcnew SerialPort("COM3"); // Or whatever the correct port name is

The method that you are trying to use wants an instance (singular) of an IContainer interface. Do you know specifically what that overloaded method does with the container? Why is it necessary for you to supply an IContainer interface? Why is your singular IContainer object named components? What does the declaration for components look like?

Hi Paul, I was a little worried about whether the board might take different COM ports, but it seems to always be COM4 so your method seems better. I'm not too sure where to create the object though, is this right?

public ref class SerialPort //: public Component;
{
      public:
            SerialPort *mySerialPort = gcnew SerialPort("COM4");
};

I commented out the "public Component" part as it was giving me a
1>.\zebraDlg.cpp(54) : error C2504: 'Component' : base class undefined

As for this line, the errors it is giving me are below, even when I try different locations.

SerialPort *mySerialPort = gcnew SerialPort("COM4");
};

1>.\zebraDlg.cpp(57) : error C3699: '' : cannot use this indirection on type 'SerialPort'
1> compiler replacing '
' with '^' to continue parsing
1>.\zebraDlg.cpp(57) : error C3845: 'SerialPort::mySerialPort': only static data members can be initialized inside a ref class or value type

It only is connecting to COM4, so I guess I don't need the "IContainer" parts like you said. Thanks, hope you can help.

public ref class SerialPort

The class SerialPort already exists. You are now trying to create a new serial class containing an instance of the class you are defining. That's recursion.

Move the mySerialPort instance to the zebra (or whatever it was) class.

Alright, I've moved the line

SerialPort *mySerialPort = gcnew SerialPort("COM4");

To the button here

void CzebraDlg::OnBnClickedButton5()
{
            SerialPort *mySerialPort = gcnew SerialPort("COM4");
       try
                  {
                  
                  if (mySerialPort->IsOpen)
                        {
                              mySerialPort->Close();
                        }
                  //mySerialPort->PortName = "COM4"// + IDC_EDIT2->Text;
                  mySerialPort->Open();
                  }
            catch (Exception ^ex)
                  {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
                  }      
}

So the SerialPort class is

public ref class SerialPort //: public Component;
{
};

But I'm still getting

1> compiler replacing '*' with '^' to continue parsing
1>.\zebraDlg.cpp(1692) : error C3673: 'SerialPort' : class does not have a copy-constructor
1>.\zebraDlg.cpp(1696) : error C2039: 'IsOpen' : is not a member of 'SerialPort'
1> .\zebraDlg.cpp(55) : see declaration of 'SerialPort'
1>.\zebraDlg.cpp(1698) : error C2039: 'Close' : is not a member of 'SerialPort'
1> .\zebraDlg.cpp(55) : see declaration of 'SerialPort'
1>.\zebraDlg.cpp(1701) : error C2039: 'Open' : is not a member of 'SerialPort'
1> .\zebraDlg.cpp(55) : see declaration of 'SerialPort'
1>zebra.cpp

Did I put it in the right place?

There were two parts to my last reply. You took care of one of them.

Microsoft provides a SerialPort class that you want to create an instance of. You are also trying to define your own SerialPort class. Why?

Get rid of your SerialPort class altogether.

If this is managed code, which I've never bothered with since C# is so much easier to use for building Windows applications, then you might want to replace the * in the method with a ^, since the compiler is trying to do that for you.

Alright, I've removed the SerialPort class altogether and left the button code as above (code below)

void CzebraDlg::OnBnClickedButton5()
{
            SerialPort *mySerialPort = gcnew SerialPort("COM4");
       try
                  {
                        
                  if (mySerialPort->IsOpen)
                        {
                              mySerialPort->Close();
                        }
                  //mySerialPort->PortName = "COM4"// + IDC_EDIT2->Text;
                  mySerialPort->Open();
                  }
            catch (Exception ^ex)
                  {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
                  }      
}

The problem with the "*" seems to have disappeared but I still have the following errors

1>.\zebraDlg.cpp(1692) : error C2065: 'SerialPort' : undeclared identifier
1>.\zebraDlg.cpp(1692) : error C2065: 'mySerialPort' : undeclared identifier
1>.\zebraDlg.cpp(1692) : error C2061: syntax error : identifier 'SerialPort'
1>.\zebraDlg.cpp(1697) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1699) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1702) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

Did I put it at the right place? Thanks.

Try using the fully qualified name:
System.IO.Ports.SerialPort ^mySerialPort = gcnew System.IO.Ports.SerialPort(...)

Hmm, I've changed it to the full name

void CzebraDlg::OnBnClickedButton5()
{
            //SerialPort *mySerialPort = gcnew SerialPort("COM4");
      System.IO.Ports.SerialPort ^mySerialPort = gcnew System.IO.Ports.SerialPort("COM4");
       try
                  {
                        
                  
                  if (mySerialPort->IsOpen)
                        {
                              mySerialPort->Close();
                        }
                  //mySerialPort->PortName = "COM4"// + IDC_EDIT2->Text;
                  mySerialPort->Open();
                  }
            catch (Exception ^ex)
                  {
                  CerrorDlg  cid;  //*** Create an instance of our dialog
                  cid.DoModal(); //Display Dialog box
                  }      
}

But now the errors are different,

1>.\zebraDlg.cpp(1693) : error C2882: 'System' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(1693) : error C2228: left of '.IO' must have class/struct/union
1>.\zebraDlg.cpp(1693) : error C2228: left of '.Ports' must have class/struct/union
1>.\zebraDlg.cpp(1693) : error C2228: left of '.SerialPort' must have class/struct/union
1>.\zebraDlg.cpp(1693) : error C2065: 'mySerialPort' : undeclared identifier
1>.\zebraDlg.cpp(1693) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(1698) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1700) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1703) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

Maybe I missed using a namespace? Here are the ones I'm using

#using <System.dll>
#include "stdafx.h"
#include "zebra.h"
#include "zebraDlg.h"
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include "usb.h"
#include <setupapi.h>
#include <hidsdi.h>
using namespace System;
using namespace System::IO;
using namespace System::Threading;

Are you using Visual Studio to create/edit this application? If so, which version? If not, what are you using?

Yup, its Visual Studio, its the 2005 version

PM me, and send me your code, please (zip up the whole solution directory). I'll see if I can figure out why your getting these errors.