-
Notifications
You must be signed in to change notification settings - Fork 587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] How to get DLLExport work correctly, even when debugging #271
Comments
Related issue #172 This could be a VS debugger issue. I couldn't repro it with dnSpy. I couldn't repro it if I started the debug build from explorer.exe. Workaround for VS debugger: debug Release builds. Or try attaching to a debug build (didn't work for me, VS crashed). |
Those are the tokens and the CLR updates them with the address of the method/stub. I don't know what you did to get this working but if you change dnlib to write 0s you'll get an exception. ======== You should use var methods = module.Types.SelectMany(x => x.Methods)
.Where(x => x.CustomAttributes.Any(IsDllExportAttribute)); |
It writes 4 or 8 bytes, which ILDASM shows as a byte array. You can't write 0s, it must be the token of the method. |
Ok, now i finally understood what you mean and that int32(0) etc. gets read by ilasm and is converted to a bytearray too. This led me to try one more thing:
Options i used for ilasm are The resulting binaries have the exact same size of 9216 bytes on disk but behave differently. Any idea what i or dnlib might be doing wrong here? |
VS' debugger crashes if there's a var ca = module.Assembly.CustomAttributes.Find("System.Diagnostics.DebuggableAttribute");
if (ca != null && ca.ConstructorArguments.Count == 1) {
var arg = ca.ConstructorArguments[0];
// VS' debugger crashes if value == 0x107, so clear EnC bit
if (arg.Type.FullName == "System.Diagnostics.DebuggableAttribute/DebuggingModes" && arg.Value is int value && value == 0x107) {
arg.Value = value & ~(int)DebuggableAttribute.DebuggingModes.EnableEditAndContinue;
ca.ConstructorArguments[0] = arg;
}
} Other stuff: You should add an assembly resolver. If this will be an MSBuild task you should pass in all references to the task and your custom asm resolver should only resolve to one of those assemblies (example). The following code adds a context which has a default asm resolver and type resolver. var creationOptions = new ModuleCreationOptions
{
// If you open more modules, they should share the same context (and asm resolver)
Context = ModuleDef.CreateModuleContext(),
TryToLoadPdbFromDisk = true
}; EDIT: the assembly resolver needs to be disposed too after you don't need it. |
Thanks a lot for your tips and teaching me a bit more about IL code! And thanks for this great project! The EnC tip fixed the debug issue and the ModuleContext lead me to solve the issue of having a forced reference to System.Runtime.CompilerServices.VisualC in netcoreapp3.0 by adding an AssemblyRefUser. I don't plan to release this as an MSBuild task but as a standalone tool. Essentially i only need it to add managed exports to libraries in Snoop (https://github.com/cplotts/snoopwpf/) to get rid of the managed c++ assemblies currently being used. |
Maybe you should add the bit about EnC to the readme about dllexport. Might help others to not run into the same issues i did. |
Yes, I added that to the README 2 days ago. |
Hi there,
i am not sure if i am doing something wrong or if it's an issue with dnlib.
I don't know much about IL code and this is my first try to do IL code manipulation through dnlib.
I also tried to understand the code in PatchSdataBytesBlob from ManagedExportsWriter but i don't even understand whats done there :-(.
Using the way to write dllexports from the readme (+ some more code from me) causes the following sdata to be written:
This causes the MDA (managed debugging assistant) to trap access violations while trying to call the exported method(s).
Changing the sdata to (for an x64 image)
or to (for an x86 image)
fixes the AC issues.
Am i thinking this wrong, or does dnlib something wrong here?
A project showcasing/reproducing what i am doing can be found here: https://github.com/batzen/NativeToManagedTest
To repro just clone the project, build and run the WPFTestApp.
If you run it from within visual studio and click the button "Call managed native" while having the MDA activated (from the exception settings) you should see a "FatalExecutionEngineError" being thrown.
If you need any more details from me feel free to ask. I am also on gitter (https://gitter.im/batzen), if that helps.
The text was updated successfully, but these errors were encountered: