Vince

Vince

  • NA
  • 1
  • 0

Importing C Dll to C#

Jul 2 2008 4:37 PM
Hiya!

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!

Answers (1)