1

I am currently translating C header to delphi. Is there any function in delphi which can replace C language's vsnprintf? or is it present in any common DLL?

int vsnprintf(char *str, size_t size, const char *format, va_list ap);

Ramnish
  • 322
  • 3
  • 12

5 Answers5

2

Your question is almost answered at Delphi "array of const" to "varargs"

Community
  • 1
  • 1
da-soft
  • 7,670
  • 28
  • 36
2

The closest equivalence function exists in Delphi, it's FormatStr. The prototype is:

function Format ( Const Formatting : string; Const Data : array of const ) : string;
function Format ( Const Formatting : string; Const Data : array of const; FormatSettings : TFormatSettings ) : string;

It takes the format control string, list of values to format, and returns the formatted string.

mbaitoff
  • 8,831
  • 4
  • 24
  • 32
0

vsnprintf() is implemented in a C compiler's runtime library implementation. It does not exist in a DLL. There is no equivilent in Delphi. Whatever you are translating will have to be re-written to use Delphi's own functionality. Please show what you are translating.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • "It does not exist in a DLL". Well, it exists in DLLs for C runtimes that are linked dynamically. Most notably in msvcrt.dll which is a Windows system component. – David Heffernan Jan 20 '11 at 08:02
0

You can link against msvcrt.dll to get C runtime functions. This is a Windows component and not part of the Visual Studio runtime. Thus it can be relied upon to be present.

If you do so be careful if you happen to use a function that does heap allocation. If you need to do that then you will need to free that memory with the same memory manager. One trick is to use malloc from msvcrt.dll to replace the Delphi memory manager.

I'm not saying this is the best way to solve your current problem. In the long run you may be better off with a native Delphi solution that is portable when Delphi supports platforms other than Windows.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Windows provides wvnsprintf. It's an API function, not a C runtime function. The documentation urges you to use one of the "safe" string functions instead, such as StringCbVPrintf; wvsnprintf doesn't guarantee null-termination of the result.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Delphi does not directly support the `va_list` type, though. That is a C type that provides access to stack-based parameters so that `vsnprintf`-style functions can walk the parameters using ordinary pointer arithmetic. It is not possible to implement a function in Delphi that accepts a `varargs` parameter, only to consume such functions from DLLs, so there is no variadic stack available to populate a manual `va_list` pointer when calling a `va_list`-based function... – Remy Lebeau Jan 22 '11 at 10:23
  • ...The best you could do is implement a normal function that uses the `stdcall` convention and a fixed parameter list, then get a pointer to the desired parameter and pass that to the `vsnprintf`-style function. – Remy Lebeau Jan 22 '11 at 10:23
  • @Remy, the `va_list` type is simply a pointer to the values. You don't need to be able to implement varargs functions in Delphi to create a `va_list` structure. If all your values are the same type, you can use an array. Otherwise, define a record. Then pass the function a pointer to the first element. – Rob Kennedy Jan 23 '11 at 00:44