0

I added to the reference the OpenHardwareMonitorLib.dll

Now I added in my code: using OpenHardwareMonitor.Hardware;.

Then I did in the top form level: Isensor Sensor;

But I can't "new" it i cant create an instance of it and I'm getting null exception on it in the constructor:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ISensor Sensor;

        public Form1()
        {
            InitializeComponent();
            string t = Sensor.Name;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

Cannot create an instance of the abstract class or interface 'OpenHardwareMonitor.Hardware

I tried ot look in the source code in the code.google.com site: http://code.google.com/p/open-hardware-monitor/source/browse/#svn%2Ftags%2F0.3.2%2FWMI

But i dont want to use all this code. I downloaded the program Open Hardware Monitor and except the exe file there is a dll file im using now in my project i was sure i can use the dll to use it easier. All i want for now is to get the temperature of the video card gpu.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
  • 1
    I know this is the obvious, but are you sure your code library contains a definition for that interface? –  Dec 26 '11 at 14:33
  • Shark im not sure. But the Isensor exist. Its not logical to download/copy all the source code from the code.google site, if so why have they put the openhwardwaremonitorlib.dll with the program ? I dont know. – Shakul Shakuli Dec 26 '11 at 14:45
  • In addition to my answer, you might look at http://stackoverflow.com/questions/2843244/how-to-read-gpu-graphic-card-temperature –  Dec 27 '11 at 03:58

1 Answers1

1

First, you can't new interfaces. You can only new concrete classes.

Second, I recommend renaming your variable from Sensor to sensor, or _sensor, or something along these lines. There is a Sensor class. It's best to avoid confusion.

What I did was downloaded the DLL and opened it up in ILSpy. Let's see what classes implement this interface. I urge you to download ILSpy and try this out for yourself.

Here's the resulting ILSpy window. Now in the bottom right I had done an "Analyze" on the interface to see where it is exposed. There don't appear to be any factory methods that return an ISensor.

Back over on the left side, we see that one class implements ISensor: the Sensor class. This class has four constructors. These will come up in intellisense in Visual Studio, or if you navigate to the Sensor class in ILSpy you can see the constructors.

What you'll end up needing to do is sensor = new Sensor(...); As for which constructor you use... that's up to you.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • My goal in answering this question wasn't to give you a fish, but to teach you how to fish yourself. You know the proverb: "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." With ILSpy on your tool belt, you can probably figure out everything you need. –  Dec 27 '11 at 01:54
  • Oh, and open both the DLL and the OpenHardwareMonitor.EXE file. Then if you run an analyze on the interface, you can see where it's used in the executable. I got the executable and DLL from [here](http://openhardwaremonitor.org/downloads/). You can see how its used, thus enabling you to do what they did! –  Dec 27 '11 at 01:59
  • Inuyasha I used the ULSpy and i did analyze on the Sensor class and i see that the Sensor class is coming from the OpenHardwareMonitor.Hardware.Sensor but when i added the OpenHardwareMonitorLib.dll as reference and then did using OpenHardwareMonitor.Hardware; on the top class level i cant see Sensor i mean in the area before the constructor where i declare variables i type: Sensor and i see Isensor but i cant see Sensor. I dont understand the class Sensor should be show up ? Its part of the Hardware no ? – Shakul Shakuli Dec 27 '11 at 07:40
  • I see now that IParameter is containing the class Sensor. So for example if i declared: IParameter sensor; and then in the constructor i need to make sensor = ? I cant create instance ofcourse but if for example i want to get the Sensor.Name ? For example in the constructor: string t = sensor.Sensor.Name; and yet sensor is in green and its all the time null. sensor is never assigned. For the simplest thing lets say i want to get the Sensor.Name – Shakul Shakuli Dec 27 '11 at 08:01
  • Where from is sensor = new Sensor(...); The onyl Sensor class i found is under IParameter interface. sensor i understand its the ISensor sensor; i did. But where is new Sensor ? Cant find it only under IParameter. – Shakul Shakuli Dec 27 '11 at 08:06