Writing a C# aplication using provided DLL

If you have a question about the software please ask it here.

Writing a C# aplication using provided DLL

Postby danut1234 » Thu Aug 25, 2022 9:32 am

Hi, does anyone succeed in using the DLL files in C#? I am trying to write a software but i think that the C# wrapper is made for 64 bit system... i might be wrong.
danut1234
 
Posts: 37
Joined: Thu Aug 25, 2022 9:26 am

Re: Writing a C# aplication using provided DLL

Postby danut1234 » Thu Aug 25, 2022 11:03 am

And when i try to use the UC100_x86.dll it say it can not load it... Unable to load DLL 'UC100_x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) (i have copied the DLL files in the debug and release folders.... in fact i have copied them all over the folders...)

Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Control_CNC_AXBB
{
    public partial class Form1 : Form
    {

        [DllImport("UC100_x86.dll", CallingConvention = CallingConvention.Cdecl)]
        unsafe public static extern int ListDevices(ref int DevicesCount);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int number = 0;
            ListDevices(ref number);
            label1.Text = Convert.ToString(number);
           
        }
    }
}
danut1234
 
Posts: 37
Joined: Thu Aug 25, 2022 9:26 am

Re: Writing a C# aplication using provided DLL

Postby dezsoe » Thu Aug 25, 2022 11:13 am

The wrapper works fine on both 32 and 64 bits. How did you try it?
dezsoe
 
Posts: 2081
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Writing a C# aplication using provided DLL

Postby danut1234 » Thu Aug 25, 2022 12:23 pm

I used Visual Studio 2012, create a C# windows forms aplication
i created a button and text label
i tried to ADD Reference, and browse the DLL file, but it does not want to work...
i tried Add Existing Item and chose the cs wrapper file, i put all the DLL, wrapper, lib, in the debug and release folder. It says that "An attempt was made to load a program with an incorrect format" in the property of the project i have selected X86 CPU, not working.
i have tried the method that i have posted in a previous post, not working, cant find the file, although it is in the same folder with the application.

Do you have a simple CS application that you can share? just to show the serial number of the board or something like that, just to see that they communicate.
danut1234
 
Posts: 37
Joined: Thu Aug 25, 2022 9:26 am

Re: Writing a C# aplication using provided DLL

Postby dezsoe » Thu Aug 25, 2022 1:15 pm

OK, so you want to use the dll without UCCNC. UCCNC copies the wanted dll besides the UCCNC.exe. The UC100.dll is the 64 bit, UC100_x86.dll is the 32 bit version. So if you want to run on 32 bit then copy the _86.dll to your exe and rename it to UC100.dll.
dezsoe
 
Posts: 2081
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Writing a C# aplication using provided DLL

Postby cncdrive » Thu Aug 25, 2022 1:28 pm

The API dll is not a .net dll, but a native C++ dll, so you cannot add it as reference to a Visual Studio C# project, because it is a .net framework based project and the API dll is not.
The wrapper file is the bridge between the managed .net application and the dll.
You should add the wrapper to your project but not the dll.
Then copy the dll file into the same folder as your application exe or dll output is.
Then you can call the functions of the C# wrapper like UC100.function();

If you running 32bits mode then you should copy the UC100_x86.dll which is the 32bits version of the API dll, but you have to rename it to UC100.dll, because in the C# wrapper the Dllimports are for the UC100.dll filename, and if you runing on 64bits then copy the UC100.dll which is the 64bits versin.
cncdrive
Site Admin
 
Posts: 4764
Joined: Tue Aug 12, 2014 11:17 pm

Re: Writing a C# aplication using provided DLL

Postby danut1234 » Thu Aug 25, 2022 3:16 pm

If i rename the UC100_x86.dll to UC100.dll it gives me the error "Unable to load DLL 'UC100.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
If i use the original UC100.dll file it gives me the error "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

Do you have a very simple example, that works?
danut1234
 
Posts: 37
Joined: Thu Aug 25, 2022 9:26 am

Re: Writing a C# aplication using provided DLL

Postby cncdrive » Thu Aug 25, 2022 5:54 pm

No, we do not have an example sorry.
The example is the UCCNC software, it uses the same dll file for all motion control, but the source code of the UCCNC is not public, sorry.
cncdrive
Site Admin
 
Posts: 4764
Joined: Tue Aug 12, 2014 11:17 pm

Re: Writing a C# aplication using provided DLL

Postby cncdrive » Thu Aug 25, 2022 5:55 pm

And you should not dllimport the file yourself, jsut use the C# API and call it's functions.
cncdrive
Site Admin
 
Posts: 4764
Joined: Tue Aug 12, 2014 11:17 pm

Re: Writing a C# aplication using provided DLL

Postby eabrust » Fri Aug 26, 2022 12:24 am

Hi danut1234,

For a simple plugin example, look at this jog plugin, w/ source on github:
http://www.forum.cncdrive.com/viewtopic.php?f=14&t=3576#p26588

This example is in VB, but has a translated UC100 wrapper file copied into the solution, and shows how to make calls to the UC api. Once you have the wrapper (pull in the UC100_wrapper.cs class from the API/wrapper folder), it's pretty easy.

For example, calls on a pair of buttons to start/stop jog of an Xaxis in both directions are like this:
Code: Select all
 'X Left
    Private Sub btnJogLeft_Click(sender As Object, e As EventArgs) Handles btnJogLeft.MouseDown
        If rbJogCont.Checked Then
            UC100.JogOnSpeed(0, True, xvelPCT(nudJogVel.Value))
        End If
        If rbJogStep.Checked Then
            UC100.AddLinearMoveRel(0, nudJogStep.Value, 1, xvelPCT(nudJogVel.Value), True)
        End If
    End Sub
    Private Sub btnJogLeft_Click1(sender As Object, e As EventArgs) Handles btnJogLeft.MouseUp
        If rbJogCont.Checked Then
            UC100.JogOnSpeed(0, True, 0)
        End If
    End Sub

    'X Right
    Private Sub btnJogRight_Click(sender As Object, e As EventArgs) Handles btnJogRight.MouseDown
        If rbJogCont.Checked Then
            UC100.JogOnSpeed(0, False, xvelPCT(nudJogVel.Value))
        End If
        If rbJogStep.Checked Then
            UC100.AddLinearMoveRel(0, nudJogStep.Value, 1, xvelPCT(nudJogVel.Value), False)
        End If
    End Sub
    Private Sub btnJogRight_Click1(sender As Object, e As EventArgs) Handles btnJogRight.MouseUp
        If rbJogCont.Checked Then
            UC100.JogOnSpeed(0, False, 0)
        End If

    End Sub


good luck

Eric
CraftyCNC: Plugins for UCCNC (and other neat stuff): http://www.craftycnc.com/plugins-for-uccnc/
eabrust
 
Posts: 355
Joined: Fri Sep 16, 2016 2:32 am
Location: Near Shirland IL, USA

Next

Return to Ask a question from support here

Who is online

Users browsing this forum: No registered users and 19 guests