calling dll function using C sharp

G

Guest

Hi,
I wrote an simple application using VS2005 C#. I am trying to call some
functions in my other dll file. This dll is written in EVC for running on
Windows CE 5.0.

This dll file has the following function exported:
void init_GPIO();
void set_GPIO_Value(int value);
void set_GPIO_Direction(int direction);
UInt16 get_GPIO_Value();
UInt16 get_GPIO_Direction();

I already have an C application written using EVC to load dll using
"LoadLibrary" and "GetProcAddress" succesfully without problem.

Now when I try to use VS2005 C# to call the same function using the
following code, only function without passing any parameter will work (the
init_GPIO() function), all other function will raise an exception when
calling.

Can anyone give me some hint on this?

---------------------------------------------------------------
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GPIOSDK
{
public partial class Form1 : Form
{
[DllImport("GPIO.dll", CharSet = CharSet.Auto)]
public static extern void init_GPIO();
public static extern void set_GPIO_Value(int value);
public static extern void set_GPIO_Direction(int direction);
public static extern UInt16 get_GPIO_Value();
public static extern UInt16 get_GPIO_Direction();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//Initial GPIO interface
init_GPIO();
}

private void button1_Click(object sender, EventArgs e)
{
//Set all output high
set_GPIO_Value(0x3FF);
set_GPIO_Direction(0x3FF);
}

private void button2_Click(object sender, EventArgs e)
{
//Set all output low
set_GPIO_Value(0x0);
set_GPIO_Direction(0x0);
}

private void button3_Click(object sender, EventArgs e)
{
UInt16 value;
//Show current value
value=get_GPIO_Value();
textBox1.Text = value.ToString();
}
}
}
 
G

Guest

Thanks for the reply,
I found the problem was every function need to have DllImport():
[DllImport("GPIO.dll", CharSet = CharSet.Auto)]
public static extern void init_GPIO();
[DllImport("GPIO.dll", CharSet = CharSet.Auto)]
public static extern void set_GPIO_Value(int value);
......
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top