次开发C使用DllImport调

在写这篇文章的时候,我正在头晕,因为下班坐车回家,有些晕车了。头疼的要死。也吃不下去饭。

早就想些这篇文章了,但是最近临近中秋十一,晚上太忙了。

版本:NX11+VS

最近这一年已经由C++过度到C#,改用C#做应用程序开发和NX二次开发。

C#在做复杂界面开发的时候,WinFrom要比MFC简单的多(这个时候纯BlockUI已经满足不了集成功能的复杂界面需求了),数据库连接也简单。

言归正传

在我经过一段时间的看QQ群别人讨论技术,给了我点启发,以及带着兴趣百度到了一些相关资料。学会了这种方法。

1.有需求

在用C#做NX二次开发的时候,我们一般需要引用NX官方封装好的的这几个dll。

用C#的,一般都是用NXOpen的比较多,用UF的比较少,因为UF官方封装的不全,有很多没有封装。也因为C#用NXOpen语言上特别简单,好用。

不需要delete,不需要迭代器,快速转换成字符串等等。

那么在项目开发中,如果遇到loop这种官方没有封装的函数怎么办?

UF_MODL_ask_face_loopsUF_MODL_ask_loop_list_countUF_MODL_ask_loop_list_itemUF_MODL_delete_loop_list

没关系,官方没有封装的UF函数,我们可以直接去这5个dll里调用。用DllImport去调用(是什么,怎么用请自行百度)。

libufun.dlllibnxopencpp.dlllibugopenint.dlllibnxopenuicpp.dlllibopenpp.dll

比如,我们现在想要调用UF_OBJ_set_color这个函数(这个函数官方已经封装了,这里只讲调用是怎么用的),给一个块设置颜色。

2.反编译C.dll

首先,找到这个函数,在上面5个中的哪个dll里。

这时就需要使用VC程序的反编译工具Depends.exe这个工具了,按个打开dll,去里面找到看有没有这个函数。

功能介绍:

查看PE模块的导入模块查看PE模块的导入和导出函数动态剖析PE模块的模块依赖性解析C++函数名称

等等

这里我先打开libufun.dll这个函数

错误不用管,点确定关掉

然后在这里找看有没有UF_OBJ_set_color

已经找到了,我们知道了可以去这个dll里调UF函数。

下一步我们要开始去C#里调用,但是怎么调用?不知道?对吧?

3.反编译.net.dll

这个时候又需要另外一个反编译dll工具了,dnSpy。

功能介绍:

dnSpy中文版是一款net程序反编译工具,可以对net程序进行反编译,还有替代库文档的功能,如果遇到了代码丢失或者损坏的情况,可以直接恢复。还可以设断点加进程调试代码。

我们打开后是这个样子

下面我们打开官方封装的C#NXOpen.UF这个dll,去反编译下,看看里面都写了什么。(dnSpy是可以反编译别人写的普通的C#exe和dll的,桌面应用程序和NX二次开发的C#程序都可以反编译出来源代码,只要不加壳,加壳能反编译出部分代码)

打开后如下

我们先去找到封装的UF_OBJ_set_color这个函数

这个时候我们就找到了,官方是怎么调用,怎么封装的NXOpen.UF.dll的了,是有出处的。

4.我们如何调用

我们把这句抄下来,去我们的NXC#项目中使用。

[DllImport(libufun.dll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi,EntryPoint=UF_OBJ_set_color)]internalstaticexternint_SetColor(Tagobject_id,intcolor);

先加命名空间

usingSystem;usingNXOpen;usingNXOpen.UF;usingSystem.Collections.Generic;//C#List的命名空间usingSystem.Runtime.InteropServices;//DllImport的命名空间

在添加一个类,写代码

然后在项目的Main入口函数里写代码。创建块-特征找体-设置颜色(用我们调用的函数)

//创建块FeatureSignssign=FeatureSigns.Nullsign;//定义布尔double[]cornet_pt={.0,0.0,0.0};//定义原点string[]edge_len={.0,.0,.0};//定义长宽高Tagblk_obj_id=Tag.Null;theUfSession.Modl.CreateBlock1(sign,cornet_pt,edge_len,outblk_obj_id);//特征找体TagBodyTag=Tag.Null;theUfSession.Modl.AskFeatBody(blk_obj_id,outBodyTag);NXOpen.Utilities.JAM.StartUFCall();//设置颜色(调用UF函数)OpenAPI._SetColor(BodyTag,);NXOpen.Utilities.JAM.EndUFCall();Caesar卢尚宇年9月29日

在我们反编译的代码中可以看到,在调用UF函数的时候,一定要用

NXOpen.Utilities.JAM.StartUFCall();和NXOpen.Utilities.JAM.EndUFCall();

这两个方法来开始和结束,中间调用。执行才有效,要不然执行不起作用。有点类似UF的

UF_initialize和UF_terminate

下面来执行下,我们上面的代码。看看效果。答案是可以调用的。也是这种方法调用的。

5.如何调用loop等函数

好,现在我们已经知道怎么调用函数了,现在我们去调用loop那些函数。可以借鉴下已经封装的UF_MODL_ask_list_item是怎么定义输入输出的。

