calling native function pointer in c++/cli

I have a native function inside a managed (c++/cli) mixed-mode library that calls a seperate native dll.

Here is the native function that initializes some function pointers using windows GetProcAdress:

//in header
static HMODULE hDll;
static void (*MYDLL_Func1)(void*);
static void (*MYDLL_Func2)(void);

int BeginDll()
{
    if (!hDll) {
        hDll= LoadLibraryHelper("mydll.dll");
        if (hDll) {
            MYDLL_Func1 = (LPVOID)GetProcAddress(hDll, "MYDLL_Func1");
            MYDLL_Func2 = (LPVOID)GetProcAddress(hDll, "MYDLL_Func2");

            if (!MYDLL_Func1||
                !MYDLL_Func2)
            {
                FreeLibrary(hDll); hDll= NULL;
            } else {
                Log("were good to go");
            }
        }
    }
    return (hDll!= NULL);
}

now I want to write a wrapper function that will call this function pointer, but in doing so I get an "AccessViolationException"

public ref struct Wrapper sealed abstract
{
    void ManagedFunc(IntPtr hwnd)
    {
        if (BeginDll())
        {
            MYDLL_Func1(reinterpret_cast<HWND>(hwnd.ToPointer));
            MYDLL_Func2();
        }
    }
}

I am not assuming I can call a function pointer in c++/cli code, I am simply wondering a correct way to do this. If I add the api dll's header file and call the functions implicitly, I run into the problem that if the native helper dll is not in the directory with the managed dll the application bombs after the first call. ("AccessViolationException")

1
задан Anton K 6 September 2014 в 20:23
поделиться