Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET VS2010
I am experimenting with a few ways of doing things and have seen that if I have routines that are used in multiple projects, then I can create that code in a DLL & call it from those different projects thereby removing the need for repeating code.
 
I've started off by just creating a small program that will tell me what version of Windows I am running, but in this case I don't need to pass anything to the DLL, I just want to call it and read the results ... how do I do this ?!?
 
This is my code and I think it will work, but I have written it to accept data & obviously I don't actually need to supply any parameters, what do I need to do ? Sorry, I am new to C# and am having real trouble getting my head around the terminology, let alone the logic !!!
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MyWindowsVersion
{
    public class WindowsVersion
    {
        public static string add(string myOSname, int myOSver) // Unsure about this line
        {
            String myStr = Environment.OSVersion.Version.ToString();
 
            if (myStr.Contains("1.04"))
                myOSname = "Windows 1.0";
            else if (myStr.Contains("2.11"))
                myOSname = "Windows 2.0";
            else if (myStr.Contains("3"))
                myOSname = "Windows 3.0";
            else if (myStr.Contains("3.10.528"))
                myOSname = "Windows NT 3.1";
            else if (myStr.Contains("3.11 3.11"))
                myOSname = "Windows for Workgroups";
            else if (myStr.Contains("3.5 3.5.807"))
                myOSname = "Windows NT Workstation";
            else if (myStr.Contains("3.51 3.51.1057"))
                myOSname = "Windows NT Workstation";
            else if (myStr.Contains("4.0.950"))
                myOSname = "Windows 95";
            else if (myStr.Contains("4.0.1381"))
                myOSname = "Windows NT Workstation 4.0";
            else if (myStr.Contains("4.1.1998"))
                myOSname = "Windows 98";
            else if (myStr.Contains("4.1.2222"))
                myOSname = "Windows 98 Second Edition";
            else if (myStr.Contains("4.90.3000"))
                myOSname = "Windows Me";
            else if (myStr.Contains("5.0.2195"))
                myOSname = "Windows 2000 Professional";
            else if (myStr.Contains("5.1.2600"))
                myOSname = "Windows XP";
            else if (myStr.Contains("5.2.3790"))
                myOSname = "Windows XP Professional x64 Edition";
            else if (myStr.Contains("6.0.6000"))
                myOSname = "Windows Vista";
            else if (myStr.Contains("6.0.6002"))
                myOSname = "Windows Vista SP2";
            else if (myStr.Contains("6.1.7600"))
                myOSname = "Windows 7";
            else myOSname = "Windows Unknown Version";
 
            myOSver = int.Parse(myStr.Substring(0,1));
 
            return (myOSver + myOSname);
 
        }
    }
}
Posted 4-Apr-12 7:51am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
an alternative way to get name of your os is using WMI.
public static string os_name
        {
            get
            {
                string Query = "Select Name from Win32_OperatingSystem";
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
                string OSVersion = null;
                foreach (ManagementObject Win32 in searcher.Get())
                {
                    OSVersion = Win32["Name"] as string;
 
                }
                return OSVersion;
            }
 
        }
 
Using:
MessageBox.Show(os_name);
 
Now the last thing is to pack it in dll.
http://msdn.microsoft.com/en-us/library/3707x96z%28v=vs.80%29.aspx[^]
 
Please have a look at WMI Tool which provides more methods for many things:
 
http://www.microsoft.com/download/en/details.aspx?id=24045[^]
 
It's a bit confusion for me, why did you overload it with string myOSname, int myOSver ?
I guess your method should simply do receiving Name of OS?
 
Regards
  Permalink  
v4
Comments
Gary Heath at 4-Apr-12 13:30pm
   
Thanks Björn, I will certainly look at WMI, but this really is just an example, it is how to create and access DLL files that I am trying to get my head around ... but can't !!!
 
The code here that you refer to is because I was trying to return 2 variables, but found that I was unable to, so I thought I would just concatenate them & take them apart again back in the calling program, it's nothing important, just learning as I go along ...
Björn Ranft at 4-Apr-12 13:33pm
   
Do you already read MSDN Article which I updated in my solution? If it's still confusing for you, you're welcome, don't hesitate to ask ;)
Gary Heath at 4-Apr-12 14:04pm
   
That is the page I have used to base my code on, but as you can see, it is passing the DLL 2 variables and receiving a result back. I realise that in most cases that is the sort of thing you would want from a DLL, but as part of my learning, I'd like to know how to call a DLL without passing a variable, as in this case, extracting information about the OS.
 
I am just amending the program to make it more compact & remove the unnecessary concatenation, I realise that I try to do too much too quickly sometimes !!!
Gary Heath at 4-Apr-12 14:23pm
   
OK, I have vastly reduced the program as follows ...
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MyWindowsVersion
{
public class WindowsVersion
{
public static string myNull(string myOSver)
{
String myStr = Environment.OSVersion.Version.ToString();
 
int myOSver1 = int.Parse(myStr.Substring(0,1));
int myOSver3 = int.Parse(myStr.Substring(0,3));
 
if (myOSver1 < 5)
myOSver = "Windows version prior to XP";
else if (myOSver3 < 5.1)
myOSver = "Windows version prior to XP";
else if (myOSver3 < 6.0)
myOSver = "Windows XP";
else if (myOSver3 < 6.1)
myOSver = "Windows Vista";
else if (myOSver3 == 6.1)
myOSver = "Windows 7";
else myOSver = "Windows Unknown Version";
 
return (myOSver);
}
}
}
 
