i have a header file and one of the function prototypes is

extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * ParamValue);

and there is an example of using this code

z=LT360LIB_CmdValue(0, lt_Goto_CCW, "5.5");

when i try this code it compile and builds correctly but when i run the program , it halts with no error messages ( and the processor goes to 100% as if it went to an infinite loop).

could it be due to passing a string to a character pointer although i had no compiler errors?
Thanks

Dani AI

Generated

Short practical summary and troubleshooting notes.

Passing a literal like "5.5" to a parameter declared char * is legal at compile time, but it only works safely when the function treats that pointer as read-only. The runtime hang described here is unlikely to be caused solely by using a string literal. More common causes are: the DLL waiting for the hardware (device not attached), the DLL writing past the end of the caller's buffer, or a mismatch between the header signature and the DLL's actual export (calling convention or integer-size mismatch). As noted, the syntax is fine; as and suggested, provide a proper writable buffer if the call returns data.

A safe, modern caller-side pattern is to allocate writable storage and pass its pointer. Example (uses std::vector so the buffer is contiguous and writable):

const size_t BUF_SZ = 16;
std::vector<char> buf(BUF_SZ);    // reserve space including room for terminating NUL

if (LT360LIB_CmdValue(handle, lt_GetMoving, buf.data())) {
    std::string state(buf.data());  // e.g. "CCW", "CW", or "NO"
    // process state
}

Pointer semantics and common mistakes. The callee receives a copy of the pointer value; writing into the buffer is visible to the caller, but reassigning the pointer inside the callee does not change the caller's pointer (this explains why 's new+reassign test produced no visible result). Do not pass a read-only literal if the function will write into the buffer.

Quick checklist for the original hang:

  • Use a writable buffer large enough for returned strings (check vendor docs).
  • Verify the device is connected; the DLL may block waiting for hardware.
  • Confirm the header exactly matches the DLL export (calling convention, parameter sizes).
  • Check the function return value and run under a debugger to see where the thread is stuck or wrap the call on a thread with a timeout.

These steps align with advice from and the correction from and will isolate whether the problem is a buffer/usage issue or an external device/DLL behavior.

Recommended Answers

All 10 Replies

No.

so its ok to pass a string to a character pointer and ill have to check for other causes , ill see thanks

Well, it does make any difference in assiging the a string to a char pointer. What I really wonder is how is that killing your processor. Is there any loops which goes to an infinite loop? Or there may be some problems with the implemenation of that function may be. What does your application do?

ssharish

im actually controlling a hardware (turntable) ,
actually i just tested this function, i didnt write any other code ,it could be due to something happening in the .dll it calls. But it occured to me maybe this problem is due to not having the device connected to my pc , the function maybe waiting for a feedback from the device, currently the device is not around me i was just testing the codes.ill check again when i connect the device.

by the way one more question, in the documentation they say this header can be used also to return a string in paramValue

//---------------------------------------------------------------------------
//  Function: 	LT360LIB_CmdValue
//  Abstract:   Sends a Command to the LT360
//              Also dislays error message dialog if command fails
//  Parameters: Handle: LT360 Index returned from Link
//              CmdID : Command Identifier
//              ParamValue : Parameter String for Value, may be nil
//                           [B]****Get calls return strings in ParamValue****[/B]
//  Returns:    Returns TRUE if executed successfully
//---------------------------------------------------------------------------
extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * [B]ParamValue[/B]);

by using "lt_GetMoving" instead "lt_Goto_CCW" in the function posted at the beggining. but how can i obtain the string returned in paramvalue, can i just declarea pointer of type e.g char*p, then placing p as the third parameter?

ParamValue must point to a large enough buffer to receive the information. The documentation should provide information wrt 'large enough buffer'.

You might use something like

const int GETMOVING_SIZE 1024;
char paramValue[GETMOVING_SIZE] = {0};

if(LT360LIB_CmdValue(handle, lt_GetMoving, paramValue))
{
    // here you can check the content of paramValue
}

sorry forgot to mention paramvalue is supposed to return (in the lt_GetMoving)
"CCW" or"CW" or "NO"

thanks for the reply, is it possible to check the value of paramvalue after the function call since it is passed by value?

i tried this test code

#include <iostream>
#include <string>
using namespace std;

void test (char *);

void main()
{
const int GETMOVING_SIZE =5;
char paramValue[GETMOVING_SIZE] = {0};
test(paramValue);
cout<<paramValue;

}

void test (char*paramvalue)
{
	paramvalue=new char [5];
	paramvalue="issa";

		
}

but no output came, i think it is due to passing paramvalue by value, which will be same if i tried with the original code since paramvalue is declared as pointer by value(i.e any changes made to paramvalue won't be seen outside the function that used it).
Thanks again

i tried this test code

#include <iostream>
#include <string>
using namespace std;

void test (char *);

void main()
{
const int GETMOVING_SIZE =5;
char paramValue[GETMOVING_SIZE] = {0};
test(paramValue);
cout<<paramValue;

}

void test (char*paramvalue)
{
	paramvalue=new char [5];
	paramvalue="issa";

		
}

but no output came, i think it is due to passing paramvalue by value, which will be same if i tried with the original code since paramvalue is declared as pointer by value(i.e any changes made to paramvalue won't be seen outside the function that used it).
Thanks again

Your test() function is making paramvalue point to something entirely different. That's not the way to go about it.

test() is supposed to expect a buffer passed to it, and it ought to fill the buffer with whatever characters it sees fit. It should look more like:

void test(char *paramvalue) {
	strcpy(paramvalue, "issa");
}

Edit:

In my haste, I overlooked this:

paramvalue=new char [5];
	paramvalue="issa";

That shows you've got the whole idea of pointers, arrays and strings (and probably a few more things that come into play here) totally jumbled up. You should consult a good book.

thanks man i got you , i tried it and it works fine. by the way i messed up big time inthe code above some times i used paramvalue and on other occasions paramValue lol thanks again.

anyway just to make sure when i want to take the returned string from ParamValue,

extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * ParamValue);

i'll have to pass an array of type char to the pointer ParamValue and the data will be passed to the array and can be seen outside the function . i.e ( similar to what mitrmkar said)
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.