[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

CLR and CreateFile w/ FILE_FLAG_DELETE_ON_CLOSE

knattetjatte@googlemail.com

8/29/2008 2:33:00 PM

Hi Group,

I have a program which loads a dll and executes a method in it. The
library it is loaded from is marked with delete-on-close. This worked
fine.

Then I introduced a .NET mixed-mode dll with the same interface, and
then it does not work anymore. The CLR throws an FileLoadException
upon the invocation of the method complaining about a file being used
by another process.

...anyone know of any information about this or even better, a
workaround?

Here is the complete code to reproduce it:

native.cpp (compile: cl /LD native.cpp):
extern "C" _declspec(dllexport) void green_version() { }

il.cil (compile: ilasm /dll /output=meep.dll il.cil):
..assembly meep {}
..method public static void green_version() cil managed
{
.export [1]
ret
}

main.cpp (compile: cl main.cpp):
#include <windows.h>
#include <stdio.h>
void load_and_run(char *dll)
{
HANDLE h = CreateFile(dll, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
if (h == INVALID_HANDLE_VALUE)
exit(1);

HANDLE foo = LoadLibrary(dll);
if (!foo)
exit(3);

typedef void (WINAPI *funcs_t)(void);
funcs_t func = (funcs_t)GetProcAddress((HMODULE)foo,
"green_version");
func();
}

int main(int argc, char **argv)
{
if (!LoadLibrary("mscoree.dll"))
exit(1);
// works fine:
load_and_run("native.dll");
// does not work
load_and_run("meep");

return 0;
}

Thanks to anyone who read all the way down to here :)

/gk