Just starting out? Need help? Post your questions and find answers here.
stange
New User
Posts: 2 Joined: Fri Mar 02, 2012 12:39 am
Post
by stange » Fri Mar 02, 2012 12:46 am
Hello!
I have such a code in C++, which I try to convert to PB, but something isn't right:
...
Code: Select all
CPhidgetTemperatureSensorHandle temp = 0;
CPhidgetTemperatureSensor_create(&temp);
CPhidget_open((CPhidgetHandle)temp, -1);
if((result = CPhidget_waitForAttachment((CPhidgetHandle)temp, 2000)))
{
printf("Problem waiting for attachment:\n");
return 0;
}
...
My version in PB is:
Code: Select all
If OpenLibrary(0,"phidget21.dll")
*tmp = AllocateMemory(1024)
*CPhidgetTemperatureSensor_create = GetFunction(0, "CPhidgetTemperatureSensor_create")
If *CPhidgetTemperatureSensor_create
CallFunctionFast(*CPhidgetTemperatureSensor_create, *tmp)
*CPhidget_open = GetFunction(0, "CPhidget_open")
If *CPhidget_open
CallFunctionFast(*CPhidget_open, tmp, -1)
*CPhidget_waitForAttachment = GetFunction(0, "CPhidget_waitForAttachment")
If *CPhidget_waitForAttachment
Result.i = CallFunctionFast(*CPhidget_waitForAttachment, tmp, 10000)
If Result <> 0
MessageRequester("ERROR", Str(Result))
EndIf
EndIf
EndIf
EndIf
CloseLibrary(0)
EndIf
I get result, which tells that invalid argument passed to function (not Problem waiting for attachment). Any idea why?
Thank you.
luis
Addict
Posts: 3895 Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy
Post
by luis » Fri Mar 02, 2012 12:57 am
From the small piece of code you posted I cannot know what kind of data is CPhidgetTemperatureSensorHandle.
A integer (since it's say handle in the name) ? If it is so then I don't understand the memory allocation in PB.
Anyway the first thing I noticed is that *tmp is becoming tmp. Those are TWO different variables in PB.
The error you get is probably because you are passing tmp = 0.
Start enabling EnableExplicit, it will force you to look harder to the vars you are using.
Read the chapter on pointers in the manual (in PB they are a little strange coming from C).
"Have you tried turning it off and on again ?"
stange
New User
Posts: 2 Joined: Fri Mar 02, 2012 12:39 am
Post
by stange » Fri Mar 02, 2012 11:51 am
luis wrote: From the small piece of code you posted I cannot know what kind of data is CPhidgetTemperatureSensorHandle.
A integer (since it's say handle in the name) ? If it is so then I don't understand the memory allocation in PB.
Anyway the first thing I noticed is that *tmp is becoming tmp. Those are TWO different variables in PB.
The error you get is probably because you are passing tmp = 0.
Start enabling EnableExplicit, it will force you to look harder to the vars you are using.
Read the chapter on pointers in the manual (in PB they are a little strange coming from C).
Thank you for your reply, I have already found solution for problem I had. I should pass address to the first function like @tmp.
But maybe you can help me with next one:
C code:
const char *name;
CPhidget_getDeviceName (TEMP, &name);
I am converting like:
Code: Select all
name.s = ""
CallFunction(0, "CPhidget_getDeviceName", ptr1, @name);
PrintN("INFO: Device is attached: " + name$)
But it returns nothing. Any idea how to correct convert that part of code? Thank you.
luis
Addict
Posts: 3895 Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy
Post
by luis » Fri Mar 02, 2012 12:16 pm
My C is a little rusty but ...
In the C code you are passing the address of a pointer.
In the PB code you are passing the address of a PB string with length 0.
Those are two very different things.
Anyway, you should have post at least the specifics of the API function you are calling, to let us know what are the expected params and if the API function is allocating the memory or if it's expecting a buffer to be externally allocated.
Those are only random pieces of codes with random variable names with unknown data type associated to them.
That's not the correct way to ask for help, IMO.
Anyway const char *name and the param &name suggest to me the function allocate the data inside it and change the pointer to point to it (hence the const data attribute). So maybe the correct parameter it's the address of a pb pointer (like @*ptr ).
And in that case *ptr would point to the first char of the buffer on exit (see PeekS() ).
But I'm guessing since I don't know how CPhidget_getDeviceName() is supposed to work.
Am I guessing right ?
Some days later : HELLO ?
"Have you tried turning it off and on again ?"