Converting a cdecl function call from C to Pascal, which uses a callback with variable argument lists

I am converting a C header file for a video encoder DLL into Delphi Pascal.

I am having problems with access violations with the following function and you need help in resolving this issue:

h264venc_tt * MC_EXPORT_API h264OutVideoNew(
  void * (MC_EXPORT_API * get_rc)(const char* name),
  const struct h264_v_settings * set,
  int32_t options,
  int32_t CPU,
  int32_t frame0,
  int32_t nframes);

Note: MC_EXPORT_API = cdecl

get_rc is declared as follows:

// resource functions dispatcher
void * MC_EXPORT_API get_rc(const char* name)
{
  if (!strcmp(name, "err_printf"))
    return (void*) error_printf;
  else if (!strcmp(name, "prg_printf"))
    return (void*) progress_printf;
  else if (!strcmp(name, "wrn_printf"))
    return (void*) warn_printf;
  else if (!strcmp(name, "inf_printf"))
    return (void*) info_printf;
  return NULL;
}

This function returns a pointer to another function with a variable argument. One of them is declared as follows:

void error_printf(const char * fmt, ...)
{
  char lst[256];
  va_list marker;
  va_start(marker,fmt);
  vsprintf(lst,fmt,marker);
  va_end(marker);
  printf("%s\n", lst);
}

I translated this function call and get_rc into this Delphi Pascal code:

PErrorMessageHandler = function (const Name: String): Pointer; cdecl varargs;

function h264OutVideoNew(
  get_rc: PErrorMessageHandler;
  settings: Ph264_v_settings;
  options: int32;
  CPU: int32;
  frame0: int32;
  nFrames: int32
): Pointer; cdecl; external 'mc_enc_avc.dll' index 4;

, , C-style error_printf, . - ? , , , h264OutVideoNew().

PS! Th264_v_settings/P_h264_v_settings , .

+5
1

C char* 8 . Delphi PAnsiChar. string, Delphi, C.

, . .

, , , C, Delphi. ​​, . , .

, . , . C, .obj, Delphi $LINK.

, :

TErrorMessageHandler = procedure(Name: PAnsiChar); cdecl;

, :

  • T, .
  • Name.
  • C.
  • varargs, Delphi , , .

:

function h264OutVideoNew(
  get_rc: TErrorMessageHandler;
  settings: Ph264_v_settings;
  options: int32;
  CPU: int32;
  frame0: int32;
  nFrames: int32
): Pointer; cdecl; external 'mc_enc_avc.dll' index 4;

:

procedure error_printf(Name: PAnsiChar); cdecl;
begin
  // do stuff here
end;
+6

All Articles