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:

Calling CNC software DLLs


Recommended Posts

I like the Silhouette and Create Boundary DLLs. However, without knowing any of the functions, I cannot run the dll. For instance, I can use a LoadLibrary and FreeLibrary to open and close the external dll, but I think I need a function inside of that dll to call. I just want to run Sillhouette boundary function inside my code to create a toolpath boundary that I will chain together. Any ideas on how to do this?

Link to comment
Share on other sites

quote:

but I think I need a function inside of that dll to call

Of course you do...

 

All C-Hooks must have a m_main function, as that is the function that is called by Mastercam when you execute the CHook DLL via the 'Setting - Run User Application' menu (ALT-C)

 

If the C-Hook is activated via the Function Table (the DLL has an .FT file in the CHooks folder), the FT can specify a different "exported" function other than m_main.

Which would be specified in the .FT file like this ->

FUNCTION CPP "ArcMultiEdit"

Link to comment
Share on other sites

Thank you. I am quite familiar with the chook process, and I've written plenty of chooks so I am familiar with m_main, I just installed my 6th SDK today. I do not simply run my chooks from the chooks folder. I wrap them with the Net-hook using script linker to a vbs file that I then make a button for. This vbs calls multiple chooks at once. I will look into the function table tomorrow, and I hope to find the names of other "exported" functions that I can use.

 

In one of these chooks I would like to run your chooks like SilhouetteBoundary.dll. I ran some online utilities to see what the Silhouette and Create Boundary dlls export, and found these couple of functions:

 

CreateBoundaryStart and GetSilhouetteBoundary

 

But I do not know about what arguments these need, or return. For instance, if I wanted to pass all of my model's drive surfaces to SilhouetteBoundary.dll, how would I do that?

 

Please tell me if I can be more clear on any of these questions/comments.

Link to comment
Share on other sites

Though I’m not familiar with the inter-workings of these functions, here is what I could discover...

 

SilhouetteBoundary.dll

When you run this as a C-Hook (and m_main is called), the user is prompted to select ‘geometry’ and then an internal function is called with this parameter -> (CHAIN **chains)

The exported function GetSilhouetteBoundary has the same input parameters and just calls the internal function mentioned above, just passing along the “chains” to that function.

GetSilhouetteBoundary has a return type of ‘int’ and always returns ‘0’ (aka. MC_NOERROR).

 

CreateBoundary.dll

The exported function CreateBoundaryStart has the same functionality as m_main. (m_main just calls CreateBoundaryStart) This is like I mentioned in the previous posting; an alternate function name that can be called via the function table, which would have the same function “signature” as m_main ->

int myfunction (int param)

Link to comment
Share on other sites

arenner,

 

Roger knows that you know how to make a chook. What he's pointing out is that these dll's are also chooks, which mean the only function that they have exposed is m_main, just like the ones you make.

 

When looking for function names to call into mastercam's code with, check the header files that ship with the SDK, but when looking to call into another chook, m_main is your boy.

Link to comment
Share on other sites

When I try something like this, as expected, I get a link error.

code:

  

extern "C" __declspec(dllimport) int GetSilhouetteBoundary(CHAIN ** chains);

What .lib file links this function, that I can put in my project properties?

 

Then I was going to put

 

GetSilhouetteBoundary(CHAIN ** chains);

 

into my chook's m_main.

 

I really don't think I'm making my question clear. I simply want to do the equivalent of the vb function "RunMastercamChook(mychook)". But instead of from a vbscript, I want to do it from a chook.

Link to comment
Share on other sites

Since SilhouetteBoundary.dll is a CHook, it is not referenced in any of the LIB files in the SDK. Meaning, you cannot use the dllimport format.

 

When Mastercam “runs” a CHook, it needs to do what any process has to do when calling a function within another DLL. It must “load” that DLL, obtain the address of the function it wishes to call, call that function and then “free” that DLL when it is done with it. A side note: Mastercam does not(default behavior) free a CHook DLL immediately after it has been “run”, but does so when Mastercam itself is exiting.

 

This is done with the functions ->

LoadLibrary()

GetProcAdress()

FreeeLibrary()

 

If we’re using MFC and are dynamically linked to MFC you can (should?) use the MFC variants of LoadLibrary and FreeLibrary.

These are -> AfxLoadLibrary and AfxFreeLibrary

See the MSDN documentation for more info)

 

The following code is the m_main function from a “caller” CHook

