1

this is my first time posting (after lurking for years).

A project I will be tackling is to use NetCDF4 (.nc) files in Unity on Windows. I will be using Unity 5.4.0f3 and Windows 10, and I have developed in Unity before and am familiar with C# and JavaScript, but NetCDF only has C, Java, and Fortran APIs, although there are wrappers in Python, C++, and others (source: https://www.unidata.ucar.edu/publications/factsheets/current/factsheet_netcdf.pdf).

So my specific question is how do I call NetCDF4-C functions (nc_get_vara_float(), nc_open, etc) in C# for use in Unity?

What I've tried so far:

To start, I googled specifically for NetCDF4-C + Unity tutorials/attempts, but did not find anything, so instead I have been looking into the compatibility of calling C functions from C#. I am currently working on a project on Linux with NetCDF4-C and written custom wrapper functions for the netcdf.h functions, so I was hoping I could reuse my code there.

I attempted to follow this SO post (Is it possible to call a C function from C#.Net) but get an error in Unity when trying to Play: "DllNotFoundException: test.so" (my file was named "test.c"). From the comments, it seems Linux uses .so files but Windows uses .dll, and I was not sure how to generate a .dll of a C file.

I looked up another post on that (How to write a DLL file in C?) and downloaded Visual Studio to follow along. While VS was downloading, I looked up how to use GCC to compile (Creating a DLL in GCC or Cygwin?), and used the Bash subsystem ("Bash on Ubuntu on Windows" terminal) but got a handful of errors that indicated the code from the previous link (2nd SO link in this post) were for C++, so I stopped working with GCC.

Once VS finished installing, I went back to trying to use VS to create the .dll, and attempted to combine the solutions from both SO posts (1 and 2) so that I would be able to use the .dll file containing C code in Unity, but to no avail: I get the same error but just with a different extension (and different name on purpose): "DllNotFoundException: Win32Project1.dll".

The code I have is as follows:

test.cs (used in Unity and attaches to a Component):

using UnityEngine;
using System.Collections;

using System.Runtime.InteropServices;

public class test : MonoBehaviour {

    [DllImport("Win32Project1.dll", EntryPoint="DisplayHelloFromMyDLL")]

    public static extern void DisplayHelloFromMyDLL ();

    // Use this for initialization
    void Start () {
        DisplayHelloFromMyDLL();
    }

    // Update is called once per frame
    void Update () {

    }
}

Win32Project1.dll (created and built in Visual Studio):

#include <stdio.h>
extern "C"
{
    __declspec(dllexport) void DisplayHelloFromMyDLL()
    {
        printf("Hello DLL.\n");
    }
}
  • The code shown is not C, but C++. That's not related to the C language. `extern "C"` does **not** introduce C code, but only C ABI. Said that: what is your **specific** problem? If you are looking for a tutorial, this is the wrong site. Read [ask]. – too honest for this site Jun 22 '17 at 21:32
  • I am aware that the code is C++ for the .dll. When I looked up how to call C functions in C#, the SO posts mentioned to use .so on Linux or .dll on Windows. When looking up how to generate a .dll for C functions, the other SO post provided C++ code that wraps around C code. My specific question is how do I call NetCDF4-C functions in C# (nc_get_vara_float(), nc_open, etc) for use in Unity? – Uncertain-Heaven Jun 22 '17 at 21:44
  • Calling C functions does not justify the C tag. Otherwise every question would have to be tagged C, because at some level all software uses C functions (syscalls). And "provided C++ code that wraps around C code" **is not possible**, see my comment above. – too honest for this site Jun 22 '17 at 21:53
  • Thank you for the edit and clarification. I will put my specific question in the post as well. – Uncertain-Heaven Jun 22 '17 at 22:14

0 Answers0