If you use the DllImport mechanism
[DllImport("DllTeste.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern IntPtr TesteEnviaXml();
to create your access to the functions in DllTeste.dll, you cannot use FreeLibrary to unload that DLL. FreeLibrary requires the
handle returned when the DLL is loaded, but you don't have that available to you. The .NET infrastructure has loaded the DLL for you when the DllImport code is executed at program start.
If you want to use FreeLibrary so that you can manipulate the DLL while the program is still running, you have to load it manually using LoadLibrary and then use GetProcAddress to get access to your function. After using it, you can then call FreeLibrary and rename the DLL.
(error checking removed for brevity)
IntPtr dllHandle = LoadLibrary ("DllTeste.dll");
IntPtr funcPtr = GetProcAddress (dllHandle, "TesteEnviaXml");
delegate IntPtr dlgMethod ();
dlgMethod dlg = (dlgMethod) Marshal.GetDelegateForFunctionPointer (funcPtr, typeof (IntPtr));
IntPtr ret = dlg ();
FreeLibrary (dllHandle);