openvr icon indicating copy to clipboard operation
openvr copied to clipboard

Is there any example of c #

Open xiaoyang-git opened this issue 10 years ago • 6 comments

Is there any example of C# to reference the openvr dll ? or some other methods to resovle

xiaoyang-git avatar Nov 17 '16 02:11 xiaoyang-git

There are multiple ways of doing so, but personally I use an IDE and do it this way:

  1. create separate project OpenVRApiModule (DLL type),
  2. enter that project root directory,
  3. execute shell command git clone https://github.com/ValveSoftware/openvr.git,
  4. add existing file {project root}/openvr/headers/openvr_api.cs to the project,
  5. add existing file {project root}/openvr/bin/win32/openvr_api.dll to the project (you can use other builds if needed),
  6. set property in the project for openvr_api.dll to always copy to output directory,
  7. add OpenVRApiModule project the workspace where you want to use it (if you didn't do it initially).
  8. add OpenVRApiModule project as a reference to projects that use OpenVR.

As a result you will get all OpenVR API available with type hinting in your IDE. When you will build your project OpenVRApiModule will also be built as a dependency into OpenVRApiModule.dll and copied along with openvr_api.dll to your output directory. As a result you should get your program working right away without any need of post-build steps.

It also makes easy to update used api to newest version. Just use shell command git pull from within {project root}/openvr directory when needed. All projects will use the newer version right away.

Otherwise you could do similarily and clone OpenVR repository into an already existing project and add openvr_api.cs and openvr_api.dll the same way as I add them to OpenVRApiModule. This will give you same result except that you will have two extra files in your project tree and when you will want to update OpenVR API to a newer version you will have to do it in all your projects.

destrofer avatar Nov 17 '16 16:11 destrofer

Thank you very much, it works.

xiaoyang-git avatar Nov 19 '16 08:11 xiaoyang-git

Thanks. I copied this here as well: https://steamcommunity.com/app/358720/discussions/0/142260895142733091/

aleiby avatar Jan 06 '17 03:01 aleiby

Update

Had to do a couple things to get everything working.

  1. Create a new Project and choose "Class Library (.NET Framework)" type Visual C#
  2. Delete the Class1.cs file, because it is unneeded.
  3. Move the files as instructed in steps 4 and 5.

Common Errors

  • "The OutputPath property is not set for this project"
    • Fix: https://stackoverflow.com/a/31703751/1762224
  • "Source file settings.designer.cs could not be found"
    • Fix: https://stackoverflow.com/a/25569975/1762224

rmkane avatar Jan 04 '19 05:01 rmkane

When I try these instructions, it works and compiles but when I run the application it says it couldn't find openvr_api.dll (I made sure to copy to output directory). I also tried putting openvr_api.dll in the project folder but it says it couldn't find assembly manifest. Also when I view the compiled assembly in a dependency viewer openvr_api is not found.

jppboi avatar Jan 18 '22 05:01 jppboi

FYI: for error Unable to load DLL 'openvr_api' or one of its dependencies: File not found (maybe #1630?) I noticed the openvr_api.dll has to be at the built root and not in a subfolder (even when debugging the app in Visual Studio). Copying openvr_api.dll into a subfolder in my project resulted in that subfolder also being created in the build output dir. After configuring Visual Studio to copy the dll file to the output root without creating a subfolder, it worked just fine.

dreiekk avatar Jun 09 '22 17:06 dreiekk