Skip to main content
Solved

Null reference after creating standalone application inside WinForm project


nikitasatcik
Forum|alt.badge.img

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

Best answer by MichaelH

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.

View original
Did this topic help you find an answer to your question?

2 replies

MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 354 replies
  • Answer
  • June 6, 2023

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.


nikitasatcik
Forum|alt.badge.img
  • Author
  • Monochrome
  • 4 replies
  • June 7, 2023

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings