# Using AxsunOCTControl.dll in C++

## Background

Calling methods in the .NET-based *AxsunOCTControl.dll* from a managed environment (.NET/C#, MATLAB, LabVIEW, Java, etc.) usually involves a straightforward constructor call.  However, to call methods in *AxsunOCTControl.dll* from an unmanaged or native C++ environment, one needs to perform some additional configuration steps described here.

If your client application integration prefers a "plain-old" C interface or is based on an OS other than Windows, consider the light-weight [AxsunOCTControl\_LW](https://docs.axsun.com/axsun-knowledge-base/api-references/axsunoctcontrol-lightweight) library instead.

## Assembly Registration using *RegAsm.exe*

* If desired, copy *AxsunOCTControl.dll* (and all of its *.dll* dependencies, e.g. *LibUsbDotNet.dll*) to the desired location on your disk, such as your project directory. Alternatively, you can leave the library where it is installed alongside the [OCT Host](https://docs.axsun.com/axsun-knowledge-base/software-tools/oct-host) application or the [Hardware Control Tool](https://docs.axsun.com/axsun-knowledge-base/software-tools/hardware-control-tool) application.
* A Microsoft Windows tool called *RegAsm.exe* registers managed libraries as a COM object in the Windows Registry.  On Windows 7, *RegAsm.exe* is located at the following paths (the version part of the path “\vX.Y.ZZZZZ\” might be different than shown here):
  * **32-bit**:  `C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe`
  * **64-bit:**  `C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe`
* Open a Windows `cmd` prompt **as Administrator** and change the current directory to that of your *AxsunOCTControl.dll* file, for example:

```
cd c:\Program Files (x86)\Axsun\Axsun OCT Control
```

* Execute the `RegAsm.exe` command (noting the correct bitness and version part of the path):

```
c:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe AxsunOCTControl.dll /tlb:AxsunOCTControl.tlb /codebase
```

&#x20;        (or for 64-bit)

```
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe AxsunOCTControl.dll /tlb:AxsunOCTControl.tlb /codebase
```

* This will create the *AxsunOCTControl.tlb* file in the current directory.  Move this *.tlb* file to the same project directory as your *.cpp* source code which will be calling the *AxsunOCTControl.dll* library functions. &#x20;

{% hint style="warning" %}
**WARNING:** Once registered, do **not** move the *AxsunOCTControl.dll* file or its dependencies. Doing so will require you to repeat the RegAsm of the library from its new location.
{% endhint %}

## Importing the *.tlb* file, starting COM, and accessing *.dll* functions in Visual C++

* Import the *AxsunOCTControl.tlb* file using the `#import` directive at the top of your source code:

```cpp
#import “AxsunOCTControl.tlb”
```

* Use the `AxsunOCTControl` namespace:

```cpp
using namespace AxsunOCTControl;
```

* Wrap the segment of your code that contains calls to *AxsunOCTControl.dll* with calls to initialize and uninitialize COM.  For example, initialize COM at the beginning of your `main()` function and uninitialize it just before completing your `main()` function:

```cpp
main () {
CoInitialize(NULL);		// init COM
	// AxsunOCTControl.dll calls go here
CoUninitialize(); 		// un-init COM
}
```

* Create a pointer to an AxsunOCTControl structure:

```cpp
IAxsunOCTControlPtr pAxsunOCTControl(__uuidof(struct AxsunOCTControl));
```

* Dereference this pointer to access the [API functions](https://docs.axsun.com/axsun-knowledge-base/api-references/axsunoctcontrol.dll) available in the AxsunOCTControl structure:

```cpp
long numDevices = pAxsunOCTControl->GetNumberOfOCTDevicesPresent();
```

## Example C++ Source Code

For simplicity, no error checking is shown in this code.

```cpp
#include <iostream>
#import "AxsunOCTControl.tlb"			// import the assembly’s .tlb file
using namespace AxsunOCTControl;

int main() {

    // initialize COM
    CoInitialize(NULL);

    // create pointer
    IAxsunOCTControlPtr pAxsunOCTControl(__uuidof(struct AxsunOCTControl));

    // check how many OCT devices are connected
    long numDevices = pAxsunOCTControl->GetNumberOfOCTDevicesPresent();

    // report status
    std::cout << "num of devices is " << numDevices << '\n';
	
	// call additional functions in pAxsunOCTControl 
	// here as needed for your application workflow
	
	// uninitialize COM
    CoUninitialize();

    return 0;
}
```
