求助大佬们!!!C#,C++,UG/NX二次开发

qq_46130698 2021-03-21 04:56:04
我现在是用C#练习UG的二次开发,想通过C#调用GRIP,所以需要对C++中的这个函数UF_call_grip进行调用。
开始时先用以下的主要代码:
using NXOpen;
using NXOpen.UF;
using System;
using System.Collections.Generic;//C# List<>的命名空间
using System.Runtime.InteropServices;//DllImport的命名空间,定义结构体时也必须添加
public class OpenAPI
{
[DllImport("libufun.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "UF_call_grip")]
internal static extern int _CallGrip(string grip_executable, int argument_count, ref Args gruf_arg_list);
}


public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();

//TODO: Add your application code here

NXOpen.Utilities.JAM.StartUFCall();
string path = "D:\\test.txt";
IntPtr init = Marshal.StringToHGlobalAnsi(path);
int grip_arg_count = 1;
Args[] grip_arg_list = new Args[1];
grip_arg_list[0].address = init;
grip_arg_list[0].length = 0;
grip_arg_list[0].type = UFConstants.UF_TYPE_CHAR;
OpenAPI._CallGrip("E:\\VSandUGDEMO\\xianshi.grs", grip_arg_count, ref grip_arg_list;
NXOpen.Session.UndoMarkId markId1;
NXOpen.Utilities.JAM.EndUFCall();

theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----

}
return retValue;
}
提示出现:无法从“ref NXOpen.UF.Args[]”转换为“ref NXOpen.UF.Args”这一个问题。(标颜色的地方)
后来我把定义
Args[] grip_arg_list = new Args[1];
grip_arg_list[0].address = init;
grip_arg_list[0].length = 0;
grip_arg_list[0].type = UFConstants.UF_TYPE_CHAR;

改成:
Args grip_arg_list;
grip_arg_list.address = init;
grip_arg_list.length = 0;
grip_arg_list.type = UFConstants.UF_TYPE_CHAR;

生成.dll文件之后在UG上运行,出现了如下图的错误提示:

这又是啥问题呢。。。有两个还不能解决的问题:(1)变量的类型从C++转变到C#上是不是出现了问题?类似C++中Char*转换成C#的string,那C++中的UF_args_p_t怎么转换成C#中对应的类型呢?(2)想着直接在C++中使用UF_call_grip函数,在通过生成的.dll引用到C#,但是引用不上。这咋整啊?
恳请各位大佬帮帮快要疯掉的孩子吧。。。。

贴一个UF_call_grip函数
extern UFUNEXPORT int UF_call_grip (
char *grip_executable, /* <I>
Name of GRIP program to execute (file name
or full path name).
*/
int count, /* <I>
Count of arguments to pass to GRIP executable
*/
UF_args_p_t UFARGS /* <I,len:count>
An array of structures where each element in
the array is a structure that contains an
arguments type, size, and address. Note that
if an argument is type UF_TYPE_CHAR, the array
must be initialized prior to the UF_call_grip
call.
*/
);

struct UF_args_s
{ int type; /* one of the types, UF_TYPE_DOUBLE,
UF_TYPE_DOUBLE_ARRAY, UF_TYPE_TAG_T,
UF_TYPE_TAG_T_ARRAY, UF_TYPE_CHAR or
UF_TYPE_CHAR_ARRAY. */
int length; /* the number of elements in an array, for
types UF_TYPE_DOUBLE_ARRAY,
UF_TYPE_TAG_T_ARRAY or UF_TYPE_CHAR_ARRAY. */
void *address; /* A pointer to an array of the given
type and length. Note that for an array
of UF_TYPE_CHAR, if the argument list
is built by the Open program, it must
initialize this array whether it is input or
output. An output array can be initialized
by setting the first byte to '\0'. */
};

typedef struct UF_args_s UF_args_t, *UF_args_p_t;
...全文
132 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