-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinvoke.htm
135 lines (124 loc) · 6.4 KB
/
pinvoke.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>PInvoke</title>
</head>
<body><a href="https://github.com/ArsenShnurkov/gentoo-mono-handbook"><img alt="Fork me on GitHub" id="forkme" src="images/forkme.png" align="right" width="100" /></a>
<table><tr><td style="vertical-align:top;">
<h1>PInvoke</h1>
</td><td style="vertical-align:top;">
<a href="index.htm">Gentoo Mono Handbook</a>
<br />
<a href="pc-files.htm">.pc-files</a>
</td></tr></table>
General overview of .so files:
<br />
<a href="http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html">http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html</a>
<br />
<br />
<a href="http://www.mono-project.com/docs/advanced/pinvoke/#linux-shared-library-search-path">http://www.mono-project.com/docs/advanced/pinvoke/#linux-shared-library-search-path</a>
<h3>Installation</h3>
<a href="https://wiki.gentoo.org/wiki/Clang">https://wiki.gentoo.org/wiki/Clang</a>
<br />
<br />
use /etc/portage/env file to enable the use of clang per-package.
<br />
create a new <a href="https://wiki.gentoo.org/wiki//etc/portage/package.env">environment override</a> file (/etc/portage/env/clang):
<br />
CC=clang
<br />
CXX=clang++
<br />
enable use of clang for packages using /etc/portage/package.env file
<br />
app-foo/bar clang
<br />
app-bar/baz clang
<h3>Packaging</h3>
libraries are installed with <a href="https://github.com/gentoo/portage/blob/master/bin/ebuild-helpers/dolib">dolib</a> in portage - <a href="https://devmanual.gentoo.org/function-reference/install-functions/">https://devmanual.gentoo.org/function-reference/install-functions/</a>
<br />
<dl>
<dt><a href="https://github.com/gentoo/portage/blob/master/bin/ebuild-helpers/dolib.so">dolib.so</a></dt>
<dd>Install a library (shared object) file</dd>
<dt><a href="https://github.com/gentoo/portage/blob/master/bin/ebuild-helpers/newlib.so">newlib.so</a></dt>
<dd>Install a .so library file using the second argument as the name</dd>
</dl>
<h3>Building automation (Autotools)</h3>
<a href="https://www.gnu.org/software/automake/manual/html_node/A-Shared-Library.html#A-Shared-Library">https://www.gnu.org/software/automake/manual/html_node/A-Shared-Library.html#A-Shared-Library</a>
<br />
<br />
<a href="https://www.gnu.org/software/automake/manual/html_node/A-Library.html">https://www.gnu.org/software/automake/manual/html_node/A-Library.html</a>
<br />
<br />
<a href="http://mij.oltrelinux.com/devel/autoconf-automake/">http://mij.oltrelinux.com/devel/autoconf-automake/</a>
<br />
<br />
according to the autoconf manual, autoconf will execute a configure.gnu script in the subdirectory if it exists.
<h3>Checking/debugging</h3>
LD_LIBRARY_PATH is a colon-separated list of directories
<br />
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH my_program
<h3>Calling</h3>
<a href="http://www.mono-project.com/docs/advanced/pinvoke/">http://www.mono-project.com/docs/advanced/pinvoke/</a>
<br />
<br />
In principle, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. For example:
<br />
[DllImport ("shared-objects.dll")]
<br />
private static extern int calc(int a, int b);
<br />
It is possible to redefine name with myassembly.dll.config file
<br />
<br />
<configuration>
<br />
<dllmap dll="shared-objects.dll" target="libshared-objects.so.1" />
<br />
</configuration>
<br />
<br />
C ABI is assumed (extern "C"), under Unix platforms, System.Runtime.InteropServices.CallingConvention.<a href="https://en.wikipedia.org/wiki/X86_calling_conventions#cdecl">Cdecl</a> is the default.
<br />
<br />
The general rule of advice is this: never pass classes or structures containing members of reference type (classes) to unmanaged code.
This is because unmanaged code can’t do anything safely with the unmanaged reference (pointer), and the CLI runtime doesn’t do a deep marshal” (marshal members of marshaled classes, and their members, ad infinitum).
<br />
<br />
The immediate net effect of this is that you can’t have array members in marshaled classes
<br />
<br />
For simple types, such as integers and floating-point numbers, marshaling is a bitwise-copy (Njlitting”), just as would be the case for unmanaged code.
In some cases, marshaling can be avoided, such as when passing structures by reference to unmanaged code (a pointer to the structure is copied instead).
It’s also possible to obtain more control over marshaling, through custom marshaling and manual marshaling.
<br />
<!-- For strings DllImport.CharSet default value is CharSet.Ansi -->
Mono on all platforms currently uses UTF-8 encoding for all string marshaling operations.
<br />
StringBuilder gets special marshaling support from the runtime. Always call EnsureCapacity() before passing it into the native method
<br />
you MUST use the StructLayout attribute and specify a LayoutKind value of LayoutKind.Sequential or LayoutKind.Explicit
<br />
structures are LayoutKind.Sequential by default,
<br />
<br />
What do you do if you don’t want the runtime to free the memory? Don’t return a class.
Instead, return an IntPtr (the moral equivalent of a C void* pointer), and then use the Marshal class methods to manipulate that pointer,
such as Marshal.PtrToStructure, which works for both C# struct types and class types marked [StructLayout(LayoutKind.Sequential)].
<h3>Compiling (clang)</h3>
<a href="http://clang.llvm.org/docs/CommandGuide/clang.html">http://clang.llvm.org/docs/CommandGuide/clang.html</a>
<br />
You can pass the <strong>soname</strong> the -Wl flags to clang:
<br />
clang -shared file.o -Wl,-soname,<strong>libfile.so.8</strong> -o file.so
<br />
the <strong>-shared</strong> command line parameter - <a href="http://stackoverflow.com/questions/9170000/how-use-llvm-linker">http://stackoverflow.com/questions/9170000/how-use-llvm-linker</a>
<table><tr><td style="vertical-align:top;">
<h3>Creating</h3>
</td><td style="vertical-align:top;">
<a href="solibrary.htm">.so library example</a>
</td></tr></table>
<a href="https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html">https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html</a>
</body>
</html>