NX11+VSusingSystem;usingNXOpen;usingNXOpen.UF;usingSystem.Collections.Generic;//C#List的命名空间usingSystem.Runtime.InteropServices;//DllImport的命名空间publicclassOpenAPI{[DllImport(libufun.dll,CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi,EntryPoint=UF_OBJ_set_color)]internalstaticexternint_SetColor(Tagobject_id,intcolor);[DllImport(libugopenint.dll)]publicstaticexternvoiduc(stringmsg,intmode);[DllImport(libufun.dll,EntryPoint=UF_MODL_ask_face_loops)]publicstaticexternvoidAskFaceLoops(Tagfaceld,outIntPtrloopList);[DllImport(libufun.dll,EntryPoint=UF_MODL_ask_loop_list_count)]publicstaticexternvoidAskLoopListCount(IntPtrloop,outintcount);[DllImport(libufun.dll,EntryPoint=UF_MODL_ask_loop_list_item)]publicstaticexternvoidAskLoopListItem(IntPtrloopList,intindex,outinttype,outIntPtrlist);[DllImport(libufun.dll,EntryPoint=UF_MODL_delete_loop_list)]publicstaticexternvoidDeleteLoopList(outIntPtrloopList);[DllImport(libufun.dll,EntryPoint=UF_MODL_ask_list_count)]publicstaticexternvoidAskListCount(IntPtrlist,outintcount);[DllImport(libufun.dll,EntryPoint=UF_MODL_ask_list_item)]publicstaticexternvoidAskListItem(IntPtrlist,intindex,outTagobj);[DllImport(libufun.dll,EntryPoint=UF_MODL_delete_list)]publicstaticexternvoidDeleteList(outIntPtrlist);[DllImport(libufun.dll,EntryPoint=UF_MODL_create_exp)]publicstaticexternvoidCreateExp(stringexpr_str);}publicclassProgram{//classmembersprivatestaticSessiontheSession;privatestaticUItheUI;privatestaticUFSessiontheUfSession;publicstaticProgramtheProgram;publicstaticboolisDisposeCalled;//------------------------------------------------------------------------------//Constructor//------------------------------------------------------------------------------publicProgram(){try{theSession=Session.GetSession();theUI=UI.GetUI();theUfSession=UFSession.GetUFSession();isDisposeCalled=false;}catch(NXOpen.NXExceptionex){//----Enteryourexceptionhandlingcodehere-----//UI.GetUI().NXMessageBox.Show(Message,NXMessageBox.DialogType.Error,ex.Message);}}///summary///获取面的loop////summary///paramname=face输入面的tag/param///paramname=loops输出lopps链表/parampublicstaticvoidMyAskFaceLoops(Tagface,outListListTagloops){NXOpen.Utilities.JAM.StartUFCall();loops=newListListTag();IntPtrloopList=IntPtr.Zero;OpenAPI.AskFaceLoops(face,outloopList);intloopCount=0;OpenAPI.AskLoopListCount(loopList,outloopCount);varlistPtrList=newListIntPtr();for(inti=0;iloopCount;i++){vartagList=newListTag();inttype=0;IntPtrlist=IntPtr.Zero;OpenAPI.AskLoopListItem(loopList,i,outtype,outlist);listPtrList.Add(list);intlistCount=0;OpenAPI.AskListCount(list,outlistCount);for(intj=0;jlistCount;j++){Tagobj=Tag.Null;OpenAPI.AskListItem(list,j,outobj);tagList.Add(obj);}loops.Add(tagList);OpenAPI.DeleteList(outlist);}OpenAPI.DeleteLoopList(outloopList);NXOpen.Utilities.JAM.EndUFCall();}//------------------------------------------------------------------------------//ExplicitActivation//Thisentrypointisusedtoactivatetheapplicationexplicitly//------------------------------------------------------------------------------publicstaticintMain(string[]args){intretValue=0;try{theProgram=newProgram();//TODO:AddyourapplicationcodehereTagaa=(Tag);//通过移刀工具得到面的tagtheUfSession.Obj.SetColor(aa,1);//获得面的所有loopListListTagloops=newListListTag();MyAskFaceLoops(aa,outloops);//高亮面的所有loopfor(inti=0;iloops.Count;i++){for(intj=0;jloops[i].Count;j++){theUfSession.Ui.DisplayMessage(1,1);theUfSession.Disp.SetHighlight(loops[i][j],1);}}theProgram.Dispose();}catch(NXOpen.NXExceptionex){//----Enteryourexceptionhandlingcodehere-----}returnretValue;}//------------------------------------------------------------------------------//Followingmethoddisposesalltheclassmembers//------------------------------------------------------------------------------publicvoidDispose(){try{if(isDisposeCalled==false){//TODO:Addyourapplicationcodehere}isDisposeCalled=true;}catch(NXOpen.NXExceptionex){//----Enteryourexceptionhandlingcodehere-----}}publicstaticintGetUnloadOption(stringarg){//Unloadstheimageexplicitly,viaanunloaddialog//returnSystem.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);//UnloadstheimageimmediatelyafterexecutionwithinNXreturnSystem.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);//UnloadstheimagewhentheNXsessionterminates//returnSystem.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);}}Caesar卢尚宇年9月29日

6.反编译别人的NX二次开发.net.dll源代码

这里博主我不提倡随便就去反编译别人的源代码,要尊重作者的开发成果。

至于什么时候,反编译,自行斟酌吧,哈哈~~。

在不加壳,加密的时候,.net很容易被反编译,加壳也能去破解的,毕竟是托管语言,只要在中间就能反编译,不像C++那种直接到内存中。

例子:就用我上面写的loop例子,用DnSpy反编译源代码

Caesar卢尚宇

年9月29日




转载请注明:http://www.shhjfk.com/zytd/zytd/15117.html

  • 上一篇文章:
  •   
  • 下一篇文章: 没有了