0

I am trying to DLLImport the function simxGetObjects from remoteApi.dll of v-rep software. Here is the link to the function description: http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctions.htm#simxGetObjects

and here is the brief description for this function from the above link:

Description: Retrieves object handles of a given type, or of all types (i.e. all object handles)

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

C parameters:
clientID: the client ID. refer to simxStart.

objectType: object type (sim_object_shape_type, sim_object_joint_type, etc., or sim_handle_all for any type of object

objectCount: pointer to a value that will receive the number of retrieved handles

objectHandles: pointer to a pointer that will receive an object handle array. The array remains valid until next remote API function is called. operationMode: a remote API function operation mode. Recommended operation mode for this function is simx_opmode_oneshot_wait

Here is the way I am importing it (simxGetObjects function):

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

and here is how I am calling it:

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");

It does not show any error, however both intptrObjectCount and intptrObjectHandles variables are zero.

I really appreciate if someone can help me on this.

4
  • What is the return value of simxGetObjects? Commented Jul 21, 2015 at 19:47
  • The return value is a flag showing the status of the function whether it was successful or not. However, the actual outputs are objectCount and objectHandles that I need to obtain. It basically calls the software that is running in the background and retrieve the number and handle of all of the objects that are in the scene. Commented Jul 21, 2015 at 19:53
  • The return value (I mean value itself) might help to understand what you are doing wrong. Commented Jul 21, 2015 at 19:56
  • It is 0 which means it runs ok. Sorry I didn't get what you meant. Commented Jul 21, 2015 at 20:00

1 Answer 1

1

Try this:

[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            
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. The reason I am using string is that based on the description sim_handle_all is the value needs to be used to get all of the objects. So I pass sim_handle_all as string to the function. I can not find the int value for it in the documents.
@NESHOM: Here is C synopsys: "simxInt simxGetObjects(simxInt clientID,simxInt objectType,simxInt* objectCount,simxInt** objectHandles,simxInt operationMode)". objectType and operationMode are just enums. String shall be removed!
Thanks for this, but the issue I have now, I think, is all about objectHandles that is a pointer to a pointer that will receive an object handle array. How can I marshal this and get access to the array items?
@NESHOM: I updated my answer. I believe this is how you should access received array of handles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.