Using C# to pinvoke Cudart64_75.dll Runtime API functions

Hello, NVIDIA CUDA community. I am trying to write a C# program that uses the CUDA runtime API to find CUDA-friendly GPUs and their information. In particular, I am trying to use the following commands:

  1. cudaGetDeviceCount(int*)

  2. cudaGetDeviceProperties(…, …)

  3. cudaMemGetInfo(…, …)

  4. cudaDeviceReset()

as described here: I’m trying to use these functions through the platform invoke (pinvoke) technique in C# in .NET 2013.

I’m having trouble figuring out the right function signatures/declarations to get the pinvoking to work though.

As an example, I’m trying the following

        private static extern cudaError cudaGetDeviceCount(int* device);

public enum cudaError
     * The API call returned with no errors. In the case of query calls, this
     * can also mean that the operation being queried is complete (see
     * ::cudaEventQuery() and ::cudaStreamQuery()).
    cudaSuccess                           =      0,
     * The device function being invoked (usually via ::cudaLaunchKernel()) was not
     * previously configured via the ::cudaConfigureCall() function.
    cudaErrorMissingConfiguration         =      1,


public void TestFunction()
           int a = 0;
            int* devCount = &a; 
            cudaError rVal = cudaGetDeviceCount(devCount);

And I’m getting a BadImageFormation exception, with the info:

[i]An unhandled exception of type ‘System.BadImageFormatException’ occurred in CsGpuQuery.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)[/i]

Any help would be greatly appreciated. In particular, I’m not sure what “hostdevice” means in front of the function declarations here, and how they relate to pinvoking.

Thanks and regards.

have you taken a look at managedCuda or Cudafy?

txbob, thanks for your help. ManagedCuda is very helpful to look at for the more complicated data type marshaling.

It turns out my initial problem was that the Visual Studio 2013 platform target setting was Any CPU, rather than x64. D’oh! Time to get to work on the more complicated functions now…