Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static linking in .net impossible? Can you write a wrapper in c++/CLI?

I am working on a VB.Net project in which I need to load Nvidia's API NvApi.lib. However on the Nvidia website it says:

"NvAPI cannot be dynamically linked to applications. You must create a static link to the library and then call NvAPI_Initialize(), which loads nvapi.dll dynamically."

My understanding is that .Net does not support static linking is there a way to wrap the NvApi.lib file so that I can call it from Visual Basic? P.S. I have seen a project here called NvApi.net which leads me to believe that it is possible but that project seems incomplete and was abandoned in 2009 and the features I need were added to the API in 2010.

EDIT:

I was able to get it working. I added a new visual c++ CLR class project to my solution. After linking the nvapi.lib file as a dependence and adding the nvapi.h file to the project I was able to write a small wrapper for the methods I needed. Below is the code I used. It just allows me to turn on and off the 3D stereo.

#include "nvapi.h"

public ref class NvApiWrapper
{   
public: 
static bool NvApiWrapper_Initialize(){
    if (NvAPI_Initialize() == 0){
        return true;
    } else {
        return false;
    }
}

static bool NvApiWrapper_Stereo_Enable(){
    if (NvAPI_Stereo_Enable() == 0){
        return true;
    } else {
        return false;
    }
}

static bool NvApiWrapper_Stereo_Disable(){
    if (NvAPI_Stereo_Disable() == 0){
        return true;
    } else {
        return false;
    }
}
};
like image 618
Alexander Van Atta Avatar asked Oct 06 '22 21:10

Alexander Van Atta


1 Answers

You would need to create a c++/cli wrapper which is statically linked to the .lib and then exposes .net classes etc. This is exactly where c++/cli comes in the most handy.

This tutorial has a good walkthrough (based on the older managed c++ syntax but the concepts are the same)

like image 168
dkackman Avatar answered Oct 10 '22 02:10

dkackman