标题:请问哪里有使用C#调用dll文件的例子呢?
只看楼主
zenggezhuang
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2011-7-25
结帖率:0
已结贴  问题点数:20 回复次数:1 
请问哪里有使用C#调用dll文件的例子呢?
我现在有一个dll文件,它跟普通的使用方法不一样,需要使用代码导入dll文件,现在不知道怎么写,请大家指教。
2011-07-25 14:04
a6068132
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:47
专家分:131
注 册:2011-5-10
得分:20 
新建-项目-类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ET199ComLib;
using System.Runtime.InteropServices;

namespace ET199Test
{
    public partial class COM
    {
        private static string _p5;
        private static string _p6;
        private static string _success;
        private static string _time;
        private static string _easy;
        private static string _clear;
        public static string P5
        {
            set { _p5 = value; }
            get { return _p5; }
        }
        public static string P6
        {
            set { _p6 = value; }
            get { return _p6; }
        }
        public static string success
        {
            set { _success = value; }
            get { return _success; }
        }
        public static string time
        {
            set { _time = value; }
            get { return _time; }
        }
        public static string easy
        {
            set { _easy = value; }
            get { return _easy; }
        }
        public static string clear
        {
            set { _clear = value; }
            get { return _clear; }
        }
      
        public void Execute(string userPin,string fileDir,string fileID,string cmd,string _p0,string _p1,string _p2,string _p3,string _p4)
        {

            try
            {
                byte[] byteUserpwd = new byte[8];
                byte[] PinUser = System.Text.Encoding.ASCII.GetBytes(userPin);
                for (int m = 0; m < PinUser.Length; m++)
                {
                    byteUserpwd[m] = PinUser[m];
                }
                object objUserPin = byteUserpwd;
                ET199ComLib.ET199 et = new ET199ComLib.ET199();
                int nUserCount = et.Enum();    //枚举设备
                et.Open(0);

                et.ChangeDir(fileDir.Replace("'\'", "'\''\'"));
                byte[] byteinbuffer = new byte[21];
                byteinbuffer[0] = (byte)(Convert.ToInt64(cmd));
                ////////////////////////////////////////////
                Int64 P0 = Convert.ToInt64(_p0);//P0
                int i0 = 4;
                while (P0 / 256 != 0)
                {
                    Int64 a = P0 / 256;
                    byteinbuffer[i0] = (byte)(P0 % 256);
                    P0 = a;
                    i0--;
                    if (i0 == 0)
                        break;
                }
                if (i0 > 0 && P0 / 256 == 0)
                {
                    byteinbuffer[i0] = (byte)(P0 % 256);
                    i0--;
                }

                //////////////////////////////////////
                Int64 P1 = Convert.ToInt64(_p1);//P1
                int i1 = 8;
                while (P1 / 256 != 0)
                {
                    Int64 a = P1 / 256;
                    byteinbuffer[i1] = (byte)(P1 % 256);
                    P1 = a;
                    i1--;
                    if (i1 == 4)
                        break;
                }
                if (i1 > 4 && P1 / 256 == 0)
                {
                    byteinbuffer[i1] = (byte)(P1 % 256);
                    i1--;
                }
                /////////////////////////////////////////
                Int64 P2 = Convert.ToInt64(_p2);//P2
                int i2 = 12;
                while (P2 / 256 != 0)
                {
                    Int64 a = P2 / 256;
                    byteinbuffer[i2] = (byte)(P2 % 256);
                    P2 = a;
                    i2--;
                    if (i2 == 8)
                        break;
                }
                if (i2 > 8 && P2 / 256 == 0)//
                {
                    byteinbuffer[i2] = (byte)(P2 % 256);
                    i2--;
                }
                ///////////////////////////////////////
                Int64 P3 = Convert.ToInt64(_p3);//P3
                int i3 = 16;
                while (P3 / 256 != 0)
                {
                    Int64 a = P3 / 256;
                    byteinbuffer[i3] = (byte)(P3 % 256);
                    P3 = a;
                    i3--;
                    if (i3 == 12)
                        break;
                }
                if (i3 > 12 && P3 / 256 == 0)
                {
                    byteinbuffer[i3] = (byte)(P3 % 256);
                    i3--;
                }
                ///////////////////////////
                Int64 P4 = Convert.ToInt64(_p4);//P4
                int i4 = 20;
                while (P4 / 256 != 0)
                {
                    Int64 a = P4 / 256;
                    byteinbuffer[i4] = (byte)(P4 % 256);
                    P4 = a;
                    i4--;
                    if (i4 == 16)
                        break;
                }
                if (i4 > 16 && P4 / 256 == 0)
                {
                    byteinbuffer[i4] = (byte)(P4 % 256);
                    i4--;
                }
                ////////////////////////////////
                object objinbuffer = byteinbuffer;
                object objoutbuffer = new byte[9];
                int pdwreturned = 9;
                et.VerifyPin(ref objUserPin, ET_PIN_TYPE.ET_USER_PIN);//验证用户
                et.Execute(fileID, ref objinbuffer, 21, ref objoutbuffer, out pdwreturned);
                byte[] byteoutbuffer = (byte[])objoutbuffer;
                int p51 = Convert.ToInt32(byteoutbuffer[1].ToString()) * 16777216;
                int p52 = Convert.ToInt32(byteoutbuffer[2].ToString()) * 65536;
                int p53 = Convert.ToInt32(byteoutbuffer[3].ToString()) * 256;
                int p54 = Convert.ToInt32(byteoutbuffer[4].ToString());
                int p61 = Convert.ToInt32(byteoutbuffer[5].ToString()) * 16777216;
                int p62 = Convert.ToInt32(byteoutbuffer[6].ToString()) * 65536;
                int p63 = Convert.ToInt32(byteoutbuffer[7].ToString()) * 256;
                int p64 = Convert.ToInt32(byteoutbuffer[8].ToString());
                P5 = Convert.ToString(p51 + p52 + p53 + p54);
                P6 = Convert.ToString(p61 + p62 + p63 + p64);
            }
            catch (COMException )
            {
               
            }
           
        }
        public void ExecuteCode(string userPin, string fileDir, string fileID,string _p0)
        {
            try
            {
                byte[] byteUserpwd = new byte[8];
                byte[] PinUser = System.Text.Encoding.ASCII.GetBytes(userPin);
                for (int m = 0; m < PinUser.Length; m++)
                {
                    byteUserpwd[m] = PinUser[m];
                }
                object objUserPin = byteUserpwd;
                ET199ComLib.ET199 et = new ET199ComLib.ET199();
                int nUserCount = et.Enum();    //枚举设备
                et.Open(0);

                et.ChangeDir(fileDir.Replace("'\'", "'\''\'"));
                byte[] byteinbuffer = new byte[5];
                byteinbuffer[0] = (byte)3;
                Int64 P0 = Convert.ToInt64(_p0);//P0
                int i = 4;
                while (P0 / 256 != 0)
                {
                    Int64 a = P0 / 256;
                    byteinbuffer[i] = (byte)(P0 % 256);
                    P0 = a;
                    i--;
                    if (i == 0)
                        break;
                }
                if (i > 0 && P0 / 256 == 0)
                {
                    byteinbuffer[i] = (byte)(P0 % 256);
                    i--;
                }
                object objinbuffer = byteinbuffer;
                object objoutbuffer = new byte[5];
                int pdwreturned = 5;

                et.VerifyPin(ref objUserPin, ET_PIN_TYPE.ET_USER_PIN);//验证用户
                et.Execute(fileID, ref objinbuffer, 5, ref objoutbuffer, out pdwreturned);
                byte[] byteoutbuffer = (byte[])objoutbuffer;
               

                success =byteoutbuffer[1].ToString();
                time= byteoutbuffer[2].ToString();
                easy = byteoutbuffer[3].ToString();
                clear = byteoutbuffer[4].ToString();

            }
            catch (COMException)
            {}
           
        }
        }

   
   
}
点击“生成”-生成“你命名的类库名称”
2011-07-26 18:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-346051-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055924 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved