0

I have a .vcxproj with a C file that does the following:

extern long __stdcall CallCreateTube(long **Data)
{
    char DllPath[256];
    FARPROC pfn;
    long rtn;
    long *pData;

    pData = *Data;

    lstrcpy(DllName, progPath);
    lstrcat(DllName, "XEQ\\tube");
    lstrcat(DllName, ".DLL");

    HANDLE hLib = LoadLibrary(DllPath); // believe me here that DllPath is constructed properly.
    if (!hLib) return -1;

    pfn = GetProcAddress((HMODULE)hLib, "CreateTube");
    if (!pfn) return -1;

    rtn = (pfn) (pData); // this will load tube.dll again!

    if(!FreeLibrary((HMODULE)hLib))
        return -1;
    return rtn;
}

Using Process Explorer, when I do the call to LoadLibrary, "tube.dll" is loaded. When I call the line rtn = (pfn)(pData) above, it loads "tube.dll" again!!

This dll (tube.dll) is a mixed assembly (C++/CLI) compiled with /clr. Is it possible that the second instance of "tube.dll" is loaded in the clr context? If so, how to prevent that? The DLL needs to be loaded once in order to use GetProcAddress, but not loaded again when I call the function!

ryrich
  • 2,164
  • 16
  • 24
  • 1
    Use `HMODULE hLib = LoadLibrary(...)` and then you won't need a cast later. – Ben Voigt Jan 13 '14 at 17:02
  • Sure, the CLR needs to read the file to obtain the metadata and the MSIL to generate the code for the managed code. The only way to unload an assembly is to unload the AppDomain. Since your native code is completely unaware that managed code even exists, you have no way to do this. You need to [host the CLR yourself](http://msdn.microsoft.com/en-us/library/dd380850%28v=vs.110%29.aspx). Or just not bother. – Hans Passant Jan 13 '14 at 17:29
  • @HansPassant: Thanks, what ends up happening here is, I do a `LoadLibrary` again at some point to load a new instance of this `tube.dll`. However, since this dll is loaded twice, it seems to mess up and use old data. Would it help if I change the project the above code is in to be compiled with `/clr`? Since you indicate native code is unaware of managed code, what if I change it to be a mixed assembly? (C++/CLI) – ryrich Jan 13 '14 at 21:59

0 Answers0