【NX二次开发】NX内部函数,调用宏程序

调用宏:

#include <windows.h> 
#include <stdio.h> 


extern DllExport void ufusr(char *param, int *returnCode, int rlen)
{
    UF_initialize();


    // API调用UG的宏
    typedef void(*load_ufusr_f_p_t)(char* param, int* retcode, int paramLen);

    load_ufusr_f_p_t load_ufusr_ptr = NULL;
    char* file_path = "D:\\Program Files\\Siemens\\NX 12.0\\NXBIN\\libugui.dll";

    UF_load_library(file_path, "?MACRO_playback_from_usertool@@YAXPEBD@Z", (UF_load_f_p_t *)& load_ufusr_ptr);

    if (load_ufusr_ptr != NULL)
    {
        int retcode;
        load_ufusr_ptr("D:\\TOOL\\application\\jjhh.macro", &retcode, 1);
    }

    UF_terminate();
}

extern int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

 将上面这段代码封装:

#include <windows.h> 
#include <stdio.h> 

int CALL_MACRO(char *macro);
extern DllExport void ufusr(char *param, int *returnCode, int rlen)
{
    UF_initialize();
    CALL_MACRO("D:\\TOOL\\application\\jjhh.macro");
    UF_terminate();
}
extern int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

int CALL_MACRO(char *macro)
{
    char function_name[257] = "";
#ifdef _Win32
    strcpy(function_name, "?MACRO_playback_from_usertool@@YAXPBD@Z");
#else    
    strcpy(function_name, "?MACRO_playback_from_usertool@@YAXPEBD@Z");
#endif

    // API调用UG的宏
    typedef void(*load_ufusr_f_p_t)(char* param, int* retcode, int paramLen);
    load_ufusr_f_p_t load_ufusr_ptr = NULL;
    char* file_path = "D:\\Program Files\\Siemens\\NX 12.0\\NXBIN\\libugui.dll";
    UF_load_library(file_path, function_name, (UF_load_f_p_t *)& load_ufusr_ptr);
    if (load_ufusr_ptr != NULL)
    {
        int retcode;
        load_ufusr_ptr(macro, &retcode, 1);
    }
    return 0;
}

效果: 

 

 

下面是网上的一些代码片段,调用宏,宏运行有以下错误:

Macro playback halted at line #11 due to an NX error. 

1.

#include <windows.h> 
#include <stdio.h> 

typedef void (WINAPI *MyFun)(char const *);

int go_dll()
{
    MyFun fun = NULL;
    HINSTANCE hHandle = NULL;
    hHandle = LoadLibraryA("D:\\Program Files\\Siemens\\NX 12.0\\NXBIN\\libugui.dll");
    fun = (MyFun)::GetProcAddress(hHandle, "?MACRO_playback_from_usertool@@YAXPEBD@Z");

    if (fun != NULL)
    {
        fun("D:\\TOOL\\application\\jjhh.macro");
        uc1601("2", 1);
    }
    uc1601("1", 1);
    return 0;
}

extern DllExport void ufusr(char *param, int *returnCode, int rlen)
{
    UF_initialize();
    go_dll();
    UF_terminate();
}
extern int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

2.

#include <windows.h> 
#include <stdio.h> 

typedef void (WINAPI *MyFun)(char const *);
bool CallBack_Macro(char* MacroPath);
extern DllExport void ufusr(char *param, int *returnCode, int rlen)
{
    UF_initialize();
    if (CallBack_Macro("D:\\TOOL\\application\\jjhh.macro"))
    {
        uc1601("return true", 1);
    }
    UF_terminate();
}
extern int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

//调用宏
bool CallBack_Macro(char* MacroPath)
{
    HINSTANCE  module = LoadLibrary(L"libugui.dll");//无法将参数 1 从“const char[12]”转换为“LPCWSTR”    

    if (module != nullptr)
    {
        typedef void(*MACRO_playback_from_usertool)(char* param);
        MACRO_playback_from_usertool newClass = (MACRO_playback_from_usertool)GetProcAddress(module, "?MACRO_playback_from_usertool@@YAXPEBD@Z");//?MACRO_playback_from_usertool@@YAXPEBD@Z
        if (newClass != nullptr)
        {
            newClass(MacroPath);
            return TRUE;
        }
        else
        {
            uc1601("2", 1);
            return FALSE;
        }
    }
    else
    {
        uc1601("3", 1);
        return FALSE;
    }

    FreeLibrary(module);
}

3.

#include <windows.h> 
#include <stdio.h> 

//调UG宏(也可以调其他内部函数,下载DLL函数查看器可以看别人用了啥函数)
BOOL CALL_MACRO(char *macro)
{
    char function_name[257] = "";
    char file_path[257] = "D:\\Program Files\\Siemens\\NX 12.0\\NXBIN\\libugui.dll";

#ifdef _Win32
    strcpy(function_name, "?MACRO_playback_from_usertool@@YAXPBD@Z");
#else    
    strcpy(function_name, "?MACRO_playback_from_usertool@@YAXPEBD@Z");
#endif
    typedef void(*load_ufusr_f_p_t)(char* param);
    load_ufusr_f_p_t load_ufusr_ptr = NULL;
    UF_load_library(file_path, function_name, (UF_load_f_p_t *)& load_ufusr_ptr);
    if (load_ufusr_ptr != NULL)
    {
        load_ufusr_ptr(macro);
        return TRUE;
    }
    return FALSE;
}

extern DllExport void ufusr(char *param, int *returnCode, int rlen)
{
    UF_initialize();
    if (CALL_MACRO("D:\\TOOL\\application\\jjhh.macro"))
    {
        uc1601("return true", 1);
    }

    UF_terminate();
}
extern int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}

 

posted @ 2020-12-08 11:14  王牌飞行员_里海  阅读(315)  评论(0编辑  收藏  举报