It “loads” the DLL (SilhouetteBoundary.dll) that contains the function it wishes to call (m_main), obtains the address of that function, calls it, and then frees the DLL.

If you replace the m_main function of a std. (X2-MR2) Chook with this code, when you run this Chook, it “runs” the SilhouetteBoundary.dll by calling ‘m_main’ in SilhouetteBoundary.dll (the “callee”), just has if you run the SilhouetteBoundary.dll Chook "directly" via the Setting - Run User Application (ALT-C) and selected the SilhouetteBoundary.dll

 

code:

extern "C" __declspec(dllexport) int m_main (int not_used)

{

AFX_MANAGE_STATE(AfxGetStaticModuleState());

 

// You need to cast the return from GetProcAddress to match the function signature

// of the function are retrieving the address of.

typedef int (* Proc) (int); // int m_main (int)

 

// You call AfxLoadLibrary (or LoadLibrary) to load the specified DLL in your address space

//HINSTANCE AFXAPI AfxLoadLibrary( LPCTSTR lpszModuleName );

CString moduleName = _T("SilhouetteBoundary.dll");

HINSTANCE hDLL = AfxLoadLibrary(moduleName);

if (!hDLL)

{

CString msg;

msg.Format(_T("Error: AfxLoadLibrary - Did not obtain a handle to -> %s"), moduleName);

AfxMessageBox(msg);

return MC_ERROR;

}

 

// You call GetProcAddress to get a function pointer to the function you're interested in

//FARPROC GetProcAddress( HMODULE hModule, LPCSTR lpProcName );

CString functionName = _T("m_main");

Proc proc = (Proc)GetProcAddress( hDLL, functionName);

 

if (!proc)

{

CString msg;

msg.Format(_T("Error: GetProcAddress - Did not obtain the adress of -> %s"), moduleName);

AfxMessageBox(msg);

return MC_ERROR;

}

 

// call the function - via the function pointer

int param = 0;

int ret = proc(param);

 

// When you are done, you need to call AfxFreeLibrary (FreeLibrary)to free your reference to the DLL

//BOOL AFXAPI AfxFreeLibrary( HINSTANCE hInstLib );

BOOL ok = AfxFreeLibrary( hDLL );

return MC_NOERROR;

}

To call GetSilhouetteBoundary in the DLL you would of course need to make some changes...

 

The typedef must match the signature of that function.

 

CHAIN * chain = NULL;

typedef int (* Proc) (CHAIN **); // int GetSilhouetteBoundary(CHAIN ** chains)

 

You would pass "GetSilhouetteBoundary" to GetProcAdress

 

And change the function call via this pointer to capture the 'proper' return data type and supply the 'proper' parameter data type the function (GetSilhouetteBoundary) expects.

 

Note that I (myself) don't know exactly what GetSilhouetteBoundary will do.

The above info is just to show you a way to call functions that are in another DLL.

Disclaimer: Just because you can call a function in a DLL, doesn't mean that everything will always work out as you might hope. You must realize that Mastercam has loaded your 'caller' CHook and called it, which is loading and calling into another 'callee' DLL.

Link to comment
Share on other sites

First, thank you for your time Roger. Next, I want you to be sure, that since the first time you mentioned it, I have taken note that SilhouetteBoundary.dll is a chook. And while I've developed chooks, and know some about how to write code within them, I know very little about how to control them, and work back and forth with them. So you mentioning that it is a chook, I believe you are trying to remind me that because it is a chook, you can only do "x" when calling and receiving from it. But I don't really know much about "x", so just telling me it's a chook doesn't signal any "ahas" for me. Therefore, I've asked about dllimport, .lib files, and a lot about "running" and "calling" dlls. So: "it needs to do what any process has to do when calling a function within another DLL" doesn't get me very far headscratch.gif

 

That is why I really appreciate the example code smile.gif As you'll see in my first post, my best guess was the LoadLibrary and FreeLibrary functions. Thanks for extending it and reassuring me that this is the right way to call that dll.

 

You'll have to forgive my ignorance on a lot of these issues (as you've done before for other complaints of mine) bonk.gif I have a mechanical engineering degree and am working on my industrial engineering master's. So programming isn't my primary skill-set. Our research work takes full advantage of the open SDK, and is one of the main reasons we went with Mastercam.

 

I will continue to try things with the GetSilhouetteBoundary function. I am guessing (hoping) it will return the chain of boundary lines that it created, and is expecting selected surfaces/solids. But I will test that. Thanks.

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...