Max
Max

Reputation: 20014

Debugging Dllimport code

Is it possible to see/debug the code which C# compiler generates for methods with DllImport attribute? I tried stepping into a method defined with DllImport attribute inside the VS debugger but it does not allow me to do this. In the disassembly view I can see the call instruction to a particular address, but when I hit F11 key (step into) it just steps over it. I have "Enable native code debugging" checkbox checked in the project properties.

UPDATE

If I understand correctly, the compiler performs some operations when it sees the DllImportAttribute attached to a method. I couldn't find anything in the DllImportAttribute class documentation. The source code for DllImportAttribute class also has only a very basic class definition. In the debugger when calling an external function I can see a call FFA0C0C8 assembly instruction and when I step into it, it moves immediately to the imported function body, but the imported function has a different address than FFA0C0C8. I couldn't change the disassembly view to this address. It looks like all the dll loading logic is inside this function, which debugger skips. I'm wondering if this is some library function or some function which was generated by the compiler?

Upvotes: 0

Views: 1889

Answers (1)

Ran
Ran

Reputation: 6159

You can debug P/Invoke calls if you enable native debugging, and if you have the symbols and source for the dll.

If you are trying to step into a P/Invoke call into some Windows dll then you probably fail since you don't have the source code for the dll.

EDIT

If you meant that you want to debug the actual CLR code that does the marshalling and the actual native call to the dll, then the answer's no - I don't think that's possible.

Upvotes: 1

Related Questions