# How can I use a DPI C function within a class

**URL:** <https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564>\
**Category:** SystemVerilog\
**Tags:** import, SystemVerilog, System-Verilog, DPI, imported-c-function\
**Created:** [March 2, 2016, 8:13am UTC](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564 "2016-03-02T08:13:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RenéPi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@RenéPi](https://verificationacademy.com/forums/u/Ren%C3%A9Pi)\
**Post date:** [March 2, 2016, 8:13am UTC](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/1 "2016-03-02T08:13:41Z")

</div>

Hello everyone,

I’m using System Verilog now just since 2 months.

I would like to use a C function inside a class.

Can anyone please explain why it’s just allowed to use inside e.g. modules.

Is there any way to use a C function inside a class or how to use it inside a class if I Import the DPI outside a class?

Many thanks in advance,  
René

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 2, 2016, 3:04pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/2 "2016-03-02T15:04:51Z")

</div>

_In reply to [RenéPi](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564):_

You can certainly _use_ (or _call_) a C function from a class method, you just can’t import a DPI C function from within a class. You can only import a C function or task from a point where you would be allowed to declare a function or task that is not a method of a class.

The reason for this restriction is that class methods have an implicit **this** argument set with the handle of the class object calling the method. That handle represents the “context” of the method. Non-class functions or tasks have no such context.

So your class method should be able to call a C function just like any other non-class function as long as the function name is visible from the scope where the class is defined.

---

<div class="post-metadata">

**Author:** ![RenéPi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@RenéPi](https://verificationacademy.com/forums/u/Ren%C3%A9Pi)\
**Post date:** [March 3, 2016, 8:55am UTC](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/3 "2016-03-03T08:55:59Z")

</div>

Thanks a lot for the reply Dave!

I have now imported the C function outside the class with:

import “DPI-C” context function CCRC32\_crcbitbybit();

I am calling the C function by calling a function within the System Verilog class

function imp\_func(byte data);

```
 longint crc_ret = 0;

$display("Importing C Function");
crc_ret = CCRC32_crcbitbybit();
$display("Return Vaule of CCRC32: %h",crc_ret);

```

endfunction

I have another question regarding datatypes between SV and C:

The CRC C function uses an **unsigned char** \* which compares to be a **string** in SV, but I am using a **byte** as Parameter for the SV function.

Is there any possibility to convert a byte to a string in SV?

Thanks a lot,  
René

_In reply to [dave\_59](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/2):_

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 3, 2016, 8:26pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/4 "2016-03-03T20:26:25Z")

</div>

_In reply to [RenéPi](https://verificationacademy.com/forums/t/how-can-i-use-a-dpi-c-function-within-a-class/31564/3):_

You can certainly pass strings through the DPI as well as convert bytes (or an array of bytes) to a string using a cast. Just remember that a string cannot contain the ‘\0’ (null) character.
