How to get type name on systemVerilog

I want to get type name for my_interface as showed :

initial begin 
    
    $display ("%s",(typename(i_top_tb.master_interface))) ; 
    $display ("%s",(typename(i_top_tb.slave_interface))) ; 

  end

Thanks

In reply to uvm_share:

You can use VPI to get the name of the module or interface.
Here is an example you can use and works on questa:

test.sv:

interface if1();
endinterface
interface if2();
endinterface

module top;
import "DPI-C" context task get_if_name(string scope);
  if1 master();
  if2 slave();
  initial begin
    get_if_name("top.master");
    get_if_name("top.slave");
  end
endmodule

test.c:

#include <sv_vpi_user.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
extern int get_if_name(char *scope);
int get_if_name(char *scope) {
  vpiHandle h = vpi_handle_by_name(scope, NULL);
  if(h) {
    printf("If Name for %s is: %s\n", scope, vpi_get_str(vpiDefName, h));
  }
  return 0;
}

Compile and run Makefile:

all: test.so
gcc -c -fPIC -I{QUESTA_SIMULATOR_HOME}/include test.c -o test.o
gcc -fPIC -shared test.o -o test.so
vlog -64 test.sv
vopt -64 top +acc -o top_opt
vsim -64 -c -do “run -all; quit -f” top_opt -sv_lib test

Output:

If Name for top.master is: if1

If Name for top.slave is: if2

In reply to uvm_share:

It would help to show a complete example of what you tried and what you are expecting.

module top_tb ; 
  import "DPI-C" context task get_if_name(string scope);
  
  initial begin 
    get_if_name ( "top_tb.master_interface") ; 
    get_if_name ( "top_tb.slave_interface") ; 
  end 
endmodule

When I try this, i have thios fatal error :
Null foreign function pointer encountered when calling ‘get_if_name’__

what is the solution in this case ?
Thanks in adavance

In reply to uvm_share:

What is the simulator command line you are using?
It works with these commands:
vlog test.sv test.c
vopt top -o top_opt
vsim -c -do “run -all; quit -f” top_opt

In reply to Venkateshwara Rao:

I’m using Questa, and i do these commands but i have always same fatal error

In reply to uvm_share:

It would help to show a complete example of what you tried and what you are expecting to do with the returned strings.

You shouldn’t need VPI for this.