Sponsors of Wiimoteproject.com
IR Pens for Wiimote Whiteboard
Wiimote Project » General Wiimote and Technology » Programmers Den » getting past C# (not a good start)
Pages: [1]
  Print  
Author Topic: getting past C# (not a good start)  (Read 7787 times)
0 Members and 1 Guest are viewing this topic.
*
Karma: +0/-0
Posts: 7
Offline Offline
View Profile Email
« on: April 08, 2008, 07:44:54 AM »

hi all, hope things are great in WiiLand! im just in the process of getting this stuff together, but ive hit a wall with the C# part. There is loads of great info here regarding hardware etc, but v little about the software side. am i the only non-programmer?

ive been trying to get through this walkthrough http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx but i get stuck on the compiling process! ive done the basic tutorial for C# (simple web browser) hoping it would help, but im stil pretty lost. what exactly am i trying to achieve..? make a little program that automatically runs, and connects the Wiimote??

if so.. where do i want to put these 2 blocks of code (taken from the code4fun site) ?

Code:
// read/write handle to the device
private SafeFileHandle mHandle;

// a pretty .NET stream to read/write from/to
private FileStream mStream;



................................
    }

    // move to the next device
    index++;
}

// 6. clean up our list
HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo);


Code:
private void OnReadData(IAsyncResult ar)
{
    // grab the byte buffer
    byte[] buff = (byte[])ar.AsyncState;

    // end the current read
    mStream.EndRead(ar);

    // start reading again
    BeginAsyncRead();
   
    // handle data....
}


I tried to paste them in as below...but i was getting compiling/build errors.


Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<IN HERE??????????????????
        }
    }
}







If you could help me out, ud be helping a stressed out student, close to insanity Smiley
thanks for all the great info so far, its a great resource!
rory
« Last Edit: April 09, 2008, 06:22:40 PM by ZaPPZion » Logged
Guru
*
Karma: +11/-1
Posts: 137
Offline Offline
View Profile
« Reply #1 on: April 08, 2008, 11:14:39 PM »

in the graphical part of visual C# (the one where you can place buttons etc) double click the form. That'll generate a Form_Load event handler. Put your code in there, leave the InitializeComponent() method where it is, since it's incredibly handy Tongue it kindof makes your program show like a program (i know there's a lot more to it, but it's not worth explaining;))

so what you'll get is:
Code:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load()
        {
            yourMethods(); // <<<<<<<<<in here :)
        }
    }
}
« Last Edit: April 08, 2008, 11:17:15 PM by ZaPPZion » Logged
*
Karma: +0/-0
Posts: 7
Offline Offline
View Profile Email
« Reply #2 on: April 09, 2008, 06:19:27 AM »

Thanks for the reply ZappZion, although i am still getting countless errors !
i have literally just copied and pasted the code from that Walkthrough into the place where you showed me. It mentions something about P-Invoke in the walkthrough, so i am kinda getting thrown off the trail a bit. am i missing a library of somesort?
thanks alot for your time and help

