Hi
I am trying to import a dll. So far I have got - 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?
8 1576
OK here is the complete code (all 400 lines) - 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?
DLLImport is an attribute. It doesn't stand alone, it decorates a method signature.
For example: - [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.
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?
If you've already added a reference then why are you using DLL import
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
| |