Solved

Null reference after creating standalone application inside WinForm project

  • 5 June 2023
  • 2 replies
  • 49 views

Userlevel 1
Badge

Dear all,

I have a WinForm/WPF project where I want to use ZOS-API.

The console application from ray tracing example file works fine. However, when I added ZOS references (ZOSAPI, ZOSAPI_Interfaces, ZOSAPI_Nethelper) to the WinForm project (compiled for x64), I got a null reference, like so:

I’ve made minimum modifications to the original example of the ray tracing program. Basically, I just wrapped it into a separate class and removed the Main method, because the WinForm project already has an entry point.

The program could be succesfully initialized, which means ZOSAPI references linked to the project:

Found OpticStudio at: c:\program files\zemax opticstudio

However, after new connection created, “TheApplication” reference is null:

IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication();

I appreciate it if anyone knows the cause of this null reference.

 

Regards,

Mykyta

icon

Best answer by MichaelH 6 June 2023, 02:06

View original

2 replies

Userlevel 6
Badge +2

Hi Mykyta,

Can you make sure that the ZOSAPI.dll and ZOSAPI_Interfaces.dll that you added in the Reference section of the Solution Explorer is set to Copy Local False (the ZOSAPI_NetHelper.dll should be Copy Local True):

The ZOSAPI_NetHelper.dll is a standalone class (the Connection class) which searches through a series of directories trying to locate OpticStudio and this is why this DLL needs to be in the same directory as your ZOS-API executable.  The ZOSAPI.dll and ZOSAPI_Interfaces.dll have relative paths for the rest of OpticStudio (all the internal libraries, 3rd party libraries, and the OpticStudio.exe itself).  If you have Copy Local True for these DLLs, then your Standalone Application will copy these 2 DLLs to the same directory as your executable and when these DLLs search for the rest of OpticStudio, they return a null reference.  You need to include the ZOSAPI.dll and ZOSAPI_Interfaces.dll so Visual Studio can provide intellisense and can compile, but the Connection class will dynamically load these DLLs from their local directory via reflection when you execute your application.

Userlevel 1
Badge

Hi @MichaelH ,

 

Thank you for the answer. It solved the problem with null reference after I changed to Copy Local False

 

Best Regards,

Mykyta

 

Reply