By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

calling dll function using C sharp

P: n/a
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();
}
}
}
Jul 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

I ran across this article today which may or may not help you:

"Call Unmanaged DLLs from C#, Killing Processes Cleanly"

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

Jul 4 '07 #2

P: n/a
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);
......
"R. K. Wijayaratne" wrote:
Hi,

I ran across this article today which may or may not help you:

"Call Unmanaged DLLs from C#, Killing Processes Cleanly"

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

Jul 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.