trungnt2910 / MemoryModule.NET

Loads a native Windows assembly... right from your .NET embedded resources. Works on all modern .NET platforms.
MIT License
40 stars 12 forks source link

Provide recipe for SampleDLL.dll and Secret.dll #19

Closed xander-m2k closed 2 years ago

xander-m2k commented 2 years ago

Hi @trungnt2910

Could you provide the recipe for creating the following files in the form of a script or Makefile?

I want to make my own library for the DemoApp example.

I am getting as far as this:

mingw32-g++ Secret.cpp -o Secret.dll --shared -static-libgcc -static-libstdc++
mingw32-g++ SampleDll.cpp -o SampleDLL.dll --shared -static-libgcc -static-libstdc++ Secret.dll

However, when I try these dll files with the DemoApp, I get the following error:

Unhandled Exception: System.ComponentModel.Win32Exception: %1 is not a valid Win32 application
   at MemoryModule.Windows.NativeAssemblyImpl.LoadLibrary(Byte* dataPtr, Int64 length, CustomAllocFunc allocMemory, CustomFreeFunc freeMemory, CustomLoadLibraryFunc loadLibrary, CustomGetProcAddressFunc getProcAddress, CustomFreeLibraryFunc freeLibrary) in C:\Users\<username>\Repos\MemoryModule.NET\MemoryModule\Windows\NativeAssemblyImpl.cs:line 93
   at MemoryModule.NativeAssembly.LoadInternal(Byte* data, Int64 length, String name) in C:\Users\<username>\Repos\MemoryModule.NET\MemoryModule\NativeAssembly.cs:line 119
   at MemoryModule.NativeAssembly.Load(Byte[] data, String name) in C:\Users\<username>\Repos\MemoryModule.NET\MemoryModule\NativeAssembly.cs:line 65
   at DemoApp.Program.Main(String[] args) in C:\Users\<username>\Repos\MemoryModule.NET\DemoApp\Program.cs:line 27

Saying that what I compiled is %1 is not a valid Win32 application.

Therefore, please provide a recipe for compilation of these .dll's.

trungnt2910 commented 2 years ago

What is your version of g++?

Mine is:

g++ (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 11.2.1 20210918
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I don't remember how I built my dlls, but building with your commands, with a few modifications to the suffix:

g++ Secret.cpp -o Secret64.dll --shared -static-libgcc -static-libstdc++
g++ SampleDll.cpp -o SampleDLL64.dll --shared -static-libgcc -static-libstdc++ Secret64.dll

(Note that I'm using the 64 suffix, as the demo app contains this code:

            var asmBytes = File.ReadAllBytes($"SampleDLL{(Environment.Is64BitProcess ? "64" : "")}.dll");

as well as

            string secretAsmName = $"Secret{(Environment.Is64BitProcess ? "64" : "")}.dll";

and my device is running Windows 11 x64)

And then:

dotnet run --framework net48

works well.

Hi, I'm a dummy initializer
Successfully loaded library.
Adding 17 and 12...
17+12=29
Address: 0x1b114c95338
Current global int: 0
Address: 0x1b114c95338
Current global int: 1
Address: 0x1b114c95338
Current global int: 2
On second thread...
Address: 0x1b114c954b8
Current global int: 0
Address: 0x1b114c954b8
Current global int: 1
Address: 0x1b114c954b8
Current global int: 2
Hello World!
Here is my secret: 30
xander-m2k commented 2 years ago

Thanks for the quick reply,

when I execute g++ -v I get the following output:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
trungnt2910 commented 2 years ago

Also, note that loading 32-bit dlls in 64-bit processes is not supported (both by MemoryModule.NET and the original MemoryModule).

Your code probably tripped here:

https://github.com/trungnt2910/MemoryModule.NET/blob/8f57652907b7a24198a3a21763337eb966faa72b/MemoryModule/Windows/NativeAssemblyImpl.cs#L275-L280

xander-m2k commented 2 years ago

Good remark, I will try installing mingw64

xander-m2k commented 2 years ago

Great, that worked! I installed mingw64 from here (GCC 12.1.0), and it worked right away. Also with these commands:

g++ Secret.cpp -o Secret64.dll --shared -static-libgcc -static-libstdc++
g++ SampleDll.cpp -o SampleDLL64.dll --shared -static-libgcc -static-libstdc++ Secret64.dll

Thanks for the help @trungnt2910!