As you can see, I still don't know how to write it so it isn't expecting a parameter, so I've assigned the string to be returned in the "Public Static" line, is that the way around this ?
 
However, I am having trouble picking it up, I'm obviously doing something wrong, but I've tried to do the same as the MSDN example but when I code ...
 
string myWinVer = MyWindowsVersion.WindowsVersion("x");
 
... I get an error "'MyWindowsVersion.WindowsVersion' is a 'type', which is not valid in the given context" ... Thanks for your help, BTW!!!
Björn Ranft at 9-Apr-12 21:12pm
   
Agree to WesAday, you think about it too much, just create a construct with zero arguments/parameters ;). Best Regards
Naerling at 14-Jul-12 11:21am
   
My 5. That does simplify things (not as simple as WesAdays answer though).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

"As you can see, I still don't know how to write it so it isn't expecting a parameter, so I've assigned the string to be returned in the "Public Static" line, is that the way around this ? However, I am having trouble picking it up, I'm obviously doing something wrong, but I've tried to do the same as the MSDN example but when I code ... string myWinVer = MyWindowsVersion.WindowsVersion("x"); ... I get an error "'MyWindowsVersion.WindowsVersion' is a 'type', which is not valid in the given context"
 
I think that you are making this waaaaaaaaaaaaay to hard on yourself.
If you do not what to pass any parameters in, then simply don't. Change your function declaration to:
public static string add()
 
Probably the easiest thing for you to do is to have a local string variable for the os name then the rest of your code should be okay as is.
 string myStr = Environment.OSVersion.Version.ToString();
 string myOSname;
 int myOSver = int.Parse(myStr.Substring(0,1));
 
The other part of your problem is the calling convention. In your example, "WindowsVersion" is the name of the class. You got that error because you did not tell it to call a function. From your calling function you would:
 
string version = MyWindowsVersion.WindowsVersion.add();
 
Of course changing the function name to something more appropriate, like GetOsName or similar, couldn't hurt.
  Permalink  
v2
Comments
Naerling at 14-Jul-12 11:21am
   
My 5. Much much easier :)
Wes Aday at 14-Jul-12 11:28am
   
Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Maybe you try this example, doing following steps.
 
1. Create a new Class Library Project with a name like Calculator.
Copy&Paste this code into this class.
 
public class Calculator
{
 

        public double Add(double val1, double val2)
        {
            return val1 + val2;
        }
 
        public double Sub(double val1, double val2)
        {
            return val1 - val2;
        }
 
        public double Multiply(double val1, double val2)
        {
            return val1 * val2;
        }
 
        public double Divide(double val1, double val2)
        {
            return val1 / val2;
        }
 
        public double square_root(double val1)
        {
            return Math.Sqrt(val1);
        }
 
        public long power(double val, int pow_val)
        {
            long result = 0;
            for (double pow = 0; pow <= pow_val; pow++)
            result = (long)Math.Pow(val, pow);
            return result;
        }
 
        public double manhatten_metrik(double[] punktA, double[] punktB)
        {
            double result = 0.0;
            int length = Math.Max(punktA.Length, punktB.Length);
            for (int i = 0; i < length; i++)
            {
                result += Math.Abs(punktA[i] - punktB[i]);
            }
            return result;
        }
}
 

Now you can build your solution and your Calculator.dll will be copied in /Release Folder. To work with this DLL, create a second WinForms-Project and add a reference to our Calculator.dll.
 
Then use it like this:
 
Calculator.Calculator calc = new Calculator.Calculator();
MessageBox.Show(calc.Add(9, 6).ToString()); //15
MessageBox.Show(calc.Sub(9, 6).ToString()); //3
MessageBox.Show(calc.Multiply(3, 2).ToString()); //6
MessageBox.Show(calc.Divide(6, 2).ToString()); //3
MessageBox.Show(calc.square_root(25).ToString()); //5
long result = calc.power(2, 10); //2^10 =1024
MessageBox.Show(result.ToString()); 
 
Regards
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I have given up on C#, it is way too complicated for me to learn at my age & am re-starting my project using VB.net, which I at least understand !!! I guess that the DLL process is similar, so I will revisit this when I get to a similar point of needing to call DLLs ... Thanks for your help ...
  Permalink  
Comments
Björn Ranft at 14-Jul-12 10:33am
   
Oh, that's sad because I'm sure if you get it done you will say "oh, it works, and it was quite simple". Where do you stuck? If you still have problems with it, download an example and see what's happening. Regards
Gary Heath at 14-Jul-12 12:37pm
   
Thanks Björn, but it's actually C# I've given up on, I'm 53 and am a COBOL programmer, so the new techniques are difficult for me to grasp ... I can just about get my head around VB.net, but C# was killing me !!! I will revisit this, I am sure, when I get to some more complicated coding, but for now I am just rewriting the skeleton of my project in VB & taking it from there ... thanks again for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 14 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100