Skip to content
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

LoadLibrary returns a 203 error code #240

Closed
sat0sh1c opened this issue Dec 25, 2024 · 9 comments
Closed

LoadLibrary returns a 203 error code #240

sat0sh1c opened this issue Dec 25, 2024 · 9 comments

Comments

@sat0sh1c
Copy link

Steps to reproduce:
Load the project inside the zip attached to this post and try to run it. The build works just fine, but in unmanaged environment there is an error, when I am trying to load the library via LoadLibrary

  • DllExport -version 1.7.4:
  • Used Visual Studio - Visual Studio 2022 Professional
    Information from Data tab or log data:
    log.txt
    Demo Project files / Samples / etc.:
    err_env.zip
@sat0sh1c sat0sh1c added the bug label Dec 25, 2024
@3F
Copy link
Owner

3F commented Dec 26, 2024

Hello,

Some Windows API functions like LoadLibrary/Ex, GetProcAddress can set the LastError > 0 when they succeed and some even vice versa. Moreover, the case for DllExport is too complicated in context of loading a regular native PE modules because DllExport's modified PE module will still contain imports record on mscoree for CLR init (stub _CorDllMain or _CorExeMain) and everything related in that chain.

This, for example, can produce 126 (0x7E) and 127 (0x7F) even for valid and correct procs. That's why you need to check for a valid module/proc handle first, and only then GetLastError().

I checked your err_env.zip (thanks for the prepared example) but unfortunately 203 (0xCB) is not reproducible for me;
But your configuration doesn't seem to be suitable for netcore-based you need to try ◻ Rebase System Object ...
Activate it either in wizard or update values manually:

<DllExportOurILAsm>true</DllExportOurILAsm>
<DllExportSysObjRebase>true</DllExportSysObjRebase>

Regarding .NET 8 from your csproj:

<TargetFramework>net8.0</TargetFramework>

For DllExport 1.7.4 not all types are supported yet if used modern netcore-based target platforms.
However, your case:

[DllExport]
public static int test_fn(int i) => i;

your native c++

...
((fn)GetProcAddress(lib, "test_fn"))(5);
// 5

Works well for me even for .NET 8

Let me know if there are still any problems with 203 (0xCB)

@sat0sh1c
Copy link
Author

Thanks for the swift answer,
I changed the attributes, that you have mentioned, but I am still getting this error, did you build it from source or just used the version, that has already been built?
Can you please check the binaries, that were provided with that archive? it seems that this issue is local, as the only info, that I found on it was a single 12year old stack over flow post

@3F
Copy link
Owner

3F commented Dec 27, 2024

This is what I was talking about; The error code depends on the actual environment due to the chain I mentioned above. Do not use GetLastError() in order to understand the problem here. I need only information about handles, stack, crash, etc. when you actually call the export test_fn function.

did you build it from source or just used the version, that has already been built?

both; your attached module looks correct

What if netfx-based target platform? Try to step into test_fn when debugging at runtime from your c++ project (don't forget to enable Mixed debugger + disable 3F's IL Assembler because it doesn't produce the .line)

Try with latest stable CI build and activate Refresh intermediate module to be sure:

DllExport -mgr-up -pkg-link https://ci.appveyor.com/api/buildjobs/8aphdlsrgttj1pov/artifacts/bin/Release/DllExport.1.7.4.nupkg

Or I reconfigured it for you: err_env_updated.zip (Unpack - Open err_env.sln in VS IDE - Run F5 - Step Into F11)

@sat0sh1c
Copy link
Author

sat0sh1c commented Dec 29, 2024

Thanks a lot, your provided example seems to work. as for the error, sorry I didn't understood your question properly first time, anyway here is the exception, that I am getting:

Unhandled exception at 0x00007FFC4001CF19 (KernelBase.dll) in err_env.exe: 0xE0434352 (parameters: 0xFFFFFFFF80070002, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x00007FFC1CE70000).

As for the library and function pointer, they store addresses, that seems to be valid, both have the following text in the debugger: Type information missing from symbol file
P.S It still gives this error, but works just as expected, thanks one more time

@3F
Copy link
Owner

3F commented Jan 1, 2025

Thanks a lot, your provided example seems to work

Good!

Unhandled exception at 0x00007FFC4001CF19 (KernelBase.dll) in err_env.exe: 0xE0434352 (parameters: 0xFFFFFFFF80070002, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x00007FFC1CE70000).

Is this an exception was for your initial .NET 8 (coreclr) ? or for .NET Framework 8.1 ?

Can you check the same err_env_updated.zip but for .NET 8 ?
Here's update: err_env_updated_net80.zip (enabled 3F's assembler + disabled PDB because no .line) Same Run F5. What result?
Thanks

@sat0sh1c
Copy link
Author

sat0sh1c commented Jan 3, 2025

Thanks for the provided sources one more time, this example seems to work. However, it works not totally as expected.
In details: I can't step into the C# code any more, it just passes, like I wasn't trying to step into it, just calculates the expected result(which is correct).
Another weird thing, that I found was one line in the debugging log:
Exception thrown at 0x00007FFAF40ECF19 (KernelBase.dll) in err_unmanged.exe: 0x04242420 (parameters: 0x0000000031415927, 0x00007FFAD8BE0000, 0x0000009ABDD8F1B0).
It is the same exception as before, but not the crash doesn't happen. I uploaded all the details, of the log, if this will help in any way.
Thanks in advance
log_exception.txt

@3F
Copy link
Owner

3F commented Jan 3, 2025

it works not totally as expected.
I can't step into the C# code any more, it just passes, like I wasn't trying to step into it, just calculates the expected result(which is correct).

This is actually expected behavior when using my 3F's assembler: #23 (comment)
Can be changed in future releases.

Also note: 1.7.4 does not fully* support modern .NET 8 (*err_env_updated_net80.zip works correctly as you can see). It is planned to resolve in 1.8

Another weird thing, that I found was one line in the debugging log:

Looks like a debugger issue because it is also reproduced for netfx with original assembler when debugger type is Native only; While the Mixed type fixes the case. I can try to review the problem after 1.8 if it really bothers you; please create a new issue.

Regarding GetLastError(), everything that I said above. Thanks for the details you provided!
I'm closing the issue but feel free to continue if needed.

@sat0sh1c
Copy link
Author

sat0sh1c commented Jan 10, 2025

@3F Hi again, can you please tell me what changes have you made to the projects, so it works as expected?

@3F
Copy link
Owner

3F commented Jan 12, 2025

@sat0sh1c Which one? You can use any diff tool on .csproj from err_env_updated.zip & err_env_updated_net80.zip

+ "Refresh intermediate module (obj) using modified" planned for 1.8; available in latest CI build using command:

  • DllExport -mgr-up -pkg-link https://ci.appveyor.com/api/buildjobs/8aphdlsrgttj1pov/artifacts/bin/Release/DllExport.1.7.4.nupkg

And "Use 3F's IL Assembler" + "Rebase" for err_env_updated_net80.zip

+ updated debug options including .vcxproj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants