Skip to content

Add dll example #818

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

Closed
wants to merge 2 commits into from
Closed

Add dll example #818

wants to merge 2 commits into from

Conversation

Jakobzs
Copy link

@Jakobzs Jakobzs commented May 22, 2021

Hello,

I have created an example for a Windows DLL that displays a messagebox upon being loaded.

Extra notes:
Looking into the window-rs documentation I was not able to locate DWORD nor LPVOID for DllMain so I opted to check the winapi crate, and found:

  1. DWORD is equivalent to c_ulong which is equivalent to u32
  2. LPVOID is equivalent to *mut c_void

@ghost
Copy link

ghost commented May 22, 2021

CLA assistant check
All CLA requirements met.

#[no_mangle]
#[allow(non_snake_case)]
#[allow(unused_variables)]
pub extern "stdcall" fn DllMain(module: HINSTANCE, reason: u32, reserved: *mut c_void) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DllMain returns a BOOL, which maps to an int in C, corresponding to an i32 in Rust.

pub extern "stdcall" fn DllMain(module: HINSTANCE, reason: u32, reserved: *mut c_void) -> bool {
match reason {
DLL_PROCESS_ATTACH => unsafe {
MessageBoxA(None, "Hello from the DLL!", "Hello World", MB_OK);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DllMain runs under the loader lock, making lots of things problematic, like "call[ing] functions in User32.dll or Gdi32.dll". A safe alternative for MessageBoxA would be OutputDebugStringA.

_ => (),
}

true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be adjusted to match the correct return type (see above).

@tim-weis
Copy link
Contributor

I'm not convinced that this example is complete enough. The primary reason to produce a DLL is to have clients consume it. In its current shape, the DLL doesn't export any useful functionality, and there is no client code to consume the library either. The latter is particularly interesting, and how this should be done is still under investigation.

It's usually a good idea to submit an issue to discuss the scope and implementation of an example before pushing a PR.

@Jakobzs
Copy link
Author

Jakobzs commented May 23, 2021

I'm not convinced that this example is complete enough. The primary reason to produce a DLL is to have clients consume it. In its current shape, the DLL doesn't export any useful functionality, and there is no client code to consume the library either. The latter is particularly interesting, and how this should be done is still under investigation.

It's usually a good idea to submit an issue to discuss the scope and implementation of an example before pushing a PR.

Thanks for the feedback Tim.

You are indeed right about BOOL and bool being a mistake since bool only occupies 1 byte, whereas BOOL being an i32 occupies 4 bytes. I have made use of the BOOL struct as found in windows-rs to resolve it.

Previously for testing I used a basic program that called LoadLibrary, but instead now I have created a project dll_loader that takes a filename (or path) as argument and calls LoadLibrary on it.

Lastly I switched from MessageBox to println! based on the reasoning you also specified. I tried OutputDebugStringA but it did not work for me, although I also feel it may not be needed for the example.

In hindsight: Yes, an issue should have been created to discuss the details.

@kennykerr
Copy link
Collaborator

You can of course create a DLL with Rust, but that does not require anything from the Windows crate. Once I have #81 completed, we should be able to add a component example that illustrates how to build a DLL that actually implements various kinds of APIs. Until then I don't think it will be beneficial to add a DLL example.

By the way, writing your own DllMain is almost always a bad idea.

@kennykerr kennykerr closed this May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants