我現在是用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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/269190.html
標籤:C#
上一篇:網頁一直顯示無法訪問
