A dynamic-link library (*.so) file that calls Linux by P/invoke mode has been implemented under Netcore.
1[DllImport (@"libdl.so.2")]2 Public Static externIntPtr Dlopen (stringFileNameintflags);3[DllImport ("libdl.so.2")]4 Public Static externIntPtr Dlsym (IntPtr handle,stringsymbol);5 6[DllImport ("libdl.so.2", EntryPoint ="Dlopen")]7 Private Static externIntPtr unixloadlibrary (String fileName,intflags);8 9[DllImport ("libdl.so.2", EntryPoint ="Dlclose", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)]Ten Private Static extern intunixfreelibrary (IntPtr handle); One A[DllImport ("libdl.so.2", EntryPoint ="Dlsym", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)] - Private Static externIntPtr unixgetprocaddress (IntPtr handle, String symbol); - the[DllImport ("libdl.so.2", EntryPoint ="Dlerror", CallingConvention = callingconvention.cdecl, CharSet = CharSet.Ansi)]
View Code
Under normal circumstances, it is possible to invoke a successful
If a call fails, it may be that the so file is missing some dependent files and can be viewed through the LDD command
LDD libzmq.so
If some dependent files cannot be found, the words "not found" will appear, such as the following
/usr/lib64/libstdc++.so.6:version ' glibcxx_3.4.20 ' not found (required by */3rd-party/protobuf-2.4.1/src/.libs/ libprotobuf.so.7)
/usr/lib64/libstdc++.so.6:version ' glibcxx_3.4.20 ' not found (required by */3rd-party/protobuf-2.4.1/src/.libs/ libprotoc.so.7)
You can use the string command to find out if a dependency is really missing
Strings/usr/lib64/libstdc++.so.6 |grep glibcxx Get results glibcxx_3.4glibcxx_3.4.1glibcxx_3.4.2glibcxx_3.4.3glibcxx_ 3.4.4glibcxx_3.4.5glibcxx_3.4.6glibcxx_3.4.7glibcxx_3.4.8glibcxx_3.4.9glibcxx_3.4.10glibcxx_3.4.11glibcxx_ 3.4.12glibcxx_3.4.13glibcxx_3.4.14glibcxx_3.4.15glibcxx_3.4.16glibcxx_3.4.17glibcxx_debug_message_length
The file is indeed missing, in which case we need to use the Find command to find the dependent file
Find/-name libstdc++.so.6*
If you can find dependent so files, you can use the CP command to copy the files to the lib64 directory
cp/usr/local/lib64/libstdc++.so.6.0.20/usr/lib64//Copy files
CentOS under System directory is/usr/lib64,suse under the possible system directory will have different
If you have an older file, you can use the RM command to delete the old file first
sudo rm-rf/usr/lib64/libstdc++.so.6 //delete old files
Finally, using the LN command, link to the new file
sudo ln-s/usr/lib64/libstdc++.so.6.0.20/usr/lib64/libstdc++.so.6//link to new version
After these are done, the old can test whether the Dlopen command can open the file normally, if it can be opened normally, then the DllImport way can be used normally
Without the development of DllImport source code, it is very doubtful that it is also called the Linux Dlopen command to invoke the so file
Out of the direct use of the DllImport method to invoke, you can also use a delegate way to invoke the so file
The following is the test code, which can be compared with the full description. Netcore p/invoke mode call so file
1 Public classSotester2 {3 Private Const stringLibraryName ="LIBZMQ";4 5 Const intRtld_now =2;//For Dlopen ' s flags6 Const intRtld_global =8;7 8[DllImport (@"libdl.so.2")]9 Public Static externIntPtr Dlopen (stringFileNameintflags);Ten[DllImport ("libdl.so.2")] One Public Static externIntPtr Dlsym (IntPtr handle,stringsymbol); A -[DllImport ("libdl.so.2", EntryPoint ="Dlopen")] - Private Static externIntPtr unixloadlibrary (String fileName,intflags); the -[DllImport ("libdl.so.2", EntryPoint ="Dlclose", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)] - Private Static extern intunixfreelibrary (IntPtr handle); - +[DllImport ("libdl.so.2", EntryPoint ="Dlsym", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)] - Private Static externIntPtr unixgetprocaddress (IntPtr handle, String symbol); + A[DllImport ("libdl.so.2", EntryPoint ="Dlerror", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)] at Private Static externIntPtr unixgetlasterror (); - - Public Delegate intSumhandler (intAintb); - Public StaticSumhandler Sumfunc =NULL; - -[DllImport ("libnativelib.so", EntryPoint ="sum", CallingConvention = callingconvention.cdecl, CharSet =CharSet.Ansi)] in Public Static extern intSum (intAintb); - to Public voidStart () + { -IntPtr libptr =IntPtr.Zero; the * stringLibname = $"{appcontext.basedirectory}libnativelib.so"; $ Panax NotoginsengLibptr = Unixloadlibrary (Libname,2|8); - the //libptr = Dlopen (Libname, rtld_now); + A if(Libptr! =IntPtr.Zero) theConsole.WriteLine ($"call Dlopen to open {libname} successfully"); + Else -Console.WriteLine ($"call Dlopen to open {libname} failed"); $ $ varSumptr = Unixgetprocaddress (Libptr,"sum"); - - if(Sumptr! =IntPtr.Zero) theConsole.WriteLine ($"Dlopen call sum succeeded"); - ElseWuyiConsole.WriteLine ($"Dlopen call sum failed"); the -Sumfunc = marshal.getdelegateforfunctionpointer<sumhandler>(sumptr); Wu - intret = Sumfunc (1,3); About $Console.WriteLine ($"Call sum result: {ret}"); - - varSumret = Sum (5,7); - AConsole.WriteLine ($"dllimport Call sum result: {Sumret}"); + the //var libname2 = $ "libc.so.6"; - varLibname2 = $"{appcontext.basedirectory}libzmq.so"; $ //var libname2 = $ "{appcontext.basedirectory}libadminconsole.so"; the varConsoleptr = Unixloadlibrary (libname2,2|8); the varErroptr =unixgetlasterror (); theConsole.WriteLine ($"error Description: {marshal.ptrtostringansi (erroptr)}"); the - if(Consoleptr! =IntPtr.Zero) inConsole.WriteLine ($"Open {libname2} succeeded"); the Else theConsole.WriteLine ($"failed to open {libname2}"); About } the}
View Code
. Netcore using P/invoke mode to invoke Linux dynamic library under Linux