dll import

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcw
    New Member
    • Nov 2008
    • 82

    #1

    dll import

    Hi

    I am trying to import a dll. So far I have got

    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.Runtime.InteropServices;
    //using system.runtime.InteropServices.DllImportAttribute;
    
    using Phidgets;
    using Phidgets.Events;
    
    namespace Robot
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
            }
            
            private InterfaceKit Ifkit;
            private Stepper stepper;
            private Servo servo;
            [DllImport("C:/Users/M/Desktop/phidgets21.net.dll")]
    But I get Error Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations.

    I have tried putting the dllimport statement in the form1_load, but I get lots more errors.

    What am I doing wrong?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Since you did not post entire code, i should make many guesses to think what might go wrong.

    You can refer this for more information

    Regards
    Dheeraj Joshi

    Comment

    • mrcw
      New Member
      • Nov 2008
      • 82

      #3
      OK here is the complete code (all 400 lines)

      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.Runtime.InteropServices;
      //using system.runtime.InteropServices.DllImportAttribute;
      
      using Phidgets;
      using Phidgets.Events;
      
      namespace Robot
      {
          public partial class Form1 : Form
          {
              
              public Form1()
              {
                  InitializeComponent();
              }
              
              private InterfaceKit Ifkit;
              private Stepper stepper;
              private Servo servo;
              //[DllImport("C:/Users/Mark/Desktop/phidgets21.net.dll")]
              
            
              int LeftVelocity;
              long LeftDirection; 
              int RightVelocity;
              long RightDirection;
              
              
      
              //Initialize the device
              private void Form1_Load(object sender, EventArgs e)
              {
                 // Phidget.enableLogging(Phidget.LogLevel.PHIDGET_LOG_VERBOSE, null);
                  Ifkit = new InterfaceKit();
                  stepper = new Stepper();
                  servo = new Servo();
      
                  this.WindowState = FormWindowState.Maximized;
      
                  Ifkit_OutputChange += new OutputChangeEventHandler(ifkit_OutputChange);
                  Ifkit.InputChange += new InputChangeEventHandler(ifKit_InputChange);
                  stepper.CurrentChange += new CurrentChangeEventHandler(stepper_CurrentChange);
                  stepper.PositionChange += new StepperPositionChangeEventHandler(stepper_PositionChange);
                  stepper.VelocityChange += new VelocityChangeEventHandler(stepper_VelocityChange);
                  stepper.InputChange += new InputChangeEventHandler(stepper_InputChange);
                  
                  ////Open the Phidget using the command line arguments
                  openCmdLine1(Ifkit);
                  openCmdLine2(stepper);
                  openCmdLine3(servo);
      
                 
              }
            
          
      
              //Digital input change event handler
              //Here we check or uncheck the corresponding input checkbox based 
              //on the index of the digital input that generated the event
              void ifKit_InputChange(object sender, InputChangeEventArgs e)
              {
                  try
                  {
                      if (e.Index == 0) 
                      { 
                          textBox1.Text = "0";
                          stop(); 
                      }
                      else if (e.Index == 1) 
                      { 
                          textBox1.Text = "1";
                          stop(); 
                      }
                      else if (e.Index == 2)
                      { 
                          textBox1.Text = "2";
                          stop(); 
                      }
                      else if (e.Index == 3)
                      { 
                          textBox1.Text = "3";
                          stop(); 
                      }
                      else if (e.Index == 4)
                      { 
                          textBox1.Text = "";
                          stop(); 
                      }
      
                      if (e.Value == false) 
                      {
                          textBox1.Text = ""; 
                      };
                  }
                  catch { }
              }
      
              private void ifkit_OutputChange(object sender, OutputChangeEventArgs e)
              {
      
              }
      
              //Stepper Current change event handler...Display the current data from the current change event
              void stepper_CurrentChange(object sender, CurrentChangeEventArgs e)
              {
                  tbLvelocityTxt.Text = e.Current.ToString(); 
              }
              //Stepper Input Change event handler...Display the data from the Input change event
              void stepper_InputChange(object sender, InputChangeEventArgs e)
              {
                  
              }
              //Stepper position change event handler...Display the position data from the position change event
              void stepper_PositionChange(object sender, StepperPositionChangeEventArgs e)
              {
                  tbLpositionTxt.Text = e.Position.ToString();
              }
      
              //Stepper Velocity Change event handler...Display the data from the velocity change event
              void stepper_VelocityChange(object sender, VelocityChangeEventArgs e)
              {
                  tbLvelocityTxt.Text = e.Velocity.ToString();             
              }
      
              private void stop()
              {
                  for (int i = 0; i < 4; i++)
      			{
      			 stepper.steppers[i].Engaged = false;
      			}
                 
              }
      
              //get distance reading from ultrasonic sensor 0
              private void sensor0()
              {
                  double distance;
                  Ifkit.outputs[0] = true;
                  distance = (Ifkit.sensors[0].Value) * 1.296;
                  Ifkit.outputs[0] = false;
                  textBox2.Text = distance.ToString() + "cm";
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  sensor0();
              }
      
              /*You will need a speed of (2x200)/120 steps /sec = 3,33
      Then, 3/4 rev is 150 steps, so you just have to increment the TargetPosition by 150 before engaging the stepper.*/
      
              private void BothOneRev()
              {
                  stepper.steppers[0].Engaged = false;
                  stepper.steppers[0].Acceleration = stepper.steppers[0].AccelerationMin +100;
                  stepper.steppers[0].VelocityLimit = LeftVelocity;
                  stepper.steppers[0].TargetPosition = stepper.steppers[0].CurrentPosition + LeftDirection;
                  stepper.steppers[0].Engaged = true;
                  stepper.steppers[1].Engaged = false;
                  stepper.steppers[1].Acceleration = stepper.steppers[1].AccelerationMin;
                  stepper.steppers[1].VelocityLimit = RightVelocity;
                  stepper.steppers[1].TargetPosition = stepper.steppers[1].CurrentPosition + RightDirection;
                  stepper.steppers[1].Engaged = true;
              }
      
             
             
              private void fore()
              {
                  LeftVelocity = 300;
                  RightVelocity = 300;
      
                  LeftDirection = +400;
                  RightDirection = +400;
      
                  BothOneRev();
              }
              
              private void back()
              {
                  LeftVelocity = 300;
                  RightVelocity = 300;
      
                  LeftDirection = -400;
                  RightDirection = -400;
      
                  BothOneRev();
              }
      
              private void SpinClockwise()
              {
                  LeftVelocity = 300;
                  RightVelocity = 300;
      
                  LeftDirection = +400;
                  RightDirection = -400;
      
                  BothOneRev();
              }
             
              private void SpinAntiClockwise()
              {
                  LeftVelocity = 300;
                  RightVelocity = 300;
      
                  LeftDirection = -400;
                  RightDirection = +400;
      
                  BothOneRev();
              }
      
              private void btnfore_Click(object sender, EventArgs e)
              {
                  fore();   
              }
              private void btnBack_Click(object sender, EventArgs e)
              {
                  back();
              }
              private void btnClock_Click(object sender, EventArgs e)
              {
                  SpinClockwise();
              }
              private void btnAnti_Click(object sender, EventArgs e)
              {
                  SpinAntiClockwise();
              }
              private void btnStop_Click(object sender, EventArgs e)
              {
                  stop();
                  
              }
            
      
      
              //Parses command line arguments and calls the appropriate open
              #region(openCmdLine)
      
              private void openCmdLine1(Phidget p)
              {
                  openCmdLine1(p, null);
              }
              private void openCmdLine1(Phidget p, String pass)
              {
                  int serial = -1;
                  int port = 5001;
                  String host = null;
                  bool remote = false, remoteIP = false;
                  string[] args = Environment.GetCommandLineArgs();
                  String appName = args[0];
      
                  try
                  { //Parse the flags
                      for (int i = 1; i < args.Length; i++)
                      {
                          if (args[i].StartsWith("-"))
                              switch (args[i].Remove(0, 1).ToLower())
                              {
                                  case "n":
                                      serial = int.Parse(args[++i]);
                                      break;
                                  case "r":
                                      remote = true;
                                      break;
                                  case "s":
                                      remote = true;
                                      host = args[++i];
                                      break;
                                  case "p":
                                      pass = args[++i];
                                      break;
                                  case "i":
                                      remoteIP = true;
                                      host = args[++i];
                                      if (host.Contains(":"))
                                      {
                                          port = int.Parse(host.Split(':')[1]);
                                          host = host.Split(':')[0];
                                      }
                                      break;
                                  default:
                                      goto usage;
                              }
                          else
                              goto usage;
                      }
      
                      if (remoteIP)
                          p.open(serial, host, port, pass);
                      else if (remote)
                          p.open(serial, host, pass);
                      else
                          p.open(serial);
                      return; //success
                  }
                  catch { }
              usage:
                  StringBuilder sb = new StringBuilder();
                  sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
                  sb.AppendLine("Usage: " + appName + " [Flags...]");
                  sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
                  sb.AppendLine("\t-r\t\tOpen remotely");
                  sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
                  sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
                  sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
                  sb.AppendLine("Examples: ");
                  sb.AppendLine(appName + " -n 50098");
                  sb.AppendLine(appName + " -r");
                  sb.AppendLine(appName + " -s myphidgetserver");
                  sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
                  MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      
                  Application.Exit();
              }
      
      
              private void openCmdLine2(Phidget p)
              {
                  openCmdLine(p, null);
              }
              private void openCmdLine(Phidget p, String pass)
              {
                  int serial = -1;
                  int port = 5001;
                  String host = null;
                  bool remote = false, remoteIP = false;
                  string[] args = Environment.GetCommandLineArgs();
                  String appName = args[0];
      
                  try
                  { //Parse the flags
                      for (int i = 1; i < args.Length; i++)
                      {
                          if (args[i].StartsWith("-"))
                              switch (args[i].Remove(0, 1).ToLower())
                              {
                                  case "n":
                                      serial = int.Parse(args[++i]);
                                      break;
                                  case "r":
                                      remote = true;
                                      break;
                                  case "s":
                                      remote = true;
                                      host = args[++i];
                                      break;
                                  case "p":
                                      pass = args[++i];
                                      break;
                                  case "i":
                                      remoteIP = true;
                                      host = args[++i];
                                      if (host.Contains(":"))
                                      {
                                          port = int.Parse(host.Split(':')[1]);
                                          host = host.Split(':')[0];
                                      }
                                      break;
                                  default:
                                      goto usage;
                              }
                          else
                              goto usage;
                      }
      
                      if (remoteIP)
                          p.open(serial, host, port, pass);
                      else if (remote)
                          p.open(serial, host, pass);
                      else
                          p.open(serial);
                      return; //success
                  }
                  catch { }
              usage:
                  StringBuilder sb = new StringBuilder();
                  sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
                  sb.AppendLine("Usage: " + appName + " [Flags...]");
                  sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
                  sb.AppendLine("\t-r\t\tOpen remotely");
                  sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
                  sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
                  sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
                  sb.AppendLine("Examples: ");
                  sb.AppendLine(appName + " -n 50098");
                  sb.AppendLine(appName + " -r");
                  sb.AppendLine(appName + " -s myphidgetserver");
                  sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
                  MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      
                  Application.Exit();
              }
      
              private void openCmdLine3(Phidget p)
              {
                  openCmdLine3(p, null);
              }
              private void openCmdLine3(Phidget p, String pass)
              {
                  int serial = -1;
                  int port = 5001;
                  String host = null;
                  bool remote = false, remoteIP = false;
                  string[] args = Environment.GetCommandLineArgs();
                  String appName = args[0];
      
                  try
                  { //Parse the flags
                      for (int i = 1; i < args.Length; i++)
                      {
                          if (args[i].StartsWith("-"))
                              switch (args[i].Remove(0, 1).ToLower())
                              {
                                  case "n":
                                      serial = int.Parse(args[++i]);
                                      break;
                                  case "r":
                                      remote = true;
                                      break;
                                  case "s":
                                      remote = true;
                                      host = args[++i];
                                      break;
                                  case "p":
                                      pass = args[++i];
                                      break;
                                  case "i":
                                      remoteIP = true;
                                      host = args[++i];
                                      if (host.Contains(":"))
                                      {
                                          port = int.Parse(host.Split(':')[1]);
                                          host = host.Split(':')[0];
                                      }
                                      break;
                                  default:
                                      goto usage;
                              }
                          else
                              goto usage;
                      }
      
                      if (remoteIP)
                          p.open(serial, host, port, pass);
                      else if (remote)
                          p.open(serial, host, pass);
                      else
                          p.open(serial);
                      return; //success
                  }
                  catch { }
              usage:
                  StringBuilder sb = new StringBuilder();
                  sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
                  sb.AppendLine("Usage: " + appName + " [Flags...]");
                  sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
                  sb.AppendLine("\t-r\t\tOpen remotely");
                  sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
                  sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
                  sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
                  sb.AppendLine("Examples: ");
                  sb.AppendLine(appName + " -n 50098");
                  sb.AppendLine(appName + " -r");
                  sb.AppendLine(appName + " -s myphidgetserver");
                  sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
                  MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      
                  Application.Exit();
              }
              #endregion
      
      
      
      
      
      
              public OutputChangeEventHandler Ifkit_OutputChange { get; set; }
      
             
          }
      }
      why do you need the complete code?

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        DLLImport is an attribute. It doesn't stand alone, it decorates a method signature.

        For example:
        Code:
        [DllImport("user32.dll")]
        static extern short GetKeyState(int keyCode);
        That tells the compiler that this method is defined externally from the code you're writing, and it tells it where to find the method at.

        Here's a link discussing how to use DLLImport:
        http://msdn.microsoft.com/en-us/libr...68(VS.71).aspx

        Good luck.

        Comment

        • ThatThatGuy
          Recognized Expert Contributor
          • Jul 2009
          • 453

          #5
          Originally posted by mrcw
          Hi

          I am trying to import a dll. So far I have got

          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.Runtime.InteropServices;
          //using system.runtime.InteropServices.DllImportAttribute;
          
          using Phidgets;
          using Phidgets.Events;
          
          namespace Robot
          {
              public partial class Form1 : Form
              {
                  
                  public Form1()
                  {
                      InitializeComponent();
                  }
                  
                  private InterfaceKit Ifkit;
                  private Stepper stepper;
                  private Servo servo;
                  [DllImport("C:/Users/M/Desktop/phidgets21.net.dll")]
          But I get Error Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations.

          I have tried putting the dllimport statement in the form1_load, but I get lots more errors.

          What am I doing wrong?
          if that's a .net DLL then you can add a reference to it...

          You can use reflection to load an external DLL which is not registered in the system...

          Comment

          • mrcw
            New Member
            • Nov 2008
            • 82

            #6
            I've already added a reference to it.

            I've looked at the site and I'm still lost. The code changes with each import. Any chance of an example based on the dll I'm using?

            Comment

            • ThatThatGuy
              Recognized Expert Contributor
              • Jul 2009
              • 453

              #7
              If you've already added a reference then why are you using DLL import

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Ok, if it is a .NET DLL, then you do not need to use DLLImport. DLLImport is for what is called "platform invoke" and is used to invoke DLLs that were written in some other language than .NET.

                To use the code in your .NET DLL, you first add the reference, then either add a using statement with the namespace used in the DLL, or fully qualify all your references.

                Comment

                • mrcw
                  New Member
                  • Nov 2008
                  • 82

                  #9
                  I am intending to use the program I have written on a very small laptop. If I copy the program onto the small laptop it doesn't work until the dll is copied over as well. I am just trying to only copy the one file to the new laptop. In effect the DLL is incorporated into my program.

                  Comment

                  Working...