heres how my code is looking::

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load()
        {
            // read/write handle to the device
            private SafeFileHandle mHandle;

            // a pretty .NET stream to read/write from/to
            private FileStream mStream;
            bool found = false;
            Guid guid;
            uint index = 0;

            // 1. get the GUID of the HID class
            HIDImports.HidD_GetHidGuid(out guid);

            // 2. get a handle to all devices that are part of the HID class
            IntPtr hDevInfo = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HIDImports.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);

            // create a new interface data struct and initialize its size
            HIDImports.SP_DEVICE_INTERFACE_DATA diData = new HIDImports.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            // 3. get a device interface to a single device (enumerate all devices)
            while(HIDImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                // create a detail struct and set its size
                HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
                diDetail.cbSize = 5; //should be: (uint)Marshal.SizeOf(diDetail);, but that's the wrong size

                UInt32 size = 0;

                // get the buffer size for this device detail instance (returned in the size parameter)
                HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                // actually get the detail struct
                if(HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    // open a read/write handle to our device using the DevicePath returned
                    mHandle = HIDImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);

                    // 4. create an attributes struct and initialize the size
                    HIDImports.HIDD_ATTRIBUTES attrib = new HIDImports.HIDD_ATTRIBUTES();
                    attrib.Size = Marshal.SizeOf(attrib);

                    // get the attributes of the current device
                    if(HIDImports.HidD_GetAttributes(mHandle.DangerousGetHandle(), ref attrib))
                    {
                        // if the vendor and product IDs match up
                        if(attrib.VendorID == VID && attrib.ProductID == PID)
                        {
                            // 5. create a nice .NET FileStream wrapping the handle above
                            mStream = new FileStream(mHandle, FileAccess.ReadWrite, REPORT_LENGTH, true);
                        }
                        else
                            mHandle.Close();
                    }
                }

                // move to the next device
                index++;
            }

            // 6. clean up our list
            HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo);

            // sure, we could find this out the hard way using HID, but trust me, it's 22
            private const int REPORT_LENGTH = 22;

            // report buffer
            private byte[] mBuff = new byte[REPORT_LENGTH];

            private void BeginAsyncRead()
            {
                // if the stream is valid and ready
                if(mStream.CanRead)
                {
                    // create a read buffer of the report size
                    byte[] buff = new byte[REPORT_LENGTH];

                    // setup the read and the callback
                    mStream.BeginRead(buff, 0, REPORT_LENGTH, new AsyncCallback(OnReadData), buff);
                }
            }

            private void OnReadData(IAsyncResult ar)
            {
                // grab the byte buffer
                byte[] buff = (byte[])ar.AsyncState;

                // end the current read
                mStream.EndRead(ar);

                // start reading again
                BeginAsyncRead();
               
                // handle data....
           }
       }


     }
 }
« Last Edit: April 09, 2008, 06:21:59 PM by ZaPPZion » Logged
Guru
*
Karma: +11/-1
Posts: 137
Offline Offline
View Profile
« Reply #3 on: April 09, 2008, 06:21:37 PM »

I tidy'd up your code Smiley
it still gives a "HIDImports does not exist in this context" error, but that's because i dont have that class in the project i tried with.

here's the tidy code:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32.SafeHandles;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // read/write handle to the device
        private SafeFileHandle mHandle;

        // a pretty .NET stream to read/write from/to
        private FileStream mStream;
        bool found = false;
        Guid guid;
        uint index = 0;

        // sure, we could find this out the hard way using HID, but trust me, it's 22
        private const int REPORT_LENGTH = 22;

        // report buffer
        private byte[] mBuff = new byte[REPORT_LENGTH];

        public Form1()
        {
            InitializeComponent();
        }


        private void BeginAsyncRead()
        {
            // if the stream is valid and ready
            if (mStream.CanRead)
            {
                // create a read buffer of the report size
                byte[] buff = new byte[REPORT_LENGTH];

                // setup the read and the callback
                mStream.BeginRead(buff, 0, REPORT_LENGTH, new AsyncCallback(OnReadData), buff);
            }
        }

        private void OnReadData(IAsyncResult ar)
        {
            // grab the byte buffer
            byte[] buff = (byte[])ar.AsyncState;

            // end the current read
            mStream.EndRead(ar);

            // start reading again
            BeginAsyncRead();

            // handle data....
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 1. get the GUID of the HID class
            HIDImports.HidD_GetHidGuid(out guid);

            // 2. get a handle to all devices that are part of the HID class
            IntPtr hDevInfo = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HIDImports.DIGCF_DEVICEINTERFACE);// | HIDImports.DIGCF_PRESENT);

            // create a new interface data struct and initialize its size
            HIDImports.SP_DEVICE_INTERFACE_DATA diData = new HIDImports.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            // 3. get a device interface to a single device (enumerate all devices)
            while (HIDImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                // create a detail struct and set its size
                HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA();
                diDetail.cbSize = 5; //should be: (uint)Marshal.SizeOf(diDetail);, but that's the wrong size

                UInt32 size = 0;

                // get the buffer size for this device detail instance (returned in the size parameter)
                HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                // actually get the detail struct
                if (HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    // open a read/write handle to our device using the DevicePath returned
                    mHandle = HIDImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);

                    // 4. create an attributes struct and initialize the size
                    HIDImports.HIDD_ATTRIBUTES attrib = new HIDImports.HIDD_ATTRIBUTES();
                    attrib.Size = Marshal.SizeOf(attrib);

                    // get the attributes of the current device
                    if (HIDImports.HidD_GetAttributes(mHandle.DangerousGetHandle(), ref attrib))
                    {
                        // if the vendor and product IDs match up
                        if (attrib.VendorID == VID && attrib.ProductID == PID)
                        {
                            // 5. create a nice .NET FileStream wrapping the handle above
                            mStream = new FileStream(mHandle, FileAccess.ReadWrite, REPORT_LENGTH, true);
                        }
                        else
                            mHandle.Close();
                    }
                }

                // move to the next device
                index++;
            }

            // 6. clean up our list
            HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo);
        }
    }
}

btw: i modified your posts a bit, it was getting a long page, because you were using quote tags instead of code tags. It's not a big deal, but can you please use code tags instead of quote's on large code snippets? thx in advantage;)
« Last Edit: April 09, 2008, 06:24:38 PM by ZaPPZion » Logged
*
Karma: +0/-0
Posts: 10
Offline Offline
View Profile
« Reply #4 on: April 20, 2008, 08:25:40 AM »

I am also a non-programmer. How do you run the program?
Logged
Guru
*
Karma: +11/-1
Posts: 137
Offline Offline
View Profile
« Reply #5 on: April 20, 2008, 05:41:47 PM »

If you want to edit stuff, then you should use Visual C# express 2005 or 2008. It's free Wink
Logged
Support Admin
*
Karma: +11/-3
Posts: 566
Offline Offline
View Profile WWW
« Reply #6 on: April 20, 2008, 06:19:31 PM »

yes you need something to compile the code... and skills of programming...
Logged

*
Karma: +0/-0
Posts: 10
Offline Offline
View Profile
« Reply #7 on: April 21, 2008, 07:45:43 PM »

My apologies in not providing more detail.
What I meant by how do I run the program is how do I check to make sure it is doing what it is supposed to do? I copied and pasted the code language into the "Code View" box in C#, but there isn't a "run" button that I am aware of.

I did select "Build Solution," and received some errors
The first error reads
      The type of namespace name 'SafeFileHandle' could not be found (are you missing a using directive or an assembly reference?)
It then points to this as the source of the error
       // read/write handle to the device
        private SafeFileHandle mHandle;   

The second error reads
     The type or namespace name 'FileStream' could not be found (are you missing a using directive or an assembly reference?)
It then points to this as the source of the error
        // a pretty .NET stream to read/write from/to
        private FileStream mStream;
        bool found = false;
        Guid guid;
        uint index = 0;


What do i do with this?
Logged
Support Admin
*
Karma: +11/-3
Posts: 566
Offline Offline
View Profile WWW
« Reply #8 on: April 21, 2008, 08:04:53 PM »

namespaces are the ones in the beginning of the code

here are the ones from above...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32.SafeHandles;
Logged

*
Karma: +0/-0
Posts: 10
Offline Offline
View Profile
« Reply #9 on: April 21, 2008, 09:21:06 PM »

I copied and pasted the namespaces and unfortunately, I now get all sorts of errors, mainly centered on not having a name HIDImports. A few "missing name errors" appeared for Marshal, and one for VID and PID.

I also renamed my project to "Wii 1st Try." Does that have anything to do with it.
Logged
Guru
*
Karma: +11/-1
Posts: 137
Offline Offline
View Profile
« Reply #10 on: April 22, 2008, 03:20:43 PM »

you should declare some variable HIDImports somewhere in the class.
like this:

public HIDImports HIDImports;

then in your form_load method, put this line:

HIDImports = new HIDImports();

then you'll have to add the class HIDImports.cs to your project. I don't have a clue where to get it, but usually www.google.com helps Cheesy
Logged
Pages: [1]
  Print  
 
Jump to:  

Clicky Web Analytics