CString
See Also: Type Conversion Functions, String Functions, CStringLength, CStringSize
Purpose
The CString function translates a zero-terminated C-string to a standard DataFlex string.
Return Type
Syntax
CString({Var})
Where:
- {Var} can be any variable, but not an expression or a constant value.
Example
Procedure DoSomething
String sFileName
Move (CString(sFileName)) to sFileName
End_Procedure
Notes
If a DataFlex program calls a DLL (to make a Windows API call, for example) and the function addressed returns a string, there are two possibilities: either the calling program (DataFlex) must pass a pointer to preserved space in memory at which the DLL writes the result, or the called program (the DLL function) returns a pointer to memory in which the value is stored.
See External_Function for more information on calling DLLs from DataFlex.
In languages like C, which are used to create DLLs, string types are different than those in DataFlex. A string in such languages is an array of characters that is terminated with a zero (0) character to indicate the end of the string.
If your function call returns the data in the memory space of a DataFlex string variable, you must remove the bytes after the terminating zero.
The CString function translates a zero-terminated C-style string to a standard DataFlex string.
Example
ZeroString 20 To sResult
Move (SomeAPIFunction(AddressOf(sResult))) To iRetval
If (iRetval) Begin
Move (CString(sResult)) To sResult
End
Example
Define MAX_COMPUTERNAME_LENGTH For 15
External_Function WinAPI_GetComputerName "GetComputerNameA" Kernel32.Dll ;
Pointer lpComputerName DWord nNameSize ;
Returns Integer
Function GetComputerName Desktop Returns String
String sComputerName
Dword dwNameSize
Integer iRetval
ZeroString MAX_COMPUTERNAME_LENGTH To sComputerName
Move MAX_COMPUTERNAME_LENGTH To dwNameSize
Move (WinAPI_GetComputerName(AddressOf(sComputerName), AddressOf(dwNameSize))) To iRetval
Function_Return (CString(sComputerName))
End_Function