C#Dllimport - 指向接收数组的指针

时间:2015-07-21 19:40:47

标签: c# pointers dllimport

我正在尝试DLLImport函数simxGetObjects 来自v-rep软件的remoteApi.dll。这是功能描述的链接: http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctions.htm#simxGetObjects

以下是上述链接中此功能的简要说明:

描述:检索给定类型或所有类型(即所有对象句柄)的对象句柄

C简介: simxInt simxGetObjects(simxInt clientID,simxInt objectType,simxInt * objectCount,simxInt ** objectHandles,simxInt operationMode)

C参数:
clientID:客户端ID。请参阅simxStart。

对于任何类型的对象,

objectType:对象类型(sim_object_shape_type,sim_object_joint_type等,或sim_handle_all

objectCount:指向将接收已检索句柄数的值的指针

objectHandles:指向将接收对象句柄数组的指针的指针。在调用下一个远程API函数之前,该数组仍然有效。 operationMode:远程API函数操作模式。此功能的推荐操作模式是simx_opmode_oneshot_wait

以下是我导入它的方式(simxGetObjects函数):

[DllImport("remoteApi.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int simxGetObjects(int clientID, string objectType, IntPtr objectCount, ref IntPtr objectHandles, string operationMode);

以下是我如何称呼它:

int intClientID = simxStart("127.0.0.1", 19999, true, true, 5000, 5);
IntPtr intptrObjectCount = IntPtr.Zero;
IntPtr intptrObjectHandles = IntPtr.Zero;
simxGetObjects(intClientID, "sim_handle_all", intptrObjectCount, ref intptrObjectHandles, "simx_opmode_oneshot_wait");

它没有显示任何错误,但是intptrObjectCount和intptrObjectHandles变量都为零。

如果有人能帮助我,我真的很感激。

1 个答案:

答案 0 :(得分:1)

试试这个:

[DllImport("remoteApi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int simxGetObjects(
    int clientID, 
    int objectType, 
    out int objectCount, 
    out IntPtr objectHandles, 
    int operationMode
);

int objectCount;
IntPtr objectHandles;

int result = simxGetObjects( clientID, 
                             objectType, 
                         out objectCount, 
                         out objectHandles, 
                             operationMode );
if( result == 0 && objectHandles != IntPtr.Zero )
{
    for( int index = 0; index < objectCount; index++ )
    {
        IntPtr handle = (IntPtr)((int)objectHandles + index*4);

        // do something with handle            
    }
}