Using C DLL with C#

sambath kumar posted at 03-Jul-08 03:14

Hi

I've got a question about importing C dlls and using them in C#.

Here are two of the C dll functions:
-----------------------------------------

1) DLL_API short getMessage(unsigned char *msg, unsigned short maxLength)

2) DLL_API short getHitDataSetValue(unsigned char *msg, unsigned short q, float *fVal)

there are other functions in the DLL but they are just void so they work fine.

here is my test code:
-------------------------

using System;
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 WindowsApplication1
{
unsafe public partial class Form1 : Form
{

static byte[] messages = new byte[32767];
float v;

public Form1()
{
InitializeComponent();
}

[DllImport("Pacdplv.dll", EntryPoint = "openPCI_DSP")]
public extern static short openPCI_DSP();

[DllImport("Pacdplv.dll", EntryPoint = "closePCI_DSP")]
public extern static short closePCI_DSP();

[DllImport("Pacdplv.dll", EntryPoint = "startTest")]
public extern static short startTest();

[DllImport("Pacdplv.dll", EntryPoint = "stopTest")]
public extern static short stopTest();

[DllImport("Pacdplv.dll", EntryPoint = "poll")]
public extern static short poll();

[DllImport("Pacdplv.dll", EntryPoint = "getMessage")]
public extern static short getMessage(byte* msg, short maxLength);

[DllImport("Pacdplv.dll", EntryPoint = "getHitDataSetValue")]
public extern static short getHitDataSetValue(byte* msg, short q, float* fVal);

private void Form1_Load(object sender, EventArgs e)
{

}

private void startBtn_Click(object sender, EventArgs e)
{
if (openPCI_DSP() == 0)
{
textBox1.Text += "opened";
startTest();
}
else
textBox1.Text += "cannot open";
}

private void stopBtn_Click(object sender, EventArgs e)
{
closePCI_DSP();
textBox1.Text += "\r\n";
textBox1.Text += "close";
}

private void startTestBtn_Click(object sender, EventArgs e)
{

textBox1.Text += "\r\ntest started";
poll();
fixed (byte* p = messages)
{
if (getMessage(p, 32767) == 0)
{
textBox1.Text += p[2];
textBox1.Text += "\r\n";

fixed (float* q = &v){
getHitDataSetValue(p, 16, q);
textBox1.Text += q.ToString();
}
}
}
}

private void stopTestBtn_Click(object sender, EventArgs e)
{
stopTest();
textBox1.Text += "\r\ntest stopped";
}
}
}

----------
i have a sensor which detects vibration connected to a PCM-CIA slot and the DLLs will access the machine. I currently have some problems with my outputs (i touch the sensor and it doesn't pick up anything on my outputs)

My question is: am i using the pointers correctly? I had to replace the "unsigned" parameters with "byte" since C# doesn't take unsigned as a key word. I use byte since it's an unsigned 8-bit character anyways so i think that's ok for my extern function. I have a feeling i'm not passing things correctly.

Any help would be much appreciated! Thanks!

Sambath




Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  Using C DLL with C# - sambath kumar  03-Jul-08 3:14:29 AM
      Check with these links - This will fix your issue - santhosh kumar  03-Jul-08 3:39:42 AM
      refer this link : - Deepak Ghule  03-Jul-08 3:52:59 AM
      See this - Sujit Patil  03-Jul-08 4:30:45 AM
      Solution to c dll - Umapathy Kaliaperumal  03-Jul-08 4:32:05 AM
      Reply - alice johnson  03-Jul-08 1:32:51 PM
View Posts