c – Generation of a DSA key pair with openssl / libcrypto

I have the following code trying to generate a pair of DSA keys.

OpenSSL_add_all_algorithms ();

ctx = EVP_PKEY_CTX_new_id (EVP_PKEY_DSA, NULL); EVP_PKEY_keygen_init (ctx);

if (EVP_PKEY_CTX_set_dsa_paramgen_bits (ctx, 1024) <= 0) ERR_print_errors_fp (stderr);

and I get the following error

3073906944: Error: 06089094: Digital Envelope Routines: EVP_PKEY_CTX_ctrl: Invalid operation: pmeth_lib.c: 398:

A clue about what I'm doing wrong?
Thank you