473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

dll import

82
Hi

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. //using system.runtime.InteropServices.DllImportAttribute;
  11.  
  12. using Phidgets;
  13. using Phidgets.Events;
  14.  
  15. namespace Robot
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private InterfaceKit Ifkit;
  26.         private Stepper stepper;
  27.         private Servo servo;
  28.         [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?
Jun 14 '10 #1
8 1576
Dheeraj Joshi
1,123 Expert 1GB
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
Jun 14 '10 #2
mrcw
82
OK here is the complete code (all 400 lines)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. //using system.runtime.InteropServices.DllImportAttribute;
  11.  
  12. using Phidgets;
  13. using Phidgets.Events;
  14.  
  15. namespace Robot
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private InterfaceKit Ifkit;
  26.         private Stepper stepper;
  27.         private Servo servo;
  28.         //[DllImport("C:/Users/Mark/Desktop/phidgets21.net.dll")]
  29.  
  30.  
  31.         int LeftVelocity;
  32.         long LeftDirection; 
  33.         int RightVelocity;
  34.         long RightDirection;
  35.  
  36.  
  37.  
  38.         //Initialize the device
  39.         private void Form1_Load(object sender, EventArgs e)
  40.         {
  41.            // Phidget.enableLogging(Phidget.LogLevel.PHIDGET_LOG_VERBOSE, null);
  42.             Ifkit = new InterfaceKit();
  43.             stepper = new Stepper();
  44.             servo = new Servo();
  45.  
  46.             this.WindowState = FormWindowState.Maximized;
  47.  
  48.             Ifkit_OutputChange += new OutputChangeEventHandler(ifkit_OutputChange);
  49.             Ifkit.InputChange += new InputChangeEventHandler(ifKit_InputChange);
  50.             stepper.CurrentChange += new CurrentChangeEventHandler(stepper_CurrentChange);
  51.             stepper.PositionChange += new StepperPositionChangeEventHandler(stepper_PositionChange);
  52.             stepper.VelocityChange += new VelocityChangeEventHandler(stepper_VelocityChange);
  53.             stepper.InputChange += new InputChangeEventHandler(stepper_InputChange);
  54.  
  55.             ////Open the Phidget using the command line arguments
  56.             openCmdLine1(Ifkit);
  57.             openCmdLine2(stepper);
  58.             openCmdLine3(servo);
  59.  
  60.  
  61.         }
  62.  
  63.  
  64.  
  65.         //Digital input change event handler
  66.         //Here we check or uncheck the corresponding input checkbox based 
  67.         //on the index of the digital input that generated the event
  68.         void ifKit_InputChange(object sender, InputChangeEventArgs e)
  69.         {
  70.             try
  71.             {
  72.                 if (e.Index == 0) 
  73.                 { 
  74.                     textBox1.Text = "0";
  75.                     stop(); 
  76.                 }
  77.                 else if (e.Index == 1) 
  78.                 { 
  79.                     textBox1.Text = "1";
  80.                     stop(); 
  81.                 }
  82.                 else if (e.Index == 2)
  83.                 { 
  84.                     textBox1.Text = "2";
  85.                     stop(); 
  86.                 }
  87.                 else if (e.Index == 3)
  88.                 { 
  89.                     textBox1.Text = "3";
  90.                     stop(); 
  91.                 }
  92.                 else if (e.Index == 4)
  93.                 { 
  94.                     textBox1.Text = "";
  95.                     stop(); 
  96.                 }
  97.  
  98.                 if (e.Value == false) 
  99.                 {
  100.                     textBox1.Text = ""; 
  101.                 };
  102.             }
  103.             catch { }
  104.         }
  105.  
  106.         private void ifkit_OutputChange(object sender, OutputChangeEventArgs e)
  107.         {
  108.  
  109.         }
  110.  
  111.         //Stepper Current change event handler...Display the current data from the current change event
  112.         void stepper_CurrentChange(object sender, CurrentChangeEventArgs e)
  113.         {
  114.             tbLvelocityTxt.Text = e.Current.ToString(); 
  115.         }
  116.         //Stepper Input Change event handler...Display the data from the Input change event
  117.         void stepper_InputChange(object sender, InputChangeEventArgs e)
  118.         {
  119.  
  120.         }
  121.         //Stepper position change event handler...Display the position data from the position change event
  122.         void stepper_PositionChange(object sender, StepperPositionChangeEventArgs e)
  123.         {
  124.             tbLpositionTxt.Text = e.Position.ToString();
  125.         }
  126.  
  127.         //Stepper Velocity Change event handler...Display the data from the velocity change event
  128.         void stepper_VelocityChange(object sender, VelocityChangeEventArgs e)
  129.         {
  130.             tbLvelocityTxt.Text = e.Velocity.ToString();             
  131.         }
  132.  
  133.         private void stop()
  134.         {
  135.             for (int i = 0; i < 4; i++)
  136.             {
  137.              stepper.steppers[i].Engaged = false;
  138.             }
  139.  
  140.         }
  141.  
  142.         //get distance reading from ultrasonic sensor 0
  143.         private void sensor0()
  144.         {
  145.             double distance;
  146.             Ifkit.outputs[0] = true;
  147.             distance = (Ifkit.sensors[0].Value) * 1.296;
  148.             Ifkit.outputs[0] = false;
  149.             textBox2.Text = distance.ToString() + "cm";
  150.         }
  151.  
  152.         private void button1_Click(object sender, EventArgs e)
  153.         {
  154.             sensor0();
  155.         }
  156.  
  157.         /*You will need a speed of (2x200)/120 steps /sec = 3,33
  158. Then, 3/4 rev is 150 steps, so you just have to increment the TargetPosition by 150 before engaging the stepper.*/
  159.  
  160.         private void BothOneRev()
  161.         {
  162.             stepper.steppers[0].Engaged = false;
  163.             stepper.steppers[0].Acceleration = stepper.steppers[0].AccelerationMin +100;
  164.             stepper.steppers[0].VelocityLimit = LeftVelocity;
  165.             stepper.steppers[0].TargetPosition = stepper.steppers[0].CurrentPosition + LeftDirection;
  166.             stepper.steppers[0].Engaged = true;
  167.             stepper.steppers[1].Engaged = false;
  168.             stepper.steppers[1].Acceleration = stepper.steppers[1].AccelerationMin;
  169.             stepper.steppers[1].VelocityLimit = RightVelocity;
  170.             stepper.steppers[1].TargetPosition = stepper.steppers[1].CurrentPosition + RightDirection;
  171.             stepper.steppers[1].Engaged = true;
  172.         }
  173.  
  174.  
  175.  
  176.         private void fore()
  177.         {
  178.             LeftVelocity = 300;
  179.             RightVelocity = 300;
  180.  
  181.             LeftDirection = +400;
  182.             RightDirection = +400;
  183.  
  184.             BothOneRev();
  185.         }
  186.  
  187.         private void back()
  188.         {
  189.             LeftVelocity = 300;
  190.             RightVelocity = 300;
  191.  
  192.             LeftDirection = -400;
  193.             RightDirection = -400;
  194.  
  195.             BothOneRev();
  196.         }
  197.  
  198.         private void SpinClockwise()
  199.         {
  200.             LeftVelocity = 300;
  201.             RightVelocity = 300;
  202.  
  203.             LeftDirection = +400;
  204.             RightDirection = -400;
  205.  
  206.             BothOneRev();
  207.         }
  208.  
  209.         private void SpinAntiClockwise()
  210.         {
  211.             LeftVelocity = 300;
  212.             RightVelocity = 300;
  213.  
  214.             LeftDirection = -400;
  215.             RightDirection = +400;
  216.  
  217.             BothOneRev();
  218.         }
  219.  
  220.         private void btnfore_Click(object sender, EventArgs e)
  221.         {
  222.             fore();   
  223.         }
  224.         private void btnBack_Click(object sender, EventArgs e)
  225.         {
  226.             back();
  227.         }
  228.         private void btnClock_Click(object sender, EventArgs e)
  229.         {
  230.             SpinClockwise();
  231.         }
  232.         private void btnAnti_Click(object sender, EventArgs e)
  233.         {
  234.             SpinAntiClockwise();
  235.         }
  236.         private void btnStop_Click(object sender, EventArgs e)
  237.         {
  238.             stop();
  239.  
  240.         }
  241.  
  242.  
  243.  
  244.         //Parses command line arguments and calls the appropriate open
  245.         #region(openCmdLine)
  246.  
  247.         private void openCmdLine1(Phidget p)
  248.         {
  249.             openCmdLine1(p, null);
  250.         }
  251.         private void openCmdLine1(Phidget p, String pass)
  252.         {
  253.             int serial = -1;
  254.             int port = 5001;
  255.             String host = null;
  256.             bool remote = false, remoteIP = false;
  257.             string[] args = Environment.GetCommandLineArgs();
  258.             String appName = args[0];
  259.  
  260.             try
  261.             { //Parse the flags
  262.                 for (int i = 1; i < args.Length; i++)
  263.                 {
  264.                     if (args[i].StartsWith("-"))
  265.                         switch (args[i].Remove(0, 1).ToLower())
  266.                         {
  267.                             case "n":
  268.                                 serial = int.Parse(args[++i]);
  269.                                 break;
  270.                             case "r":
  271.                                 remote = true;
  272.                                 break;
  273.                             case "s":
  274.                                 remote = true;
  275.                                 host = args[++i];
  276.                                 break;
  277.                             case "p":
  278.                                 pass = args[++i];
  279.                                 break;
  280.                             case "i":
  281.                                 remoteIP = true;
  282.                                 host = args[++i];
  283.                                 if (host.Contains(":"))
  284.                                 {
  285.                                     port = int.Parse(host.Split(':')[1]);
  286.                                     host = host.Split(':')[0];
  287.                                 }
  288.                                 break;
  289.                             default:
  290.                                 goto usage;
  291.                         }
  292.                     else
  293.                         goto usage;
  294.                 }
  295.  
  296.                 if (remoteIP)
  297.                     p.open(serial, host, port, pass);
  298.                 else if (remote)
  299.                     p.open(serial, host, pass);
  300.                 else
  301.                     p.open(serial);
  302.                 return; //success
  303.             }
  304.             catch { }
  305.         usage:
  306.             StringBuilder sb = new StringBuilder();
  307.             sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
  308.             sb.AppendLine("Usage: " + appName + " [Flags...]");
  309.             sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
  310.             sb.AppendLine("\t-r\t\tOpen remotely");
  311.             sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
  312.             sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
  313.             sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
  314.             sb.AppendLine("Examples: ");
  315.             sb.AppendLine(appName + " -n 50098");
  316.             sb.AppendLine(appName + " -r");
  317.             sb.AppendLine(appName + " -s myphidgetserver");
  318.             sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
  319.             MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  320.  
  321.             Application.Exit();
  322.         }
  323.  
  324.  
  325.         private void openCmdLine2(Phidget p)
  326.         {
  327.             openCmdLine(p, null);
  328.         }
  329.         private void openCmdLine(Phidget p, String pass)
  330.         {
  331.             int serial = -1;
  332.             int port = 5001;
  333.             String host = null;
  334.             bool remote = false, remoteIP = false;
  335.             string[] args = Environment.GetCommandLineArgs();
  336.             String appName = args[0];
  337.  
  338.             try
  339.             { //Parse the flags
  340.                 for (int i = 1; i < args.Length; i++)
  341.                 {
  342.                     if (args[i].StartsWith("-"))
  343.                         switch (args[i].Remove(0, 1).ToLower())
  344.                         {
  345.                             case "n":
  346.                                 serial = int.Parse(args[++i]);
  347.                                 break;
  348.                             case "r":
  349.                                 remote = true;
  350.                                 break;
  351.                             case "s":
  352.                                 remote = true;
  353.                                 host = args[++i];
  354.                                 break;
  355.                             case "p":
  356.                                 pass = args[++i];
  357.                                 break;
  358.                             case "i":
  359.                                 remoteIP = true;
  360.                                 host = args[++i];
  361.                                 if (host.Contains(":"))
  362.                                 {
  363.                                     port = int.Parse(host.Split(':')[1]);
  364.                                     host = host.Split(':')[0];
  365.                                 }
  366.                                 break;
  367.                             default:
  368.                                 goto usage;
  369.                         }
  370.                     else
  371.                         goto usage;
  372.                 }
  373.  
  374.                 if (remoteIP)
  375.                     p.open(serial, host, port, pass);
  376.                 else if (remote)
  377.                     p.open(serial, host, pass);
  378.                 else
  379.                     p.open(serial);
  380.                 return; //success
  381.             }
  382.             catch { }
  383.         usage:
  384.             StringBuilder sb = new StringBuilder();
  385.             sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
  386.             sb.AppendLine("Usage: " + appName + " [Flags...]");
  387.             sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
  388.             sb.AppendLine("\t-r\t\tOpen remotely");
  389.             sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
  390.             sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
  391.             sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
  392.             sb.AppendLine("Examples: ");
  393.             sb.AppendLine(appName + " -n 50098");
  394.             sb.AppendLine(appName + " -r");
  395.             sb.AppendLine(appName + " -s myphidgetserver");
  396.             sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
  397.             MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  398.  
  399.             Application.Exit();
  400.         }
  401.  
  402.         private void openCmdLine3(Phidget p)
  403.         {
  404.             openCmdLine3(p, null);
  405.         }
  406.         private void openCmdLine3(Phidget p, String pass)
  407.         {
  408.             int serial = -1;
  409.             int port = 5001;
  410.             String host = null;
  411.             bool remote = false, remoteIP = false;
  412.             string[] args = Environment.GetCommandLineArgs();
  413.             String appName = args[0];
  414.  
  415.             try
  416.             { //Parse the flags
  417.                 for (int i = 1; i < args.Length; i++)
  418.                 {
  419.                     if (args[i].StartsWith("-"))
  420.                         switch (args[i].Remove(0, 1).ToLower())
  421.                         {
  422.                             case "n":
  423.                                 serial = int.Parse(args[++i]);
  424.                                 break;
  425.                             case "r":
  426.                                 remote = true;
  427.                                 break;
  428.                             case "s":
  429.                                 remote = true;
  430.                                 host = args[++i];
  431.                                 break;
  432.                             case "p":
  433.                                 pass = args[++i];
  434.                                 break;
  435.                             case "i":
  436.                                 remoteIP = true;
  437.                                 host = args[++i];
  438.                                 if (host.Contains(":"))
  439.                                 {
  440.                                     port = int.Parse(host.Split(':')[1]);
  441.                                     host = host.Split(':')[0];
  442.                                 }
  443.                                 break;
  444.                             default:
  445.                                 goto usage;
  446.                         }
  447.                     else
  448.                         goto usage;
  449.                 }
  450.  
  451.                 if (remoteIP)
  452.                     p.open(serial, host, port, pass);
  453.                 else if (remote)
  454.                     p.open(serial, host, pass);
  455.                 else
  456.                     p.open(serial);
  457.                 return; //success
  458.             }
  459.             catch { }
  460.         usage:
  461.             StringBuilder sb = new StringBuilder();
  462.             sb.AppendLine("Invalid Command line arguments." + Environment.NewLine);
  463.             sb.AppendLine("Usage: " + appName + " [Flags...]");
  464.             sb.AppendLine("Flags:\t-n   serialNumber\tSerial Number, omit for any serial");
  465.             sb.AppendLine("\t-r\t\tOpen remotely");
  466.             sb.AppendLine("\t-s   serverID\tServer ID, omit for any server");
  467.             sb.AppendLine("\t-i   ipAddress:port\tIp Address and Port. Port is optional, defaults to 5001");
  468.             sb.AppendLine("\t-p   password\tPassword, omit for no password" + Environment.NewLine);
  469.             sb.AppendLine("Examples: ");
  470.             sb.AppendLine(appName + " -n 50098");
  471.             sb.AppendLine(appName + " -r");
  472.             sb.AppendLine(appName + " -s myphidgetserver");
  473.             sb.AppendLine(appName + " -n 45670 -i 127.0.0.1:5001 -p paswrd");
  474.             MessageBox.Show(sb.ToString(), "Argument Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  475.  
  476.             Application.Exit();
  477.         }
  478.         #endregion
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.         public OutputChangeEventHandler Ifkit_OutputChange { get; set; }
  486.  
  487.  
  488.     }
  489. }
why do you need the complete code?
Jun 14 '10 #3
Curtis Rutland
3,256 Expert 2GB
DLLImport is an attribute. It doesn't stand alone, it decorates a method signature.

For example:
Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll")]
  2. 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.
Jun 15 '10 #4
ThatThatGuy
449 Expert 256MB
@mrcw
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...

http://www.codeproject.com/KB/cs/csharpreflection.aspx
Jun 15 '10 #5
mrcw
82
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?
Jun 15 '10 #6
ThatThatGuy
449 Expert 256MB
If you've already added a reference then why are you using DLL import
Jun 15 '10 #7
Curtis Rutland
3,256 Expert 2GB
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.
Jun 15 '10 #8
mrcw
82
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.
Jun 15 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.