[Skip to Content]

Mach3 Control via C Sharp Winform Application

Published on Monday 25 November 2013
Categories: DIY Pick and Place | General |

This demo app allows the user to send Gcode commands to Mach 3 to control a CNC mill, lathe or other cnc hardware.

The application was made using Visual Studio 2013 and the demo app can be downloaded here.

First create a new Windows Forms Application:

create a new Windows Forms Application

Under the Solutions Explorer expand the References entry, right click and select Add Reference:

Add Reference

Click the Browse button to open the file explorer:

Browse button to open the file explorer

Browse to the folder where you have installed Mach3, normally c:\Mach3 and select the Mach3.exe file and click Add, then on the next screen click OK.

Mach3 folder

Back in Visual Studio add a textbox and a button to your form. Use the default names for the textbox and button. Double click the form header to add the form load event handler and double click the button to add the button click handler.

button click handler

In the top section of the code add:

using System.Runtime.InteropServices;

At the top of your Form1 class add:

private Mach4.IMach4 Mach = null;
private Mach4.IMyScriptObject Script = null;

In the Form1_Load handler add:

Mach = (Mach4.IMach4)Marshal.GetActiveObject("Mach4.Document");
Script = (Mach4.IMyScriptObject)Mach.GetScriptDispatch();

In the button1_Click event handler add:


Now you need to launch Mach3 using Run as Administrator.

Build the application and go to the build folder and Run as Administrator. You will now be able to enter gcode commands in the text box and they will be sent to Mach3 when the button is pressed.

If you need to update the GUI while the command is running you will need to use Threading in the application and run the Script.Code commands under their own thread.

Use this code at your own risk! I accept no responsibility if your home blows up, computer crashes or cnc mill drives away as a result of using this demo.

It appears that some installations of Mach3 do not correctly set the registry keys for the Mach4 objects. I have exported all the registry keys which Mach3 has installed on my computer into a text file which will need to be renamed from .txt to .reg to install the missing registry keys. Download reg file.

Also ensure that you have the latest version of Mach3 installed as the older versions may not compile correctly.



thai nguyen quoc

01 November 2018 at 5:01 pm

Dear Mr.Brian Dorey.
i work with vs2005 c#.
Please help me to modify your example project to vs2005 and run on windows XP.
Thanks you very much.



02 October 2019 at 11:33 pm

Hello I did everything what I can , but I still getting error message that I can not get access to Mach3. Is it any secret settings in Mach3 that must be done before running?
Thank you

David J Gall

22 November 2019 at 4:09 pm

This would be the "brute force" hostile takeover of Mach3 if it worked. A far more elegant (and documented!) approach that actually works is to download the Mach3 SDK and simply write a Mach3 plugin. Yes you can write plugins in C-Sharp using NET and WinForms. There's even a Visual Studio Project Wizard that works in Visual Studio versions up through VS 2010 to start your project. Then you can open the project in VS 2013 or later and VS will "convert" the project to the newer format. A more useful enterprise might have been to update the project wizard to work in VS 2013 or newer....

Leave a comment