how to use a variable in parameters - string

i need to know how to use a variable in parameters
PROCESS is initalized as
string PROCESS in the class parameters.
Process::GetProcessesByName("")
which gives me an error when i replace the string with a variable
Process::GetProcessesByName(PROCESS)
The error i get back is:
No instance of overloaded function "System::Diagnostics::Process::GetProcessByName" Matches the argument list argument types are (std::string)

I think GetProcessesByName may take a parameter as LPSTR or char const *, but not a std::string,
try use
Process::GetProcessesByName(PROCESS.c_str());
or if your PROCESS is defined as std::string PROCESS;,
String^ process_name = gcnew String(PROCESS.c_str());
Process::GetProcessesByName(process_name);

Related

int execle(const char *path, const char *arg, ..., char * const envp[]);

int execle(const char *path, const char *arg, ..., char * const envp[]);
In the prototype of function above used in Linux, after variable arguments, they used named variable which is not possible in C language.
Because in C language in variadic function after variable argument we can not pass named argument then why in function given above they used named argument after variable argument (...).
The final argument is technically part of the variable argument list (...). It is just shown to provide the type and a name for the final argument, to make the documentation easier to understand. The current version of the manual page shows this argument in a comment to match the actual C prototype.

Is it possible to define a CAPL function returning a text string?

I develop CAPL scripts in Vector CANoe, and I need to define several functions returning text strings. In C, I would write something like this:
char * ErrorCodeToMsg(int code)
or
char [] ErrorCodeToMsg(int code)
In CAPL, both definitions fail with a parse error. The only working solution I came up with so far is:
variables {
char retval[256];
}
void ErrorCodeToMsg(int code) {
char [] msg = "Hello word";
strncpy(retval, msg, 256);
}
Of course this is very ugly, because each call to ErrorCodeToMsg requires two statements instead of one. Is there a better way?
You have to do it as you would do with string-based functions :
void ErrorCodeToMsg(char buffer[], int code){
buffer = myListOfCodes[code];
}
The value will be stored in the buffer using its reference value. It is not possible to return string in Capl. This is why you can't access String System variables using the # Selector.
I have implemented a workaround for functions which return string constants. It consists in defining an array of possible return values char errorMsg[][] and defining a function int ErrorCodeToMsg(errno) which is returns and index in that array, so it is called like this:
write("Error: %s", errorMsg[ErrorCodeToMsg(errno)]);
Note that this method is error-prone when coded manually, because it's easy to get the function and the array out of sync after a modification. In my case, error codes are defined in a specification (XML file), so that array of error messages and the ErrorCodeToMsg function are automatically generated.

swift println float using string

I wish to ask a conceptual question. My code is to print an array of float values of 5 decimal places onto the console. Why must it be String instead of Float? Ans[y] is an array of type float.
println(String(format: "%.5f", Ans[y]))
Instead of Float
println(Float(format: "%.5f", Ans[y]))
Float gives an error of extra argument 'format' in call
You can use map() to format your Float array as string array. Btw you should give it a name starting with a lowercase letter. Try doing as follow:
let floatArray:[Float] = [1.23456,3.21098,2.78901]
let formattedArray = floatArray.map{String(format: "%.5f", $0)}
println(formattedArray) // "[1.23456, 3.21098, 2.78901]"
It's just a matter of understanding what your words mean. String is an object type (a struct). Float is an object type (a struct). The syntax Thing(...) calls a Thing initializer - it creates a new object of type Thing and calls an initializer method init(...). That's what you're doing when you say String(...) and Float(...).
Well, there is a String init(format:) initializer (it actually comes from Foundation NSString, to which String is bridged), but there is no Float init(format:) initializer - the Float struct doesn't declare any such thing. So in the second code you're calling a non-existent method.
You can use NSLog instead of println. NSLog is still in the foundation class and has the flexibility of specifying the exact format you need.

What is the correct XDR syntax for a variable-length array of strings?

I am reading the RFC 4506 to understand the XDR data definition language.
It mentions that variable-length arrays are declared as follows.
type-name identifier<m>;
It also mentions that variable-length strings are declared as follows.
string object<m>;
Unfortunately, the only way it shows to have a variable length array of strings is a linked list, which seems very manual.
struct *stringlist {
string item<>;
stringlist next;
};
Is there a more simple or more correct way to declare a variable-length array of strings?
You can use the typedef keyword.
typedef does not declare any data either, but serves to define new
identifiers for declaring data. The syntax is:
typedef declaration;
The new type name is actually the variable name in the declaration
part of the typedef. For example, the following defines a new type
called "eggbox" using an existing type called "egg":
typedef egg eggbox[DOZEN];
We can define a variableLengthString type with
typedef string variableLengthString<>;
and then declare a variableLengthString array with
variableLengthString object<>;

How does one pass a string back to labview using a call Library function node

I want to use LabVIEW's Call Library Function Node to access a DLL function, and have this function return a string to displayed on my VI. How would I go about doing this? I am quite happy returning numbers from my DLL, but am really struggling to find any examples of how to return a string.
There are at least a few ways to return a string from a Call Library Function Node:
Return a C string pointer from your DLL function, and configure the Call Library Function Node to have a return type of "C String Pointer". Note that the returned string must be valid after the function returns, so it can't be a pointer to a string allocated on the stack. It must be one of the following: allocated on the heap, statically allocated, a constant string literal, etc.
It looks like examples/dll/regexpr/Regular Expression Solution/VIs/Get Error String.vi in the LabVIEW directory takes this approach.
Allocate a string in your VI, pass it to the Call Library Function Node using a "C String Pointer" parameter as Azim suggested, and overwrite its contents in the DLL. One way to allocate the string is to use Initialize Array to create a u8 array of the desired size, and then use Byte Array To String to convert it to a string.
Be sure that the string you pass in is large enough to hold the contents of your string, and make sure to pass the string length to the DLL so that it knows how large the buffer is. I believe that the default parameter is an empty string. Figuring out the right string length may require calling into the DLL twice, if your VI's first guess isn't large enough.
Pass the string to the Call Library Function Node using a "String Handle" parameter, and use LabVIEW functions in your DLL to resize the string as necessary. This requires your DLL to be specifically designed to interface with LabVIEW and requires linking against a static library that is provided with LabVIEW.
An example of this method ships with LabVIEW as examples/dll/hostname/hostname.vi.
I assume from your question that you already have a DLL that can return numbers to Labview. To return a string from the DLL, I have created a DLL with the following C++ function
void returnString(char myString[])
{
const char *aString = "test string";
memcpy(myString, aString, 12);
}
In Labview I then use the Call Library Function Node and configure it as follows
Library Name or Path: c:\path\to\my\custom.dll
Function Name: returnString
Calling Convention: C
Parameters:
Parameter: return type
type: void
Parameter: arg1
type: String
string format: C String Pointer
Function prototype:
void returnString(CStr arg1);
After connect the arg1 output in the block diagram to a string indicator and run. The string "test string" should appear in the front panel.
I tried to have the returnString function be of type CStr as in
CStr returnString()
{ ...
}
but I got build errors when compiling the DLL project.
Update
Thanks to #bk1e comment don't forget to pre-allocate space in Labview for the string.

Resources