调用基本的C#DLL [英] Calling a basic C# DLL

查看:116
本文介绍了调用基本的C#DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试几种处理方式,并且看到如果我有在多个项目中使用的例程,那么可以在DLL&中创建该代码.从那些不同的项目中调用它,从而消除了重复代码的需要.

我首先创建了一个小程序,该程序将告诉我我正在运行的Windows版本,但是在这种情况下,我不需要将任何内容传递给DLL,我只想调用它并阅读其中的内容即可.结果...我该怎么办?!?

这是我的代码,并且我认为它可以工作,但是我已经编写了它以接受数据&;显然,我实际上不需要提供任何参数,我该怎么办?抱歉,我是C#的新手,我实在难以理解术语,更不用说逻辑了!!!

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);

        }
    }
}

推荐答案



另一种获取操作系统名称的方法是使用WMI.
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);



现在,最后一件事是将其打包到dll中.
http://msdn.microsoft.com/en-us/library/3707x96z%28v = vs.80%29.aspx [ ^ ]

请看一下WMI工具,它为许多事情提供了更多的方法:

http://www.microsoft.com/download/en/details.aspx?id=24045 [ ^ ]

对我来说有点困惑,为什么要用string myOSname, int myOSver重载它?
我想您的方法应该只是接收OS的名称?

问候



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


如您所见,我仍然不知道如何编写它,因此它不需要参数,因此我已分配了要返回的字符串在公共静态"行中,是这样吗?但是,我在拾起它时遇到了麻烦,我显然做错了什么,但是我尝试做与MSDN示例相同的操作,但是当我编写代码...字符串myWinVer = MyWindowsVersion.WindowsVersion("x"); ...我收到错误消息''MyWindowsVersion.WindowsVersion"是类型",在给定的上下文中无效"

我认为您正在努力提高自己的能力.
如果您不传入任何参数,那就不要.将函数声明更改为:
"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()



对于您来说,最简单的操作可能是为os名称设置一个本地字符串变量,然后其余代码就可以了.



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));



问题的另一部分是调用约定.在您的示例中,"WindowsVersion"是类的名称.之所以收到该错误,是因为您没有告诉它调用函数.通过调用函数,您将:



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();



当然,将函数名称更改为更合适的名称(如GetOsName或类似名称)不会有什么坏处.



Of course changing the function name to something more appropriate, like GetOsName or similar, couldn''t hurt.


也许您可以尝试以下示例,并按以下步骤操作.

1.创建一个新的类库项目,其名称类似于Calculator.
复制此代码并将其粘贴到此类中.

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;
        }
}




现在,您可以构建您的解决方案,并且您的Calculator.dll将复制到/Release文件夹中.要使用此DLL,请创建第二个WinForms-Project并添加对我们Calculator.dll的引用.

然后像这样使用它:




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


这篇关于调用基本的C#DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