Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Run external dll inside my program


Recommended Posts

Hello

I would run the DLL "SilhouetteBoundary.dll" from my program VB.NET.

I tried to add the dll in "Add Reference " using the tab "Browse" with which you can 'select a file outside, but when I click on the dll I get a sign that says:

 

"A reference to 'c:\programmi\mcamx5\chooks\silhouetteboundary.ddl" could not be added. Please make sure that the file is accessible, and that is a valid assembly or COM component.

 

Is there another way to load it on my program?

Thanks for the answer!

Link to comment
Share on other sites

"Add Reference" is not going to work - as you've discovered.

 

SilhouetteBoundary.DLL is a CHook.

This means that it is "unmanaged" C++ code.

To call unmanaged code from managed (.NET) code you can use Platform Invoke (aka. P/Invoke)

and call any of the exported functions within the unmanaged DLL.

 

Every CHook must have an exported "m_main" function.

*More notes... ;)

"m_main" is the function that Mastercam calls you to do a Run User App,

either from the Settings menu or ALT-C and directly selected the DLL.

If you can "run" a CHook from a toolbar and/or menu that CHook DLL may have additional entry point functions

that get called based on the information the the Function Table (FT) file that "tells" Mastercam about the DLL.

The one thing you can count on is that if an exported function is meant to be a std "CHook Entry Point",

that its signature will be like "m_main"; a function that take one integer as a parameter and returns an integer.

 

To "run" SilhouetteBoundary.DLL from a .NET application, you can do this ->

 

 

Imports System.Runtime.InteropServices

''' <summary>
''' Encapsulates the DllImports from the C-Hook DLL
''' </summary>	
Class CHookInterop

' The name of the DLL to call into.
' This DLL must exist in the same folder as the caller.
' If not we must supply the path to the DLL in this declaration ->
Public Const CHookDLL As String = "SilhouetteBoundary.DLL"

' This is the signature of the required 'm_main' function in a CHook DLL
' extern "C" __declspec(dllexport) int m_main (int not_used)
'
' Note the we are using EntryPoint to alllow use to give a friendly 
' alias name to call. (This is optional)
<DllImport(CHookDLL, EntryPoint := "m_main", CharSet := CharSet.Auto)> _
Public Shared Function RunSilhouetteBoundary(not_used As Integer) As Integer
End Function

End Class


' NETHook Entry Point
Public Overrides Function Run(param As Integer) As MCamReturn
Dim result As MCamReturn = MCamReturn.NoErrors
Try

	' Example of calling into an external "unmanaged" DLL ->
	Dim rv As Integer = CHookInterop.RunSilhouetteBoundary(0)
	' Normally we really don't care about the return from calling into a CHook
	' But you can check it.  It will be an MC_RETURN type value.

Catch ex As Exception
	MessageBox.Show(ex.Message, "UnHandled Exception: " + ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
	result = MCamReturn.ErrorOccurred
End Try
Return result
End Function

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...