开发者

vsnprintf equivalent in Delphi?

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);


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.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