├── .gitattributes
├── .gitignore
├── Builders
├── IWrapperBuilder.cs
├── PortableWrapper.cs
├── PortableWrapperUnsafe.cs
├── SRLHook.cs
├── SRLWrapper.cs
└── Wrapper.cs
├── Extensions.cs
├── LICENSE
├── Main.Designer.cs
├── Main.cs
├── Main.resx
├── Program.cs
├── Readme.md
├── SourceParser.cs
├── WrapperGenerator.csproj
└── WrapperGenerator.sln
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # JustCode is a .NET coding add-in
131 | .JustCode
132 |
133 | # TeamCity is a build add-in
134 | _TeamCity*
135 |
136 | # DotCover is a Code Coverage Tool
137 | *.dotCover
138 |
139 | # AxoCover is a Code Coverage Tool
140 | .axoCover/*
141 | !.axoCover/settings.json
142 |
143 | # Visual Studio code coverage results
144 | *.coverage
145 | *.coveragexml
146 |
147 | # NCrunch
148 | _NCrunch_*
149 | .*crunch*.local.xml
150 | nCrunchTemp_*
151 |
152 | # MightyMoose
153 | *.mm.*
154 | AutoTest.Net/
155 |
156 | # Web workbench (sass)
157 | .sass-cache/
158 |
159 | # Installshield output folder
160 | [Ee]xpress/
161 |
162 | # DocProject is a documentation generator add-in
163 | DocProject/buildhelp/
164 | DocProject/Help/*.HxT
165 | DocProject/Help/*.HxC
166 | DocProject/Help/*.hhc
167 | DocProject/Help/*.hhk
168 | DocProject/Help/*.hhp
169 | DocProject/Help/Html2
170 | DocProject/Help/html
171 |
172 | # Click-Once directory
173 | publish/
174 |
175 | # Publish Web Output
176 | *.[Pp]ublish.xml
177 | *.azurePubxml
178 | # Note: Comment the next line if you want to checkin your web deploy settings,
179 | # but database connection strings (with potential passwords) will be unencrypted
180 | *.pubxml
181 | *.publishproj
182 |
183 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
184 | # checkin your Azure Web App publish settings, but sensitive information contained
185 | # in these scripts will be unencrypted
186 | PublishScripts/
187 |
188 | # NuGet Packages
189 | *.nupkg
190 | # NuGet Symbol Packages
191 | *.snupkg
192 | # The packages folder can be ignored because of Package Restore
193 | **/[Pp]ackages/*
194 | # except build/, which is used as an MSBuild target.
195 | !**/[Pp]ackages/build/
196 | # Uncomment if necessary however generally it will be regenerated when needed
197 | #!**/[Pp]ackages/repositories.config
198 | # NuGet v3's project.json files produces more ignorable files
199 | *.nuget.props
200 | *.nuget.targets
201 |
202 | # Microsoft Azure Build Output
203 | csx/
204 | *.build.csdef
205 |
206 | # Microsoft Azure Emulator
207 | ecf/
208 | rcf/
209 |
210 | # Windows Store app package directories and files
211 | AppPackages/
212 | BundleArtifacts/
213 | Package.StoreAssociation.xml
214 | _pkginfo.txt
215 | *.appx
216 | *.appxbundle
217 | *.appxupload
218 |
219 | # Visual Studio cache files
220 | # files ending in .cache can be ignored
221 | *.[Cc]ache
222 | # but keep track of directories ending in .cache
223 | !?*.[Cc]ache/
224 |
225 | # Others
226 | ClientBin/
227 | ~$*
228 | *~
229 | *.dbmdl
230 | *.dbproj.schemaview
231 | *.jfm
232 | *.pfx
233 | *.publishsettings
234 | orleans.codegen.cs
235 |
236 | # Including strong name files can present a security risk
237 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
238 | #*.snk
239 |
240 | # Since there are multiple workflows, uncomment next line to ignore bower_components
241 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
242 | #bower_components/
243 |
244 | # RIA/Silverlight projects
245 | Generated_Code/
246 |
247 | # Backup & report files from converting an old project file
248 | # to a newer Visual Studio version. Backup files are not needed,
249 | # because we have git ;-)
250 | _UpgradeReport_Files/
251 | Backup*/
252 | UpgradeLog*.XML
253 | UpgradeLog*.htm
254 | ServiceFabricBackup/
255 | *.rptproj.bak
256 |
257 | # SQL Server files
258 | *.mdf
259 | *.ldf
260 | *.ndf
261 |
262 | # Business Intelligence projects
263 | *.rdl.data
264 | *.bim.layout
265 | *.bim_*.settings
266 | *.rptproj.rsuser
267 | *- [Bb]ackup.rdl
268 | *- [Bb]ackup ([0-9]).rdl
269 | *- [Bb]ackup ([0-9][0-9]).rdl
270 |
271 | # Microsoft Fakes
272 | FakesAssemblies/
273 |
274 | # GhostDoc plugin setting file
275 | *.GhostDoc.xml
276 |
277 | # Node.js Tools for Visual Studio
278 | .ntvs_analysis.dat
279 | node_modules/
280 |
281 | # Visual Studio 6 build log
282 | *.plg
283 |
284 | # Visual Studio 6 workspace options file
285 | *.opt
286 |
287 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
288 | *.vbw
289 |
290 | # Visual Studio LightSwitch build output
291 | **/*.HTMLClient/GeneratedArtifacts
292 | **/*.DesktopClient/GeneratedArtifacts
293 | **/*.DesktopClient/ModelManifest.xml
294 | **/*.Server/GeneratedArtifacts
295 | **/*.Server/ModelManifest.xml
296 | _Pvt_Extensions
297 |
298 | # Paket dependency manager
299 | .paket/paket.exe
300 | paket-files/
301 |
302 | # FAKE - F# Make
303 | .fake/
304 |
305 | # CodeRush personal settings
306 | .cr/personal
307 |
308 | # Python Tools for Visual Studio (PTVS)
309 | __pycache__/
310 | *.pyc
311 |
312 | # Cake - Uncomment if you are using it
313 | # tools/**
314 | # !tools/packages.config
315 |
316 | # Tabs Studio
317 | *.tss
318 |
319 | # Telerik's JustMock configuration file
320 | *.jmconfig
321 |
322 | # BizTalk build output
323 | *.btp.cs
324 | *.btm.cs
325 | *.odx.cs
326 | *.xsd.cs
327 |
328 | # OpenCover UI analysis results
329 | OpenCover/
330 |
331 | # Azure Stream Analytics local run output
332 | ASALocalRun/
333 |
334 | # MSBuild Binary and Structured Log
335 | *.binlog
336 |
337 | # NVidia Nsight GPU debugger configuration file
338 | *.nvuser
339 |
340 | # MFractors (Xamarin productivity tool) working folder
341 | .mfractor/
342 |
343 | # Local History for Visual Studio
344 | .localhistory/
345 |
346 | # BeatPulse healthcheck temp database
347 | healthchecksdb
348 |
349 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
350 | MigrationBackup/
351 |
352 | # Ionide (cross platform F# VS Code tools) working folder
353 | .ionide/
354 |
--------------------------------------------------------------------------------
/Builders/IWrapperBuilder.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace WrapperGenerator
4 | {
5 | interface IWrapperBuilder
6 | {
7 | public string Name { get; }
8 | public string BuildWrapper(string Name, Function[] Exports);
9 |
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Builders/PortableWrapper.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace WrapperGenerator
4 | {
5 | class PortableWrapper : IWrapperBuilder
6 | {
7 | public string Name => "Portable Wrapper";
8 |
9 | public string BuildWrapper(string Name, Function[] Exports)
10 | {
11 | StringBuilder Builder = new StringBuilder();
12 | Builder.AppendLine("using System;");
13 | Builder.AppendLine("using System.IO;");
14 | Builder.AppendLine("using System.Reflection;");
15 | Builder.AppendLine("using System.Runtime.InteropServices;");
16 | Builder.AppendLine();
17 | Builder.AppendLine("namespace Wrapper");
18 | Builder.AppendLine("{");
19 | Builder.AppendLine(" /// ");
20 | Builder.AppendLine($" /// This is a wrapper to the {Name}.dll");
21 | Builder.AppendLine(" /// ");
22 | Builder.AppendLine($" public static class {Name.Trim().Replace(" ", "")}");
23 | Builder.AppendLine(" {");
24 | Builder.AppendLine();
25 | Builder.AppendLine(" static string CurrentDllName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);");
26 | Builder.AppendLine(" static string CurrentDllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);");
27 | Builder.AppendLine(" static string RealDllPath = null;");
28 | Builder.AppendLine(" static bool WOW64 => !Environment.Is64BitProcess && Environment.Is64BitOperatingSystem;");
29 | Builder.AppendLine();
30 | Builder.AppendLine(" public static IntPtr RealHandler;");
31 | Builder.AppendLine($" static {Name.Trim().Replace(" ", "")}()");
32 | Builder.AppendLine(" {");
33 | Builder.AppendLine(" if (RealHandler != IntPtr.Zero)");
34 | Builder.AppendLine(" return;");
35 | Builder.AppendLine();
36 | Builder.AppendLine(" RealHandler = LoadLibrary(CurrentDllName);");
37 | Builder.AppendLine();
38 | Builder.AppendLine(" if (RealHandler == IntPtr.Zero)");
39 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
40 |
41 | Builder.AppendLine();
42 | foreach (Function Export in Exports)
43 | {
44 | Builder.AppendLine($" d{Export.Name} = GetDelegate(RealHandler, \"{Export.Name}\", false);");
45 | }
46 | Builder.AppendLine();
47 | Builder.AppendLine(" }");
48 |
49 | Builder.AppendLine();
50 |
51 | foreach (Function Export in Exports)
52 | {
53 | var Return = Export.ReturnType != "void" ? "return " : "";
54 | Builder.AppendLine($" [DllExport(CallingConvention = CallingConvention.{Export.Calling})]");
55 | Builder.AppendLine($" public static {Export}");
56 | Builder.AppendLine(" {");
57 | Builder.AppendLine($" {Return}d{Export.Name}({Export.ArgumentNames});");
58 | Builder.AppendLine(" }");
59 | Builder.AppendLine();
60 | }
61 |
62 | Builder.AppendLine();
63 |
64 |
65 | Builder.AppendLine(" [DllImport(\"kernel32\", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]");
66 | Builder.AppendLine(" internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);");
67 | Builder.AppendLine();
68 | Builder.AppendLine(" [DllImport(\"kernel32\", SetLastError = true, CharSet = CharSet.Unicode)]");
69 | Builder.AppendLine(" internal static extern IntPtr LoadLibraryW(string lpFileName);");
70 | Builder.AppendLine();
71 | Builder.AppendLine(" internal static IntPtr LoadLibrary(string lpFileName)");
72 | Builder.AppendLine(" {");
73 | Builder.AppendLine(" string DllPath = lpFileName;");
74 | Builder.AppendLine(" if (lpFileName.Length < 2 || lpFileName[1] != ':')");
75 | Builder.AppendLine(" {");
76 | Builder.AppendLine(" string DLL = Path.GetFileNameWithoutExtension(lpFileName);");
77 | Builder.AppendLine(" DllPath = Path.Combine(Environment.CurrentDirectory, $\"{DLL}_ori.dll\");");
78 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
79 | Builder.AppendLine(" DllPath = Path.Combine(Environment.CurrentDirectory, $\"{DLL}.dll\");");
80 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
81 | Builder.AppendLine(" DllPath = Path.Combine(CurrentDllPath, $\"{DLL}_ori.dll\");");
82 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
83 | Builder.AppendLine(" DllPath = Path.Combine(CurrentDllPath, $\"{DLL}.dll.ori\");");
84 | Builder.AppendLine(" if (!File.Exists(DllPath))");
85 | Builder.AppendLine(" {");
86 | Builder.AppendLine(" DllPath = WOW64 ? Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) : Environment.SystemDirectory;");
87 | Builder.AppendLine(" DllPath = Path.Combine(DllPath, $\"{DLL}.dll\");");
88 | Builder.AppendLine(" }");
89 | Builder.AppendLine(" }");
90 | Builder.AppendLine(" RealDllPath = DllPath;");
91 | Builder.AppendLine();
92 | Builder.AppendLine(" IntPtr Handler = LoadLibraryW(DllPath);");
93 | Builder.AppendLine();
94 | Builder.AppendLine(" if (Handler == IntPtr.Zero)");
95 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
96 | Builder.AppendLine();
97 | Builder.AppendLine(" return Handler;");
98 | Builder.AppendLine(" }");
99 | Builder.AppendLine();
100 | Builder.AppendLine(" internal static T GetDelegate(IntPtr Handler, string Function, bool Optional = true) where T : Delegate");
101 | Builder.AppendLine(" {");
102 | Builder.AppendLine(" IntPtr Address = GetProcAddress(Handler, Function);");
103 | Builder.AppendLine(" if (Address == IntPtr.Zero)");
104 | Builder.AppendLine(" {");
105 | Builder.AppendLine(" if (Optional)");
106 | Builder.AppendLine(" {");
107 | Builder.AppendLine(" return null;");
108 | Builder.AppendLine(" }");
109 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
110 | Builder.AppendLine(" }");
111 | Builder.AppendLine(" return (T)Marshal.GetDelegateForFunctionPointer(Address, typeof(T));");
112 | Builder.AppendLine(" }");
113 |
114 | Builder.AppendLine();
115 |
116 |
117 |
118 | foreach (Function Export in Exports)
119 | {
120 |
121 | Builder.AppendLine($" static t{Export.Name} d{Export.Name};");
122 | }
123 |
124 | Builder.AppendLine();
125 | foreach (Function Export in Exports)
126 | {
127 | Function tmp = Export;
128 | tmp.Name = "t" + tmp.Name;
129 | Builder.AppendLine($" [UnmanagedFunctionPointer(CallingConvention.{Export.Calling}, CharSet = CharSet.{Export.Charset})]");
130 | Builder.AppendLine($" delegate {tmp};");
131 | }
132 |
133 | Builder.AppendLine();
134 | Builder.AppendLine(" }");
135 | Builder.AppendLine("}");
136 |
137 | return Builder.ToString();
138 | }
139 | }
140 |
141 | }
142 |
--------------------------------------------------------------------------------
/Builders/PortableWrapperUnsafe.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace WrapperGenerator
4 | {
5 | class PortableWrapperUnsafe : IWrapperBuilder
6 | {
7 | public string Name => "Portable Wrapper Unsafe";
8 |
9 | public string BuildWrapper(string Name, Function[] Exports)
10 | {
11 | Exports.SetUnsafeMode(true);
12 |
13 | StringBuilder Builder = new StringBuilder();
14 | Builder.AppendLine("using System;");
15 | Builder.AppendLine("using System.IO;");
16 | Builder.AppendLine("using System.Reflection;");
17 | Builder.AppendLine("using System.Runtime.InteropServices;");
18 | Builder.AppendLine();
19 | Builder.AppendLine("namespace Wrapper");
20 | Builder.AppendLine("{");
21 | Builder.AppendLine(" /// ");
22 | Builder.AppendLine($" /// This is a wrapper to the {Name}.dll");
23 | Builder.AppendLine(" /// ");
24 | Builder.AppendLine($" public unsafe static class {Name.Trim().Replace(" ", "")}");
25 | Builder.AppendLine(" {");
26 | Builder.AppendLine();
27 | Builder.AppendLine(" static string CurrentDllName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);");
28 | Builder.AppendLine(" static string CurrentDllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);");
29 | Builder.AppendLine(" static string RealDllPath = null;");
30 | Builder.AppendLine(" static bool WOW64 => !Environment.Is64BitProcess && Environment.Is64BitOperatingSystem;");
31 | Builder.AppendLine();
32 | Builder.AppendLine(" public static void* RealHandler;");
33 | Builder.AppendLine($" static {Name.Trim().Replace(" ", "")}()");
34 | Builder.AppendLine(" {");
35 | Builder.AppendLine(" if (RealHandler != null)");
36 | Builder.AppendLine(" return;");
37 | Builder.AppendLine();
38 | Builder.AppendLine(" RealHandler = LoadLibrary(CurrentDllName);");
39 | Builder.AppendLine();
40 | Builder.AppendLine(" if (RealHandler == null)");
41 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
42 |
43 | Builder.AppendLine();
44 | foreach (Function Export in Exports)
45 | {
46 | Builder.AppendLine($" d{Export.Name} = GetDelegate(RealHandler, \"{Export.Name}\", false);");
47 | }
48 | Builder.AppendLine();
49 | Builder.AppendLine(" }");
50 |
51 | Builder.AppendLine();
52 |
53 | foreach (Function Export in Exports)
54 | {
55 | var Return = Export.ReturnType != "void" ? "return " : "";
56 | Builder.AppendLine($" [DllExport(CallingConvention = CallingConvention.{Export.Calling})]");
57 | Builder.AppendLine($" public static {Export}");
58 | Builder.AppendLine(" {");
59 | Builder.AppendLine($" {Return}d{Export.Name}({Export.ArgumentNames});");
60 | Builder.AppendLine(" }");
61 | Builder.AppendLine();
62 | }
63 |
64 | Builder.AppendLine();
65 |
66 |
67 | Builder.AppendLine(" [DllImport(\"kernel32\", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]");
68 | Builder.AppendLine(" internal static extern void* GetProcAddress(void* hModule, string procName);");
69 | Builder.AppendLine();
70 | Builder.AppendLine(" [DllImport(\"kernel32\", SetLastError = true, CharSet = CharSet.Unicode)]");
71 | Builder.AppendLine(" internal static extern void* LoadLibraryW(string lpFileName);");
72 | Builder.AppendLine();
73 | Builder.AppendLine(" internal static void* LoadLibrary(string lpFileName)");
74 | Builder.AppendLine(" {");
75 | Builder.AppendLine(" string DllPath = lpFileName;");
76 | Builder.AppendLine(" if (lpFileName.Length < 2 || lpFileName[1] != ':')");
77 | Builder.AppendLine(" {");
78 | Builder.AppendLine(" string DLL = Path.GetFileNameWithoutExtension(lpFileName);");
79 | Builder.AppendLine(" DllPath = Path.Combine(Environment.CurrentDirectory, $\"{DLL}_ori.dll\");");
80 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
81 | Builder.AppendLine(" DllPath = Path.Combine(Environment.CurrentDirectory, $\"{DLL}.dll\");");
82 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
83 | Builder.AppendLine(" DllPath = Path.Combine(CurrentDllPath, $\"{DLL}_ori.dll\");");
84 | Builder.AppendLine(" if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())");
85 | Builder.AppendLine(" DllPath = Path.Combine(CurrentDllPath, $\"{DLL}.dll.ori\");");
86 | Builder.AppendLine(" if (!File.Exists(DllPath))");
87 | Builder.AppendLine(" {");
88 | Builder.AppendLine(" DllPath = WOW64 ? Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) : Environment.SystemDirectory;");
89 | Builder.AppendLine(" DllPath = Path.Combine(DllPath, $\"{DLL}.dll\");");
90 | Builder.AppendLine(" }");
91 | Builder.AppendLine(" }");
92 | Builder.AppendLine(" RealDllPath = DllPath;");
93 | Builder.AppendLine();
94 | Builder.AppendLine(" void* Handler = LoadLibraryW(DllPath);");
95 | Builder.AppendLine();
96 | Builder.AppendLine(" if (Handler == null)");
97 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
98 | Builder.AppendLine();
99 | Builder.AppendLine(" return Handler;");
100 | Builder.AppendLine(" }");
101 | Builder.AppendLine();
102 | Builder.AppendLine(" internal static T GetDelegate(void* Handler, string Function, bool Optional = true) where T : Delegate");
103 | Builder.AppendLine(" {");
104 | Builder.AppendLine(" void* Address = GetProcAddress(Handler, Function);");
105 | Builder.AppendLine(" if (Address == null)");
106 | Builder.AppendLine(" {");
107 | Builder.AppendLine(" if (Optional)");
108 | Builder.AppendLine(" {");
109 | Builder.AppendLine(" return null;");
110 | Builder.AppendLine(" }");
111 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
112 | Builder.AppendLine(" }");
113 | Builder.AppendLine(" return (T)Marshal.GetDelegateForFunctionPointer(new IntPtr(Address), typeof(T));");
114 | Builder.AppendLine(" }");
115 |
116 | Builder.AppendLine();
117 |
118 |
119 |
120 | foreach (Function Export in Exports)
121 | {
122 |
123 | Builder.AppendLine($" static t{Export.Name} d{Export.Name};");
124 | }
125 |
126 | Builder.AppendLine();
127 | foreach (Function Export in Exports)
128 | {
129 | Function tmp = Export;
130 | tmp.Name = "t" + tmp.Name;
131 | Builder.AppendLine($" [UnmanagedFunctionPointer(CallingConvention.{Export.Calling}, CharSet = CharSet.{Export.Charset})]");
132 | Builder.AppendLine($" delegate {tmp};".Replace("delegate unsafe", "unsafe delegate"));
133 | }
134 |
135 | Builder.AppendLine();
136 | Builder.AppendLine(" }");
137 | Builder.AppendLine("}");
138 |
139 | return Builder.ToString();
140 | }
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/Builders/SRLHook.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace WrapperGenerator.Builders
6 | {
7 | class SRLHook : IWrapperBuilder
8 | {
9 | public string Name => "SRL Hook";
10 |
11 | public string BuildWrapper(string Name, Function[] Exports)
12 | {
13 | Exports.SetUnsafeMode(true);
14 |
15 | StringBuilder Builder = new StringBuilder();
16 | Builder.AppendLine("namespace StringReloads.Hook");
17 | Builder.AppendLine("{");
18 | for (int i = 0; i < Exports.Length; i++)
19 | {
20 | var Export = Exports[i];
21 | Builder.AppendLine($" public unsafe class {Export.Name} : Base.Hook<{Export.Name}Delegate>");
22 | Builder.AppendLine(" {");
23 | Builder.AppendLine($" public override string Library => \"{Name}.dll\";");
24 | Builder.AppendLine();
25 | Builder.AppendLine($" public override string Export => \"{Export.Name}\";");
26 | Builder.AppendLine();
27 | Builder.AppendLine(" public override void Initialize()");
28 | Builder.AppendLine(" {");
29 |
30 | var Return = Export.ReturnType != "void" ? "return " : "";
31 | string ExportName = Export.Name;
32 | Export.Name += "Hook";
33 | Builder.AppendLine($" HookDelegate = new {Export.Name}Delegate({Export.Name});");
34 | Builder.AppendLine(" Compile();");
35 | Builder.AppendLine(" }");
36 | Builder.AppendLine();
37 | Builder.AppendLine($" private {Export}");
38 | Builder.AppendLine(" {");
39 | Builder.AppendLine($" {Return}Bypass({Export.ArgumentNames});");
40 | Builder.AppendLine(" }");
41 | Builder.AppendLine();
42 | Export.Name = ExportName;
43 | Builder.AppendLine($" [UnmanagedFunctionPointer(CallingConvention.{Export.Calling}, CharSet = CharSet.{Export.Charset})]");
44 | Export.Name += "Delegate";
45 | Builder.AppendLine($" public delegate {Export};");
46 | Builder.AppendLine(" }");
47 | Builder.AppendLine();
48 | Export.Name = ExportName;
49 | }
50 | Builder.AppendLine("}");
51 |
52 | return Builder.ToString();
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/Builders/SRLWrapper.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace WrapperGenerator
4 | {
5 | class SRLWrapper : IWrapperBuilder
6 | {
7 | public string Name => "SRL Wrapper";
8 |
9 | public string BuildWrapper(string Name, Function[] Exports)
10 | {
11 | Exports.SetAnonType(true);
12 |
13 | StringBuilder Builder = new StringBuilder();
14 | Builder.AppendLine("using System;");
15 | Builder.AppendLine("using System.Runtime.InteropServices;");
16 | Builder.AppendLine("using SRLWrapper.Wrapper.Base;");
17 | Builder.AppendLine("using static SRLWrapper.Wrapper.Base.Wrapper;");
18 | Builder.AppendLine();
19 | Builder.AppendLine("namespace SRLWrapper.Wrapper");
20 | Builder.AppendLine("{");
21 | Builder.AppendLine(" /// ");
22 | Builder.AppendLine($" /// This is a wrapper to the {Name.ToUpperInvariant()}.dll");
23 | Builder.AppendLine(" /// ");
24 | Builder.AppendLine($" public unsafe static class {Name.Trim().Replace(" ", "")}");
25 | Builder.AppendLine(" {");
26 | Builder.AppendLine(" public static void* RealHandler;");
27 | Builder.AppendLine($" public static {Name.Trim().Replace(" ", "")}()");
28 | Builder.AppendLine(" {");
29 | Builder.AppendLine(" if (RealHandler != null)");
30 | Builder.AppendLine(" return;");
31 | Builder.AppendLine();
32 | Builder.AppendLine(" RealHandler = LoadLibrary(CurrentDllName);");
33 | Builder.AppendLine();
34 | Builder.AppendLine(" if (RealHandler == null)");
35 | Builder.AppendLine(" Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED");
36 |
37 | Builder.AppendLine();
38 | foreach (Function Export in Exports)
39 | {
40 | Builder.AppendLine($" d{Export.Name} = GetDelegate(RealHandler, \"{Export.Name}\", false);");
41 | }
42 | Builder.AppendLine();
43 |
44 | Builder.AppendLine(" InitializeSRL();");
45 | Builder.AppendLine(" }");
46 |
47 | Builder.AppendLine();
48 |
49 | foreach (Function Export in Exports)
50 | {
51 | var Return = Export.ReturnType != "void" ? "return " : "";
52 | Builder.AppendLine($" [DllExport(CallingConvention = CallingConvention.{Export.Calling})]");
53 | Builder.AppendLine($" public static {Export}");
54 | Builder.AppendLine(" {");
55 | Builder.AppendLine($" {Return}d{Export.Name}({Export.ArgumentNames});");
56 | Builder.AppendLine(" }");
57 | Builder.AppendLine();
58 | }
59 |
60 | Builder.AppendLine();
61 |
62 | foreach (Function Export in Exports)
63 | {
64 | Builder.AppendLine($" static RET_{Export.Arguments.Length} d{Export.Name};");
65 | }
66 | Builder.AppendLine();
67 | Builder.AppendLine(" }");
68 | Builder.AppendLine("}");
69 |
70 | return Builder.ToString();
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Builders/Wrapper.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace WrapperGenerator
4 | {
5 | class Wrapper : IWrapperBuilder
6 | {
7 | public string Name => "Wrapper";
8 |
9 | public string BuildWrapper(string Name, Function[] Exports)
10 | {
11 | Exports.SetAnonType(true);
12 |
13 | StringBuilder Builder = new StringBuilder();
14 | Builder.AppendLine("using System;");
15 | Builder.AppendLine("using System.Runtime.InteropServices;");
16 | Builder.AppendLine();
17 | Builder.AppendLine("namespace Wrapper");
18 | Builder.AppendLine("{");
19 | Builder.AppendLine(" /// ");
20 | Builder.AppendLine($" /// This is a wrapper to the {Name}.dll");
21 | Builder.AppendLine(" /// ");
22 | Builder.AppendLine($" public static class {Name.Trim().Replace(" ", "")}");
23 | Builder.AppendLine(" {");
24 | Builder.AppendLine();
25 |
26 | foreach (Function Export in Exports)
27 | {
28 | Builder.AppendLine($" [DllImport(\"{Name.Trim().Replace(" ", "")}.dll\", CallingConvention = CallingConvention.{Export.Calling})]");
29 | Builder.AppendLine($" public static extern {Export};");
30 | Builder.AppendLine();
31 | }
32 |
33 | Builder.AppendLine(" }");
34 | Builder.AppendLine("}");
35 |
36 | return Builder.ToString();
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Extensions.cs:
--------------------------------------------------------------------------------
1 | using System.Diagnostics;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace WrapperGenerator
6 | {
7 | static class Extensions
8 | {
9 | ///
10 | /// Waits asynchronously for the process to exit.
11 | ///
12 | /// The process to wait for cancellation.
13 | /// A cancellation token. If invoked, the task will return
14 | /// immediately as canceled.
15 | /// A Task representing waiting for the process to end.
16 | public static Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default(CancellationToken))
17 | {
18 | var tcs = new TaskCompletionSource